LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Adding text to places in a file (https://www.linuxquestions.org/questions/programming-9/adding-text-to-places-in-a-file-589753/)

helptonewbie 10-05-2007 03:23 PM

Adding text to places in a file
 
Hello,
I have a script that what i want to do is be able to insert a new line in a text file at a specific place where some other text is found, this other text could be at any place in the file so i do:

Code:

grep -n TEXT filename
which i can then awk or cut the line number from no problem, the issue is i want to then say the line number is 40 for that piece of text, i want to add a new line under line number 40 say, so i can input some text into the new line. Its probably something for sed rather than awk my guesses i could probably do it real long way round counting number of lines in file then head to line 40 tail the rest then recreate the file with my text in the middle but there must be a neater method than that??

eg:
line number
40:stuff written here
i want to create new line and enter stuff here
41:stuff written here

Hope this makes sense
Cheers Regards
Mark

helptonewbie 10-05-2007 03:39 PM

no problems i've worked it out, using sed at last

its like this

Code:

sed -e '/text string in file/a\create new line enter this text' FILENAME.BLAH
Cheers
hope its helpful for others

pixellany 10-05-2007 03:41 PM

Look at the "append" (a) command in SED. For example:

cat filename | sed '/^40/a text for the new line'

for every line beginning with "40", add a new line following: "text for the new line"

cfaj 10-05-2007 04:52 PM

Quote:

Originally Posted by pixellany (Post 2914627)
Look at the "append" (a) command in SED. For example:

cat filename | sed '/^40/a text for the new line'


UUOC, and missing a backslash (and for non-GNU sed, a newline) after /a:

Code:

sed '/^40/a\
text for the new line
' filename


pixellany 10-05-2007 05:21 PM

"Useless Use Of Cat" UUOC--I am really really sorry. I hope the penalties are not too severe...
"\" not required unless you want to enter your new line text on a new line!!

I don't do non-GNU......Hmmm, has a sort of poetic sound to it. With some bad grammar, it might sound even better:
I don' do no non GNU.....;)

cfaj 10-05-2007 05:52 PM

Quote:

Originally Posted by pixellany (Post 2914724)
"Useless Use Of Cat" UUOC--I am really really sorry. I hope the penalties are not too severe...


I'll not press for the maximum sentence ... probation, perhaps?
Quote:

"\" not required unless you want to enter your new line text on a new line!!

It is required by the POSIX/SUS standard.
Quote:

I don't do non-GNU......

Then your advice is limited in its usefulness. Many people do not use GNU utilities (and this group is not only for GNU/Linux).

helptonewbie 10-05-2007 06:09 PM

Thanks, but i had already sorted it and posted my fix which is the same as what you guy's put, but never mind thanks anyway

Regards


All times are GMT -5. The time now is 07:15 AM.