Operating System - HP-UX
1834155 Members
2474 Online
110064 Solutions
New Discussion

Scripting: Add an entry on a prticular line

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Scripting: Add an entry on a prticular line

Hi All,

Please help me using a script. I have a big file, and I want to add an entry after a certain line. For example if the line contain a work "good" add a line "world".So it looks line this. File called file1 contains :
good morning
good afternoon
every moning
good day

so the file out should be :
good morning
world
good afternoon
world
every morning
good day
world

How can I do that using a script since the file is a big file. Please help. Thanks in advance.

Thanks and Best Regards,
Negara
Santos
3 REPLIES 3
Mark Grant
Honored Contributor
Solution

Re: Scripting: Add an entry on a prticular line

How about

perl -ne 'print; if(/\bgood\b/){print "world\n"}' file1
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: Scripting: Add an entry on a prticular line

Negara

awk '
{print}
/^good/ {print "world"}
' file1 > outfile

The above will only match lines which begin "good". To match lines containing "good" anywhere on the line, take out the "^" character.

- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Dewa Negara_4
Regular Advisor

Re: Scripting: Add an entry on a prticular line

Hi Graham and Mark,

Thanks for your help. I have tried on my file and it was working well using your script. Thanks again.

Best Regards,
Negara
Santos