Re: [PLUG] awk: is field blank?
Awk variable NF contains number of fieds. So: awk 'NF==8 { print }' Will print lines with 8 fields. Best, Tomas On Sat, Oct 30, 2021, 11:31 Rich Shepard wrote: > I've a 351K line file with 8 fields. About 50K of those lines has $8 blank. > I want awk to print only rows with values in all 8 fields. I'm not finding > how to tell awk to print $0 if $8 is not blank. > > My awk/sed book doesn't seem to have this information and my web searches > aren't finding the answer, either. > > Clue needed. > > Rich >
Re: [PLUG] awk: is field blank?
On Sat, 30 Oct 2021, Robert Citek wrote: Simplified version of your script: BEGIN {FS="|"} $8 != " " Robert, Thanks for the insights. I've not been aware of some before. Regards, Rich
Re: [PLUG] awk: is field blank?
A few pointers: - {print $0} and {print} are the same - {print} is the default action for any pattern match - since you are printing the entire line, the OFS isn't used and, therefore, doesn't need to be specified - you don't need to specify an "if" as that is part of the structure of an awk script: conditional {action} Simplified version of your script: BEGIN {FS="|"} $8 != " " ... and an example: $ echo -e "a|b|c|d\na|b||d" | awk -F'|' '$3 != ""' a|b|c|d Regards, - Robert On Sat, Oct 30, 2021 at 9:51 AM Rich Shepard wrote: > On Sat, 30 Oct 2021, Robert Citek wrote: > > > Here's a sample: > > $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '{print}' > > a b c d > > a b d > > > $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '$3=="" {print}' > > a b d > > My script uses if and awk doesn't like how I've used it: > BEGIN {FS="|";OFS="|"} > if {$8!=""} { >print $0 > } > > gawk -f clean-broadway-file.awk mean-vel-broadway.dat > out.dat > gawk: clean-broadway-file.awk:8: if {$8==""} { > gawk: clean-broadway-file.awk:8: ^ syntax error > > I want to print the whole line only if $8 is not empty. > > Thanks, > > Rich >
Re: [PLUG] awk: is field blank?
On Sat, 30 Oct 2021, Robert Citek wrote: Here's a sample: $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '{print}' a b c d a b d $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '$3=="" {print}' a b d My script uses if and awk doesn't like how I've used it: BEGIN {FS="|";OFS="|"} if {$8!=""} { print $0 } gawk -f clean-broadway-file.awk mean-vel-broadway.dat > out.dat gawk: clean-broadway-file.awk:8: if {$8==""} { gawk: clean-broadway-file.awk:8: ^ syntax error I want to print the whole line only if $8 is not empty. Thanks, Rich
Re: [PLUG] awk: is field blank?
... and to negate the conditional use an exclamation point (!) : $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '$3 != "" {print}' a b c d Regards, - Robert On Sat, Oct 30, 2021 at 9:46 AM Robert Citek wrote: > Sorry, this item didn't get pasted in my last post: > > $ echo -e "a\tb\tc\td\na\tb\t\td" | awk '{print $1" = "$2" = "$3" = "$4}' > a = b = c = d > a = b = d = > > Notice that you have to specify the field separator with the -F option, > otherwise awk compresses consecutive whitespace characters by default. > > Regards, > - Robert > > On Sat, Oct 30, 2021 at 9:41 AM Robert Citek > wrote: > >> Hello Rich, >> >> Here's a sample: >> >> $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '{print}' >> a b c d >> a b d >> >> $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '$3=="" {print}' >> a b d >> >> Good luck and let us know how it goes. >> >> Regards, >> - Robert >> >> On Sat, Oct 30, 2021 at 9:31 AM Rich Shepard >> wrote: >> >>> I've a 351K line file with 8 fields. About 50K of those lines has $8 >>> blank. >>> I want awk to print only rows with values in all 8 fields. I'm not >>> finding >>> how to tell awk to print $0 if $8 is not blank. >>> >>> My awk/sed book doesn't seem to have this information and my web searches >>> aren't finding the answer, either. >>> >>> Clue needed. >>> >>> Rich >>> >>
Re: [PLUG] awk: is field blank?
Sorry, this item didn't get pasted in my last post: $ echo -e "a\tb\tc\td\na\tb\t\td" | awk '{print $1" = "$2" = "$3" = "$4}' a = b = c = d a = b = d = Notice that you have to specify the field separator with the -F option, otherwise awk compresses consecutive whitespace characters by default. Regards, - Robert On Sat, Oct 30, 2021 at 9:41 AM Robert Citek wrote: > Hello Rich, > > Here's a sample: > > $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '{print}' > a b c d > a b d > > $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '$3=="" {print}' > a b d > > Good luck and let us know how it goes. > > Regards, > - Robert > > On Sat, Oct 30, 2021 at 9:31 AM Rich Shepard > wrote: > >> I've a 351K line file with 8 fields. About 50K of those lines has $8 >> blank. >> I want awk to print only rows with values in all 8 fields. I'm not finding >> how to tell awk to print $0 if $8 is not blank. >> >> My awk/sed book doesn't seem to have this information and my web searches >> aren't finding the answer, either. >> >> Clue needed. >> >> Rich >> >
Re: [PLUG] awk: is field blank?
Hello Rich, Here's a sample: $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '{print}' a b c d a b d $ echo -e "a\tb\tc\td\na\tb\t\td" | awk -F'\t' '$3=="" {print}' a b d Good luck and let us know how it goes. Regards, - Robert On Sat, Oct 30, 2021 at 9:31 AM Rich Shepard wrote: > I've a 351K line file with 8 fields. About 50K of those lines has $8 blank. > I want awk to print only rows with values in all 8 fields. I'm not finding > how to tell awk to print $0 if $8 is not blank. > > My awk/sed book doesn't seem to have this information and my web searches > aren't finding the answer, either. > > Clue needed. > > Rich >
[PLUG] awk: is field blank?
I've a 351K line file with 8 fields. About 50K of those lines has $8 blank. I want awk to print only rows with values in all 8 fields. I'm not finding how to tell awk to print $0 if $8 is not blank. My awk/sed book doesn't seem to have this information and my web searches aren't finding the answer, either. Clue needed. Rich