Append text on particular line after pattern found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append text on particular line after pattern found
# 1  
Old 09-03-2013
Append text on particular line after pattern found

hi,

i have /etc/inittab, I want to add another line after that when i find a pattern "l6:6:wait:/etc/rc.d/rc 6".

Code:
original
l6:6:wait:/etc/rc.d/rc 6

Code:
after-change
l6:6:wait:/etc/rc.d/rc 6
/sbin/if-pp-to-cng

# 2  
Old 09-03-2013
Code:
sed -i.bak '/l6:6:wait:\/etc\/rc.d\/rc 6/ a \/sbin\/if-pp-to-cng' /etc/inittab

# 3  
Old 09-03-2013
Code:
awk -v new="/sbin/if-pp-to-cng" '($0=="l6:6:wait:/etc/rc.d/rc 6") {$0=$0"\n"new} 1' /etc/inittab

# 4  
Old 09-03-2013
Why use a variable, add it directly
Code:
awk '($0=="l6:6:wait:/etc/rc.d/rc 6") {$0=$0"\n/sbin/if-pp-to-cng"} 1' /etc/inittab

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Modify text file if found multiple pattern match for every line.

Looking for help, i have input file like below and want to modify to expected output, if can without create additional file, hope can direct modify it. have 2 thing need do. 1st is adding a word (testplan generation off) after ! ! IPG: Tue Aug 07 14:31:17 2018 2nd is adding... (16 Replies)
Discussion started by: kttan
16 Replies

2. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

3. Shell Programming and Scripting

Displaying text till pattern match found in a line

Hi All, From the below line if we want to display all the text till found pattern dot/. I was trying with the below code but couldn't able to print text before the pattern. it display texts which is found after pattern. awk '/assed/{print;getline;print}' file_name | sed 's/^*. *//' input... (4 Replies)
Discussion started by: Optimus81
4 Replies

4. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

5. UNIX for Dummies Questions & Answers

Append text to files with a same pattern

Hi Folks, I wanted to know if i can use RegEx in a for-loop of a shell script. Here's a scenario, I have a set of files say x1, x2, x3..x9 in a directory(obviously with files other than this pattern). I want to append a line of text to all files that follow pattern x. Can someone help me out? I... (4 Replies)
Discussion started by: prithvirao17
4 Replies

6. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

7. Shell Programming and Scripting

Append next line to previous line when one pattern not found

Hi, I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are... (4 Replies)
Discussion started by: kannansr621
4 Replies

8. Shell Programming and Scripting

To find the line no, where the particular pattern is not found

Hi, suppose i have a txt file containing thye following data 2012156|sb3|nwknjps|BAYONNE|NJ|tcg 201221|094|mtnnjprc:HACKENSACK|NJ|tcg 201222|wn3|mtnnjtc|HACKENSACK|NJ|tcg 2018164|ik4|mtnntc|JERSEY CITY|NJ|tcg 20123482|ik4|mtnnjpritc,JERSEY CITY|NJ|tcg... (3 Replies)
Discussion started by: priyanka3006
3 Replies

9. Shell Programming and Scripting

Sed: add text if pattern not found

Hello, I would like to add the line TIMEZONE="CET" if the pattern TIMEZONE is not found between the range <JOB and JOB> : Example: Src file: <!DOCTYPE DEFTABLE SYSTEM "deftable.dtd"> <DEFTABLE > <JOB TASKTYPE="Job" TIMEFROM="0030" TIMEZONE="CET" </JOB> <JOB... (5 Replies)
Discussion started by: mutunzi
5 Replies

10. Shell Programming and Scripting

how to append the pattern at the end of the line

-Hi I have multiple files which contain a line with the word "exec". I need to add the following pattern " -cmode -ccheap" on the same line where "exec" is at the end. Any idea? Thanks a lot in advance to everybody... -A (2 Replies)
Discussion started by: aoussenko
2 Replies
Login or Register to Ask a Question