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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert line in file through script 1

Status
Not open for further replies.
Aug 3, 2001
29
US
I am looking for an easy instruction to insert text into a file where the record starts with a "QH1" specific text.

the file is on a RS6000 box and its all text with variable lengths.

Anyone with a solution ???
 
Post a sample of the input file and the output you want. CaKiwi
 
Here is what the file is looking like

QH1 INFORMATION 640CHARACTERS
QH4 INFORMATION ADDRESS
QH5 CITY STATE ZIP
QH9 INFORMATION

ALL HAVE VARIABLE LENGTHS

want to put infront of QH1 DATA
a new line
SYS CODES IDS' 80CHARACTERS LONG

Thanks
 
Try this:
Code:
perl -i.old -p -e 'print "foo\n" if /QH1/' file.txt
[tt]file.txt[/tt] will be changed. The original version will be saved as [tt]file.txt.old[/tt]
Cheers, Neil
 
Toolkit, you beat me to it (with an excellent solution).
Here is an awk program in case it is useful.
Code:
$1=="QH1" { print "SYS CODES IDS" }
{print}
CaKiwi
 
Here is a little awk script that I think does what you are looking for:

#cat ins.awk
/^QH1/ {print "SYS CODES IDS\' 80CHARACTERS LONG"
print $0}
!/^QH1/ {print $0}

Run it like this:

#awk -f ins.awk file > new_file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top