I have some text files. I would like to add a blank line before the first line in each text file. How can I do this with awk?
Use sed 's insert ( i ) option which will insert the text in the preceding line. Also note that some non-GNU sed implementations (for example the one on macOS) require an argument for the -i flag (use -i '' to get the same effect as with GNU sed ).
Original answer: Append printf "\n" at the end of each awk action {} . printf "\n" will print a newline.
I would prefer GNU sed
for this task:
To add a space to the beginning of the file:
sed '1s/.*/ &/' file.txt
To perform this on multiple text files with the .txt
extension, and make the changes directly to the files (i.e. overwrite them), try:
sed -s -i '1s/.*/ &/' *.txt
To add a blank line at the beginning of the file:
sed '1i\\' file.txt
To perform this on multiple text files with the .txt
extension, and make the changes directly to the files (i.e. overwrite them), try:
sed -s -i '1i\\' *.txt
This will give you a blank space at the start of your data file:
awk 'BEGIN{printf(" ")}1' data.txt
Alternatively, this will give you a blank line at the start of your data file.
awk 'BEGIN{print""}1' data.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With