LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2007, 03:23 PM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Question 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
 
Old 10-05-2007, 03:39 PM   #2
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
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
 
Old 10-05-2007, 03:41 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
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"
 
Old 10-05-2007, 04:52 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by pixellany View Post
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
 
Old 10-05-2007, 05:21 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740Reputation: 740
"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.....
 
Old 10-05-2007, 05:52 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by pixellany View Post
"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).
 
Old 10-05-2007, 06:09 PM   #7
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 09:50 AM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 11:55 AM
adding text to a file from console rob_xx17 Linux - General 8 02-10-2006 07:16 AM
adding text to lines in a file tpreitano Linux - General 2 10-04-2005 09:30 AM
adding text file browser Linux - General 5 09-09-2005 09:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration