3

File:

TABLE1

1234 
9555    
87676  
2344

Expected output:

Description of the following table:
TABLE1

1234
9555
87676
2344
2
  • @don_crissti: Because this question includes an awk tag, I would argue that it is closely related but not a duplicate.
    – Thor
    May 6, 2016 at 11:43
  • 2
    @Thor it's the same problem, solved in the same way with the same set of tools. that's far more significant than the tags. unless the question says something like "i don't have foo installed" or "i am required to write this in bar" then an answerer is at liberty to ignore the tags and write an answer using whatever tool they think is best for the job at hand....or just to provide an alternate method using a different tool.
    – cas
    May 6, 2016 at 12:04

4 Answers 4

9

Actually echo and cat are enough to do what you want :

echo "Description of the following table:" | cat - file

The - argument tells cat to read from stdin.

3
  • but how to stop output of the this operation to terminal ? Feb 20, 2022 at 13:05
  • @MudassirHussain ctrl-c should stop the output of the cat operation. It sometimes looks like it doesn't if cat has finished outputting its text but your console is still busy displaying (which can take much more time)
    – Aaron
    Feb 21, 2022 at 17:42
  • Not bad, but this doesn't exactly answer OPs question, which seems to want to add "in place". In other words, this does not work, any idea? echo "Description of the following table:" | cat - file > file. EDIT: this does the trick! echo "Description of the following table:" | cat - file | tee file Aug 23, 2022 at 9:33
8

With sed:

$ sed -e '1i\
Description of the following table:
' <file
Description of the following table:
TABLE1

1234
9555
87676
2344
1
  • I almost forgot the i option for sed :D
    – sjsam
    May 6, 2016 at 10:58
6
printf "%s\n" 1 i "Description of the following table:" . w | ed filename

The printf outputs ed commands (one per line) which are then piped into ed filename.

ed edits the file as instructed:

1                                        # go to line 1
i                                        # enter insert mode
Description of the following table:      # text to insert
.                                        # end insert mode
w                                        # write file to disk

BTW, ed performs a real in-place edit, not write-to-temp-file-and-move like sed and most other text editing tools. The edited file keeps the same inode in the filesystem.

3
  • also BTW, ed can do pretty much everything that sed can. sed is the stream-oriented clone of ed, after all. GNU ed's man page is almost useless, but full documentation is available with info ed (or bettter yet, pinfo ed).
    – cas
    May 6, 2016 at 11:43
  • but this prints output to terminal. how to avoid that? Feb 20, 2022 at 12:42
  • @MudassirHussain read the man page. it documents the -s (--silent, --quiet) option. and/or just redirect ed's output to /dev/null as you would with any other program the outputs stuff you don't want to see.
    – cas
    Feb 21, 2022 at 0:52
2

The awk option would be :

gawk '
      BEGIN{print "Description of the following table:"}
      {print $0}' file > temp && mv temp file

A bit more work than sed here because sed has got an in-place edit option -i by which you could directly write to file.

2
  • 1
    If it's a recent gawk it also has -i inplace May 6, 2016 at 11:18
  • @don_crissti : Wow ! That is great news. Mine don't have
    – sjsam
    May 6, 2016 at 11:32

Not the answer you're looking for? Browse other questions tagged .