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!

Insert multiple lines 3 lines before string 3

Status
Not open for further replies.

dodge20

MIS
Jan 15, 2003
1,048
US
I would like to insert the lines

printf("%s",RETURN)
LINE_COUNT = LINE COUNT + LINE_INC

3 lines before the string (also inserting the blank lines above and below)

function wrln(ln){

I have tried using awk and sed but I keep getting sytax errors, and I really don't think I am very close.

Dodge20
 
I have tried using awk and sed but I keep getting sytax errors
Why not posting what you've tried so far ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Because it is kind of embarrassing.

awk '/function wrln(ln)\{/{print "printf("%s",RETURN)"}3' iastunspd.aster > test.txt

sed '/function wrln(ln)/iprintf("%s",RETURN)' iastunspd.aster > test.txt

I haven't even tried multiple lines, I was just trying to get something to work, but no luck.

Dodge20
 
A starting point:
awk '
/function wrln(ln)\{/{r=NR-3}
NR==FNR{next}
FNR==r{
print "\nprintf(\"%s\",RETURN)\nLINE_COUNT = LINE COUNT + LINE_INC\n"
}
{print}
' iastunspd.aster iastunspd.aster > test.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
What do I need to do to make it actually print something?

Dodge20
 
Doesn't test.txt contain what you wanted ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
No, it is just an exact copy of iastunspd.aster

Dodge20
 
Does iastunspd.aster really have a line (with line# > 3) containing "function wrln(ln){" ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yes it does. I even deleted all the text and made a file that contains only function wrln(ln){
on line 5.

Code:
function wrln(ln){

The permissions are -rwxrwxrwx

Dodge20
 
Hi

PHV, you just copied Dodge20's code... In [tt]awk[/tt] the parenthesis ( () ) are escaped, the braces ( {} ) by default not.
Code:
/function wrln[red]\[/red](ln[red]\[/red]){/{r=NR-3}

Feherke.
 
That looks like that was the problem. Thanks a bunch.

Dodge20
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top