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!

Do a find and add a string in next line 2

Status
Not open for further replies.

waiting485

Programmer
Feb 18, 2005
1
US
I have a file ( programs.list ) which contains all the programs where I need to add one more line after I find a prticular string.

program.list has contents like this.

woctr.c
soptr.c
ptpart.c
adstr.c
.
.
etc...
There are about 200 file names in the program.list

Here is the string which is common in all these .c files.

{create.v}

Now I need to find this string {create.v} and add a line after it so that it looks like following.

{create.v}
{modify.v}

Thanks in Advance to all of you.
 
I think this will do it.
Code:
BEGIN {
    ext = ".bak" # suffix for backups
    lookfor = "{create.v}"
    insert = "{modify.v}"
}
{
    origname = $0
    newname = origname ext
    cmd = "mv " origname " " newname
    if (result = system(cmd)) {
        print "\"" cmd "\" returned \"" result "\"!" > "/dev/stderr"
        next
    }
    while (getline line < newname > 0) {
        print line > origname
        if (line ~ lookfor) {
            print insert > origname
        }
    }
    close(newname)
    close(origname)
}
To invoke:
awk -f scriptname program.list

HTH
 
Create a sub directory, tmp say, and a file, f.awk say, containing

{print > "tmp/" FILENAME}
/\{create\.v\}/{print "{modify.v}" > "tmp/" FILENAME}

Run it using

cat program.list | xargs awk -f f.awk

This will create a set of changed files in the directory tmp

CaKiwi
 
Another way (without awk):
while read f
do echo "/{create\\.v}/a\n{modify.v}\n.\nwq" | ex -s $f
done < program.list

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top