Problems to exclude lines and bad filter within AWK script
Hi everyone,
Looking for some help to fix 2 problems I have in my script. (I´m using bash on cygwin) I have the following source file ($7 does not have data): Code:
HEADER_1,HEADER_2,HEADER_3,HEADER_4,HEADER_5,HEADER_6,HEADER_7 Code:
awk 'BEGIN { FS = OFS = "," } #-1) Set "," as field separator Code:
HEADER_1,NEW_HEADER,HEADER_3,HEADER_4,HEADER_5,HEADER_6,HEADER_2 In line 5 of the script, that is " {$2 !~ /pattern4|pattern5|pattern6/ }" oriented to delete lines containing pattern4, pattern5 and pattern6 from column 2 it seems not to be working. What I´m doing wrong in this case? Question 2: If I see the output, the line highlighted in red, is present and should not appear, because this line does not contain nor HEADER nor pattern1 nor pattern2. Why does this happen? Maybe somebody could help me with this. Thanks in advance. |
Code:
{ $2 !~ /pattern4|pattern5|pattern6/ } Code:
{ /HEADER/||/pattern1/||/pattern2/ } Maybe you want something like: Code:
/HEADER/||/pattern1/||/pattern2/ { |
For question 2, the "conditional" part of an AWK stanza applies to the bracketed statements following the condition.
In other words /condition/{statement1;statement2;..}is processed when the "condition" is true. Since the print statement HAS NO CONDITION, it is executed for every line since "no condition" is defined as "true." As to question 1, I believe your syntax is incorrect. Try: Code:
/HEADER|pattern1|pattern2|pattern4|pattern5|pattern6/{next;} |
PTrenholme,
Many thanks for your explanation. Now I got the point, I´m more clear now about the AWK condition and its dunction. Hey colucix, many thanks it works :-)!!! But testing your suggestion I detected something that could give me bad results in the past. Why the use of a brace "{" in the same line or in the next one change the output in so obvious manner? (I explain my question below) I´ve noticed that If I run this script (the right one): Code:
awk ' Code:
HEADER_1,NEW_HEADER,HEADER_3,HEADER_4,HEADER_5,HEADER_6,HEADER_2 Code:
/HEADER/||/pattern1/||/pattern2/ Code:
/HEADER/||/pattern1/||/pattern2/ { Code:
HEADER_1,HEADER_2,HEADER_3,HEADER_4,HEADER_5,HEADER_6,NEW_HEADER Regards. |
In the main body of an AWK program, the syntax is test{expression} with the "test" part defaulting to EMPTY and the "expression" part defaulting to {}. Therefore your second program contains two stanzas in the main body: A "test" followed by a (default) {}, and another stanza consisting an (implied) EMPTY test followed by a print expression that's executed for every input record.
Note that you can use a backslash at the end of an input line to continue it to the next line. So Code:
/HEADER/||/pattern1/||/pattern2/ \ |
PTrenholme,
Really thanks for clarify me this points. At the first attempt to test your suggestion, I got an error even when I put the backslash after ....pattern2/, but the error reported by console it was easy to undertand.(sometimes I just don´t have idea what printed error means) Code:
awk: cmd. line:7: /HEADER/||/pattern1/||/pattern2/ \ becomes in a correct one. I´ve learned so interesting things with answers of you both. I now understand that many things I just enable to get I working script due to apparently little details like these you´ve explained me. This will help me a lot in the future. Many thanks. Best regards. |
I know you have your solution, but thought I would show you how you could neaten it up a bit:
Code:
awk 'BEGIN{OFS=FS=","} |
Hey grail, how are you doing?
Great, great! My script is neated up ussing your suggestion. It´s much better, much faster and much shorter than the first one. I see several things more clear regarding how to use in better way AWK tools and how to find syntax errors. Many thanks for your great and kindly help again. |
All times are GMT -5. The time now is 11:01 AM. |