Home > Archive > AWK > May 2004 > awk simple script queuestion
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
awk simple script queuestion
|
|
| Tomasz K. 2004-04-30, 3:37 pm |
| Hello all,
Coud you please help me to solve small problem?
I was trying to use awk to parse text file and delete the line
containing "key_word".
I was able to use something like this:
awk '!/key_word/' source_file > destination_file
The problem is that I would like to delete the next line after
"key_word". Can anyone help?
Thanks and kind regards, Tom.
--
Those who can, do; those who can't, simulate.
bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Robert Stearns 2004-04-30, 3:37 pm |
| # line contains keyword: remember and go to the next record
/keyword/{delete=1;next}
# previous line contained keyword: forget and go to the next record
delete==1{delete=0;next}
# neither of the above cases: print the record
{print $0}
Tomasz K. wrote:
> Hello all,
>
> Coud you please help me to solve small problem?
>
> I was trying to use awk to parse text file and delete the line
> containing "key_word".
>
> I was able to use something like this:
> awk '!/key_word/' source_file > destination_file
>
> The problem is that I would like to delete the next line after
> "key_word". Can anyone help?
>
> Thanks and kind regards, Tom.
>
| |
| Tomasz K. 2004-04-30, 3:38 pm |
| On Fri, 30 Apr 2004 09:48:47 -0400, Robert Stearns wrote:
> # line contains keyword: remember and go to the next record
> /keyword/{delete=1;next}
> # previous line contained keyword: forget and go to the next record
> delete==1{delete=0;next}
> # neither of the above cases: print the record
> {print $0}
Thank you.
I;m not aware of awk notation, could you please show me how it exactly
should look like?
something like this? :
awk '!/keyword/{delete=1;next};{print $0}' source.txt > destination.txt
--
Those who can, do; those who can't, simulate.
bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Stefan Lagotzki 2004-04-30, 3:38 pm |
| Tomasz K.<mbeonx@mbeonx.pbz-ROT13-> asked:
> I;m not aware of awk notation, could you please show me how it exactly
> should look like?
>
> something like this? :
> awk '!/keyword/{delete=1;next};{print $0}' source.txt > destination.txt
awk ' BEGIN{del=0}
/keyword/ {del=1}
!/keyword/ && del==0 {print}
!/keyword/ && del==1 {del=0}' input > output
Test:
awk ' BEGIN{del=0}
/keyword/ {del=1}
!/keyword/ && del==0 {print}
!/keyword/ && del==1 {del=0}' << EOF
A
B
keyword
X
C
D
keyword
X
E
F
G
EOF
Result:
A
B
C
D
E
F
G
| |
| Charles Demas 2004-05-03, 1:03 pm |
| In article <c6tvtt$uor$07$1@news.t-online.com>,
Stefan Lagotzki <lago20@gmx.de> wrote:
> Tomasz K.<mbeonx@mbeonx.pbz-ROT13-> asked:
>
>awk ' BEGIN{del=0}
> /keyword/ {del=1}
> !/keyword/ && del==0 {print}
> !/keyword/ && del==1 {del=0}' input > output
awk '/keyword/ {del=1; next}
del==0 {print; next}
{del=0}' input > output
is better awk style, IMO
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
| |
| Tomasz K. 2004-05-03, 1:04 pm |
| On Fri, 30 Apr 2004 18:45:09 +0200, Stefan Lagotzki wrote:
> awk ' BEGIN{del=0}
> /keyword/ {del=1}
> !/keyword/ && del==0 {print}
> !/keyword/ && del==1 {del=0}' << EOF
> A
> B
> keyword
> X
> C
> D
> keyword
> X
> E
> F
> G
> EOF
>
> Result:
>
> A
> B
> C
> D
> E
> F
> G
Thanks a lot, that really works :)
I was wondering if you could just tell me how should I modify this to
get output only without "X" and keep "keyword"s?
something like this:
A
B
keywordC
D
keyword
E
F
G
Regards, Tom.
--
Those who can, do; those who can't, simulate.
bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Tomasz K. 2004-05-03, 1:04 pm |
| On Fri, 30 Apr 2004 18:45:09 +0200, Stefan Lagotzki wrote:
> awk ' BEGIN{del=0}
> /keyword/ {del=1}
> !/keyword/ && del==0 {print}
> !/keyword/ && del==1 {del=0}' << EOF
> A
> B
> keyword
> X
> C
> D
> keyword
> X
> E
> F
> G
> EOF
>
> Result:
>
> A
> B
> C
> D
> E
> F
> G
Thanks a lot, that really works :)
I was wondering if you could just tell me how should I modify this to
get output only without "X" and keep "keyword"s?
something like this:
A
B
keyword
C
D
keyword
E
F
G
Regards, Tom.
--
Those who can, do; those who can't, simulate.
bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Charles Demas 2004-05-03, 1:04 pm |
| In article <slrn.pl.c9c6at.lhe.mbeonx@defcon.merkator.pl>,
Tomasz K. <mbeonx@mbeonx.pbz-ROT13-> wrote:
>On Fri, 30 Apr 2004 18:45:09 +0200, Stefan Lagotzki wrote:
>
>Thanks a lot, that really works :)
>
>I was wondering if you could just tell me how should I modify this to
>get output only without "X" and keep "keyword"s?
>
>something like this:
>
>A
>B
>keyword
>C
>D
>keyword
>E
>F
>G
awk '!/X/ ' infile
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
| |
| Charles Demas 2004-05-03, 1:34 pm |
| In article <c6tvtt$uor$07$1@news.t-online.com>,
Stefan Lagotzki <lago20@gmx.de> wrote:
> Tomasz K.<mbeonx@mbeonx.pbz-ROT13-> asked:
>
>awk ' BEGIN{del=0}
> /keyword/ {del=1}
> !/keyword/ && del==0 {print}
> !/keyword/ && del==1 {del=0}' input > output
awk '/keyword/ {del=1; next}
del==0 {print; next}
{del=0}' input > output
is better awk style, IMO
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
| |
| Tomasz K. 2004-05-03, 1:35 pm |
| On Fri, 30 Apr 2004 18:45:09 +0200, Stefan Lagotzki wrote:
> awk ' BEGIN{del=0}
> /keyword/ {del=1}
> !/keyword/ && del==0 {print}
> !/keyword/ && del==1 {del=0}' << EOF
> A
> B
> keyword
> X
> C
> D
> keyword
> X
> E
> F
> G
> EOF
>
> Result:
>
> A
> B
> C
> D
> E
> F
> G
Thanks a lot, that really works :)
I was wondering if you could just tell me how should I modify this to
get output only without "X" and keep "keyword"s?
something like this:
A
B
keywordC
D
keyword
E
F
G
Regards, Tom.
--
Those who can, do; those who can't, simulate.
bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Tomasz K. 2004-05-03, 1:35 pm |
| On Fri, 30 Apr 2004 18:45:09 +0200, Stefan Lagotzki wrote:
> awk ' BEGIN{del=0}
> /keyword/ {del=1}
> !/keyword/ && del==0 {print}
> !/keyword/ && del==1 {del=0}' << EOF
> A
> B
> keyword
> X
> C
> D
> keyword
> X
> E
> F
> G
> EOF
>
> Result:
>
> A
> B
> C
> D
> E
> F
> G
Thanks a lot, that really works :)
I was wondering if you could just tell me how should I modify this to
get output only without "X" and keep "keyword"s?
something like this:
A
B
keyword
C
D
keyword
E
F
G
Regards, Tom.
--
Those who can, do; those who can't, simulate.
bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Charles Demas 2004-05-03, 1:35 pm |
| In article <slrn.pl.c9c6at.lhe.mbeonx@defcon.merkator.pl>,
Tomasz K. <mbeonx@mbeonx.pbz-ROT13-> wrote:
>On Fri, 30 Apr 2004 18:45:09 +0200, Stefan Lagotzki wrote:
>
>Thanks a lot, that really works :)
>
>I was wondering if you could just tell me how should I modify this to
>get output only without "X" and keep "keyword"s?
>
>something like this:
>
>A
>B
>keyword
>C
>D
>keyword
>E
>F
>G
awk '!/X/ ' infile
Chuck Demas
--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
| |
| Pat Phelps 2004-05-04, 12:53 pm |
| Dang nevermind forgot to read his "delete the next line" stmt.
Doh!
Pat
"Tomasz K." <mbeonx@mbeonx.pbz-ROT13-> wrote in message
news:slrn.pl.c94l93.rhh.mbeonx@defcon.merkator.pl...
> Hello all,
>
> Coud you please help me to solve small problem?
>
> I was trying to use awk to parse text file and delete the line
> containing "key_word".
>
> I was able to use something like this:
> awk '!/key_word/' source_file > destination_file
>
> The problem is that I would like to delete the next line after
> "key_word". Can anyone help?
>
> Thanks and kind regards, Tom.
>
> --
> Those who can, do; those who can't, simulate.
> bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
> Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Pat Phelps 2004-05-04, 12:53 pm |
| Ok it boarders on heresy, but may I suggest a simpler solution:
grep -v key_word inputfile > outputfile
Pat
"Tomasz K." <mbeonx@mbeonx.pbz-ROT13-> wrote in message
news:slrn.pl.c94l93.rhh.mbeonx@defcon.merkator.pl...
> Hello all,
>
> Coud you please help me to solve small problem?
>
> I was trying to use awk to parse text file and delete the line
> containing "key_word".
>
> I was able to use something like this:
> awk '!/key_word/' source_file > destination_file
>
> The problem is that I would like to delete the next line after
> "key_word". Can anyone help?
>
> Thanks and kind regards, Tom.
>
> --
> Those who can, do; those who can't, simulate.
> bash$ declare -i n=0;r(){ echo $n;n=n+1;r;};r
> Chcesz o coś spytać? -> http://rtfm.bsdzine.org/
| |
| Patrick TJ McPhee 2004-05-04, 12:53 pm |
| In article <c76da9$o58$1@avnika.corp.mot.com>,
Pat Phelps <Pat.Phelps@motorola.com> wrote:
% Ok it boarders on heresy, but may I suggest a simpler solution:
%
% grep -v key_word inputfile > outputfile
It's not so much heresy as off-topic, and whether that matters depends
partly on whether the answer is correct (it's not), and on whether
you also offer an on-topic solution (you don't). Anyway, your solution
doesn't satisfy the clearly-stated requirement.
--
Patrick TJ McPhee
East York Canada
ptjm@interlog.com
|
|
|
|
|