Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I always thought about writing a GNU/POSIX utility called 'pp' (prepend), that would simply take an input file and insert it above the first line of a target file. So, essentially, if I have a CSV file without a header and want to quickly insert the header file, I would run:

  pp header.txt spreadsheet.csv
And it would be the equivalent of:

  cat header.txt spreadsheet.csv > temp
  mv temp spreadsheet.csv
But also support all of the expected POSIX niceties, reading from stdin, etc.

But I never got around to it, and it's been a few years since I had to use C at my day-job (which was never great to begin with), so it fell to the wayside.



The old line editor, ed, is good at this for files. I assume you could tinker a bit to get stdin support.

  echo -e "0r header.txt\nw" | ed spreadsheet.csv


Replying to my own post, looks like ex is more straightforward.

  ex -sc '0r header.txt' -c wq spreadsheet.csv
And ugly, but works for stdin:

  echo "whee" | ex -sc '0r /dev/stdin' -c wq somefile


sponge from moreutils is useful for this, eg

    cat a b | sponge a


Why not just `pp() { echo -e "$(cat $1 $2)" > $2;}`? It can read from stdin just fine.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: