Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting text in a file

Status
Not open for further replies.

ranjit

Technical User
Apr 14, 2000
131
GB
My input file contains the following extract:

>>Current file<<
default:
SysVal=1035
Other text....

I would like to insert a line such that:
>>Desired output<<
default:
SysVal=1035
NewVal=33997
Other text....

How do I insert text in a file below a particular search string, in this case SysVal=1035?

Thanks



 
The awk way:
awk '
{print}/SysVal=1035/{print "NewVal=33997"}
' /path/to/currentfile >desiredoutput

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Code:
BEGIN{ if (!newval) newval=33997 }

# Print every line.
1
# If line matches, print NewVal.
/^SysVal=1035/ {print "NewVal=" newval}
Save this in file named [tt]insertnewval.awk[/tt]. Run it with
[tt]nawk -f insertnewval.awk inputfile >outputfile[/tt]
(If you don't have nawk, use awk.)
When you want to use a different number for NewVal, do it this way:
[tt]nawk -f insertnewval.awk newval=12345 inputfile >outputfile[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top