LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2014, 04:38 PM   #1
rmori
LQ Newbie
 
Registered: Mar 2008
Location: Seguin, Texas
Distribution: Red Hat 9
Posts: 10

Rep: Reputation: 0
Need help inserting 2 lines into a file following a specific pattern


Greetings all:
I am writing a bash script on CentOS 6.4. I have a file that contains text entries of the form:

name1 field2 field3 field4 field5
name2 field2 field3 field4 field5
name3 field2 field3 field4 field5
name1 field2 field3 field4 field5
name4 field2 field3 field4 field5
name5 field2 field3 field4 field5
name2 field2 field3 field4 field5
name1 field2 field3 field4 field5

I have a shell variable named 'newlines' which contains 2 additional lines that I need to insert following the first 'name2' entry without regard to line numbers. I'd also like to perform the insertion into the file in place.

I've tried many iterations of both awk (preferred) and sed to no avail. Can somebody please help me?

Thanks in advance..
 
Old 10-06-2014, 05:39 PM   #2
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware, *BSD
Posts: 379

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
I don't know if sed and awk can do that. This might be ugly, and it needs a temporary file, but it works:

Code:
NAME="name"
NEWLINES="line1\nline2"
FILE="file.txt"
TMP=".${FILE}.tmp"

# Line with first match of $NAME
line=$(grep -m 1 -n "$NAME" "$FILE" | cut -d ':' -f 1)

head -n $line "$FILE" > "$TMP"
echo -e "$NEWLINES" >> "$TMP"
tail -n +$(($line + 1)) "$FILE" >> "$TMP"
mv "$TMP" "$FILE"

Last edited by Philip Lacroix; 10-06-2014 at 05:43 PM.
 
Old 10-06-2014, 06:18 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,805

Rep: Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168
sed got way too ugly - try awk or perl
Code:
awk -v new="$NEWLINES" '{print} ; /name2/ && !x {x++ ;print new}' input.file

Last edited by syg00; 10-06-2014 at 07:02 PM.
 
1 members found this post helpful.
Old 10-06-2014, 07:20 PM   #4
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware, *BSD
Posts: 379

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by syg00
awk -v new="$NEWLINES" '{print} ; /name2/ && !x {x++ ;print new}' input.file
That's a neat one-liner: I've learned something here today.
 
1 members found this post helpful.
Old 10-06-2014, 08:10 PM   #5
rmori
LQ Newbie
 
Registered: Mar 2008
Location: Seguin, Texas
Distribution: Red Hat 9
Posts: 10

Original Poster
Rep: Reputation: 0
Very nice syg00. That worked, but it added the new lines after each of 2 occurrences of 'name2'. I'm looking for a solution that inserts the 2 lines after only the first occurrence. But thank you - very impressive.
 
Old 10-06-2014, 08:14 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,805

Rep: Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168Reputation: 2168
Not on my test - that's what I used x to avoid. What version of Linux, and awk ?.
 
Old 10-06-2014, 08:23 PM   #7
rmori
LQ Newbie
 
Registered: Mar 2008
Location: Seguin, Texas
Distribution: Red Hat 9
Posts: 10

Original Poster
Rep: Reputation: 0
syg00: you're correct - pls excuse my inability to observe the test results. Thanks again. It really did work.

With one exception - the new lines weren't inserted in the original file. But not a big deal. I just redirected output. Thanks again

Last edited by rmori; 10-06-2014 at 08:51 PM. Reason: forgot to add comment
 
  


Reply

Tags
awk, bash, sed


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
Printing $ Lines After Finding Specific Pattern pravin.bhande Programming 6 04-12-2012 10:20 AM
Copy lines starting and ending with specific pattern from multiple files to a file ssn Linux - Newbie 2 07-27-2011 10:44 AM
deleting lines from a file with specific pattern using AWK gandhigaurav1986 Programming 12 06-08-2010 02:08 AM
[SOLVED] Replace pattern in specific lines and column with AWK cgcamal Programming 10 04-26-2010 01:11 AM
Inserting first two lines into a file ZAMO Programming 2 09-04-2008 08:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:46 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