views:

69

answers:

3

I have something to do that must be finished before 4.00PM.

I want to create a batch file with awk, grep or sed that keeps all lines beginning with 'INSERT' and deletes the other lines.

After this, I want to replace a string "change)" by "servicechange)" when the 3rd word in the treated line is "donextsit".

Please explain how to do this.

+1  A: 
awk '/INSERT/{ 
    if ($3=="donextsit"){
       gsub("change","servicechange");
       print
    }
}' file

since this is homework, something is still not working..you should find out for yourself

ghostdog74
could also avoid the if statement: `awk '/^INSERT/ print}' file`
glenn jackman
I interpret his qns differently. My guess is he wants to print the INSERT statements that doesn't have "donextsit" as well. NB: my solution is not complete on purpose.
ghostdog74
A: 
sed '
    /^INSERT/ ! d;
    /^ *[^ ]\+ *[^ ]\+ *donextsit / s/change)/servicechange)/g;
' -i file

Edit: Incorporated Jonathan Leffler's suggestions. Thx.

edgar.holleis
Easier: `sed '/^INSERT/!d; ...' ...` which saves on labels and branches.
Jonathan Leffler
A: 

Thx to edgar.holleis and ghostdog74.
It seems good ideas but i have some problems again. :(
I want to put the result in a file. How to do this? I think it's the "print" command that may be replaced by something else. Who can help me?

Mat
I believe even with only one rep you can comment on others' answers - that'd be the best place to ask. As for your new question, I think maybe you should look into output redirection - use the google!
Jefromi