Reputation: 4318
I would like to add a header to an ascii
file using bash script
. What is the shortest way to do it without making temporary text file?
Upvotes: 0
Views: 275
Reputation: 74685
You can use the BEGIN
block in awk to print some output before the file is processed:
awk 'BEGIN{print "header text"}1' input.txt > output.txt
A newline will be appended after the header string. If this is undesired, you can use printf
instead. The 1
at the end is a shorthand, which means that all the lines in the file are printed.
In order to overwrite the original file, you can just use a temporary file:
awk 'BEGIN{print "header text"}1' input.txt > tmp && mv tmp input.txt
Upvotes: 1
Reputation: 5092
You can try this way
sed '1 i\Header' FileName
Example:
seq 5 | sed '1 i\\tHeader'
Output:
Header
1
2
3
4
5
Upvotes: 4