Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepend text in file

Tags:

linux

shell

I have used an awk command to find a particular line in a file and would like to prepend this in a second file. Can someone help me in this respect?

like image 324
Shweta Avatar asked Mar 01 '11 05:03

Shweta


People also ask

How do you prepend text in Linux?

1s;^;to be prepended; substitutes the beginning of the first line by the given replacement string, using ; as a command delimiter.

How do you insert a text at the beginning of a file?

Add Text to Beginning of File Using echo and cat Commands Then, we simply output the variable and redirect it to the same file. As you can see above, the new text was added on a new line at the beginning of the file. To add the text at the beginning of the existing first line, use the -n argument of echo.

How do I append to the beginning of a file in Bash?

Append to a File using the Redirection Operator ( >> ) The >> redirection operator appends the output to a given file. There are a number of commands that you can use to print text to the standard output and redirect it to the file, with echo and printf being the most used ones.


1 Answers

The short answer is that you can't. You'll need a temp file.

echo "Prepended Line" > tmpfile && cat origfile >> tmpfile && mv tmpfile origfile

Edit:

sed -i 's/\(line you want\)/Prefix \1/g' origfile
like image 194
Daenyth Avatar answered Oct 05 '22 16:10

Daenyth