Help with the awk syntax


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with the awk syntax
# 8  
Old 06-06-2013
Quote:
Originally Posted by juzz4fun
Sorry it is not working.
one field per line is still how the o/p is... except there is one blank line after every last field...

awk 'NR==FNR{a[$1];next}{for (i = 1; i <= NF; i++)if(!(i in a))print (i==NF?$i RS:$i)}' nofields input
I said printf not print
# 9  
Old 06-06-2013
Oops ! My apologies, Yoda. Smilie
# 10  
Old 06-06-2013
elixir sinari is right about the problem that he pointed out.

So use this code instead:
Code:
awk 'NR==FNR{A[$1];next}{for(i=1;i<=NF;i++) { if( !( i in A )) printf $i OFS } printf "\n" }' nofields input

# 11  
Old 06-06-2013
Quote:
Originally Posted by juzz4fun
Thanks all... I modified my script as below and it worked as expected.

nawk 'NR==FNR{a[$1];next}{for (i = 1; i <= NF; i++)if(!(i in a))printf("%s", i<NF ? $i FS : $i "\n")}' nofields input
I'm surprised this works for you. If nofields contains:
Code:
1
3
5

and input contains:
Code:
1 2 3 4 5
1 2 3 4 5 6 7 
1 2 3 4
1
1 2
1 2 3

the output I get is:
Code:
2 4 2 4 6 7
2 4
2
2

where the first two lines are joined, the last line is incomplete (ending with a <space> instead of a <newline>), and the input line that just contained one field was deleted (where I expected you wanted an empty line) as follows:
Code:
2 4
2 4 6 7
2 4

2
2

where each line is terminated by a <newline> and there is never a <space> immediately before any line's terminating <newline>. If this is what you want, you could try something like:
Code:
nawk '
NR == FNR {
        a[$1]
        next
}
{       first=0
        for(i = 1; i <= NF; i++)
                if(!(i in a))
                        printf("%s%s", first++ ? FS : "", $i)
        print ""
}' nofields input

or, if you insist on a much less readable single line version:
Code:
nawk 'NR==FNR{a[$1];next}{f=0;for(i=1;i<=NF;i++)if(!(i in a))printf("%s%s",f++?FS:"",$i);print ""}' nofields input

I am not a fan of one-liners!

Note that Yoda's script will take care of most of the issues discussed here, but will still sometimes print a field separator at the end of line (before the terminating <newline>).
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 06-06-2013
I didn't faced this problem .. might be due the fact that my input file had all the rows with same number of fields in it... and last field is not in nofields file.
But I must appreciate you for forcing my mind to rethink about this one-liner... Smilie
# 13  
Old 06-07-2013
@Yoda
I would have replaced printf "\n" with print ""
# 14  
Old 06-07-2013
Quote:
Originally Posted by Yoda
elixir sinari is right about the problem that he pointed out.

So use this code instead:
Code:
awk 'NR==FNR{A[$1];next}{for(i=1;i<=NF;i++) { if( !( i in A )) printf $i OFS } printf "\n" }' nofields input

This code silently converts %% into % and will implode if a % is present and not followed by another %.

Regards,
Alister

Last edited by alister; 06-07-2013 at 01:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: syntax error

awk -F, ' NR>1 { BEGIN{ chr=$2 } END{ for (k in chr) {print k} } } ' $gene_file I've a really simple code. I want to store gene name and it's chromosome and in the end print them. I'm skipping line one as it contains headers. But I don't know why I get error as: (2 Replies)
Discussion started by: genome
2 Replies

2. Shell Programming and Scripting

Syntax Problem with awk

Hello, I have perl script,which take some part of data in the file. the below command works fine in normal cmd prompt. `awk '/CDI/ && // && !/Result for/ {print $3 $5 > "final.txt"}' datalist.txt`; `nawk -F"" '{print $2}' finalcdi.txt`; But not working. Please use code tags, thanks. (5 Replies)
Discussion started by: rasingraj
5 Replies

3. Shell Programming and Scripting

AWK Function syntax

Hi, I would like to know what is the correct syntax to perform a function in awk. Although I have seen several examples, not get it to work, this is what I'm trying: #!/bin/bash awk function multi (number) { return number * 3 } print multi (4)Thanks (2 Replies)
Discussion started by: Godie
2 Replies

4. Shell Programming and Scripting

awk syntax

Hi I have a bash file which will split a big file to many small files. But I got a syntax error.H="$(head -1 CCC.tped)" awk 'print $0 > $1 ".tped"' CCC.tped for f in $(ls *.tped); do echo "$H\n" "$(cat $f)" >$f; done And -bash-4.1$ bash split awk: print $0 > $1".tped" awk: ^ syntax error... (3 Replies)
Discussion started by: zhshqzyc
3 Replies

5. AIX

Help with syntax using AWK

I have a file which is comma separated and has quotes. I can use this command and awk -F"," '{ if ($4=="01" print $0 }' test.txt But this doesn't fetch me the data.since it has quotes. If the data has no quotes,the above command works fine. In Unix you can skip quote \" but this doesn't work.... (7 Replies)
Discussion started by: ganesnar
7 Replies

6. Shell Programming and Scripting

AWK syntax help

i have a ksh code that needs to be written in AWK. can someone please help me here? :( if }" | grep -c "$2") -gt 0 ] ; then print - "found $2 in array ignore" else print - "did not find $2 in array ignore" fi ignore=4ty56r ignore=er45ty . . . ignore=frhtg2 (27 Replies)
Discussion started by: usustarr
27 Replies

7. UNIX for Dummies Questions & Answers

awk syntax

Little bit confusing while using awk :confused::confused: In Sed while pattern search we can use "(double quotes) i mean $a=hello $cat file.txt |sed -n "/$a/p"this thing work fine But if i use it in awk it's not working How could i do the substitution of pattern by a variables and the... (1 Reply)
Discussion started by: posix
1 Replies

8. Shell Programming and Scripting

AWK syntax

Hi I am trying to understand AWK syntax so I tried this command which gives me the home directory of root awk 'BEGIN { FS = ":"} {if ($1 == "root") print $6 }' /etc/passwd I would know what are the following commands doing. The first one prints all /etc/passwd, second prints nothing. ... (4 Replies)
Discussion started by: wakatana
4 Replies

9. Shell Programming and Scripting

awk syntax help

I don't get correct output when I run this command line: nmap -sP failedhost.com | grep -i failed | awk -F '{print $6}' I basically want it to return 'failedhost.com' but its just showing the output of the nmap scan. (8 Replies)
Discussion started by: streetfighter2
8 Replies

10. Shell Programming and Scripting

Help with Awk Syntax

I have written many awk commands which go in multiple lines. I have this confusion many times. Some time they work if i dont terminate them with "\" but some time error. Some time in "if" statements between if and else if i dont use ";" it gives error but sometimes it doesnt. The below... (4 Replies)
Discussion started by: pinnacle
4 Replies
Login or Register to Ask a Question