Nested if not working with /usr/xpg4/bin/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nested if not working with /usr/xpg4/bin/awk
# 1  
Old 05-05-2014
Nested if not working with /usr/xpg4/bin/awk

Hi,
I am trying to do if inside the If in /usr/xpg4/bin/awk. But I am getting below error :

Code:
/usr/xpg4/bin/awk -v a="${THREADIDARR[*]}" 'BEGIN {FS="|"; n=split(a,b," "); for(i=1; i<=n; i++) c[b[i]]=1;} length($3) == 0{ftag == 1{{print}}; length($3) != 0{$3 in c{ftag=1;print;} !$3 in c{ftag=0;}}' $INPUTFILE >> $OUTPUTFILE

Error :
Code:
/usr/xpg4/bin/awk: syntax error  Context is:
>>>      i<=n; i++) c[b[i]]=1;} length($3) == 0{ftag == 1{      <<<

Can anyone suggest how I can implement the If - else and Nested If statement in /usr/xpg4/bin/awk.
# 2  
Old 05-05-2014
Well, right off the top, you seem to have more { than } .. so that's probably confusing the heck out of things.
# 3  
Old 05-05-2014
Definitely. Adding some whitespace to the situation:

Code:
/usr/xpg4/bin/awk -v a="${THREADIDARR[*]}" '
BEGIN {
  FS="|"
  n=split(a,b," ")
  for(i=1; i<=n; i++)
    c[b[i]]=1
}

length($3) == 0 {
  ftag == 1

{{print}}

length($3) != 0 {
  $3 in c

{ftag=1;print;}

!$3 in c {ftag=0;}

}' $INPUTFILE >> $OUTPUTFILE

what's up with the extra {{print}}? I think this could be simplified a lot more if we were given sample input and desired output.
This User Gave Thanks to neutronscott For This Post:
# 4  
Old 05-05-2014
you need to wrap couple of statements in round brackets with if
Code:
/usr/xpg4/bin/awk -v a="${THREADIDARR[*]}" 'BEGIN {FS="|"; n=split(a,b," "); for(i=1; i<=n; i++) c[b[i]]=1;} length($3) == 0{if(ftag == 1) {{print}}; length($3) != 0{if($3 in c) {ftag=1;print;} !($3 in c){ftag=0;}}' $INPUTFILE >> $OUTPUTFILE

This User Gave Thanks to SriniShoo For This Post:
# 5  
Old 05-07-2014
Thanks SriniShoo and neutronscott for great suggestion Smilie Smilie

I have changed following code and it is working fine.... Thanks a lot.... Smilie


Code:
/usr/xpg4/bin/awk -v a="${THREADIDARR[*]}" 'BEGIN {FS="|"; n=split(a,b," "); for(i=1; i<=n; i++) c[b[i]]=1;} length($3) == 0{if(ftag == 1) {{print}};} length($3) != 0{if($3 in c) {ftag=1;print;} if($3 in c == 0) {{ftag=0}};}' $INPUTFILE >> $OUTPUTFILE


Last edited by Don Cragun; 05-07-2014 at 02:42 AM.. Reason: Get rid of font and size changes inside CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. BSD

FreeBSD: /usr/bin/ld not looking in /usr/local/lib

I'm not sure if this is the default behavior for the ld command, but it does not seem to be looking in /usr/local/lib for shared libraries. I was trying to compile the latest version of Kanatest from svn. The autorgen.sh script seems to exit without too much trouble: $ ./autogen.sh checking... (2 Replies)
Discussion started by: AntumDeluge
2 Replies

2. Shell Programming and Scripting

/usr/local/bin/expr function not working

Legends, I am not able to set "expr" function in ksh script. Below is the sample code i used, and output is as "Syntax error" Please help me to come out of it. OUTPUT (9 Replies)
Discussion started by: sdosanjh
9 Replies

3. Solaris

What is the difference between xpg4/bin and usr/bin?

Hi Experts, I found that the same commands(sort, du, df, find, grep etc.) exists in both dir. What is the difference to use them? i.e: to use xpg4/bin/grep and usr/bin/grep My OS version is SunOS 5.10 Regards, Saps (7 Replies)
Discussion started by: saps19
7 Replies

4. OS X (Apple)

When to use /Users/m/bin instead of /usr/local/bin (& whats the diff?)?

Q1. I understand that /usr/local/bin means I can install/uninstall stuff in here and have any chance of messing up my original system files or effecting any other users. I created this directory myself. But what about the directory I didn't create, namely /Users/m/bin? How is that directory... (1 Reply)
Discussion started by: michellepace
1 Replies

5. Solaris

How do I link ld in /usr/ucb/ to /usr/ccs/bin?

Hi all, below is the problem details: ora10g@CNORACLE1>which ld /usr/ucb/ld ora10g@CNORACLE1>cd /usr/ccs/bin ora10g@CNORACLE1>ln -s /usr/ucb/ld ld ln: cannot create ld: File exists ora10g@CNORACLE1> how to link it to /usr/ccs/bin? (6 Replies)
Discussion started by: SmartAntz
6 Replies

6. Linux

/usr/xpg4/bin/awk on linux

Hello people, What is the equivalent of /usr/xpg4/bin/awk on linux? Linux has awk and gawk. But gawk is not the same as /usr/xpg4/bin/awk thanks (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

7. Shell Programming and Scripting

#/usr/bin/csh -f not working as expected?

Hey everyone, A coworker of mine has written a csh script that starts with #!/usr/bin/csh -f. It's my understanding that the -f should skip the .cshrc and .login files, but here's the problem: In the script "line" is used, and I happen to have a "line" in my ~/bin. When the script is ran my... (4 Replies)
Discussion started by: effigy
4 Replies

8. Solaris

/usr/bin/cut not working with largefiles on Solaris 10

I have a person running a perl script that is parsing > 2G log files and pipes to cut -d " " -f 1,6,7,8... The script itself is in a nfs mounted home directory. It runs fine when started from a solaris 8 box but fails after about 400 lines when started from the solaris 10 box. The solaris... (1 Reply)
Discussion started by: wottie
1 Replies

9. UNIX for Dummies Questions & Answers

/bin/sh: /usr/bin/vi: No such file or directory when doing crontab

I just set up an ftp server with Red Hat 5.2. I am doing the work, I'm baby stepping, but it seems like every step I get stuck. Currently, I'm trying to set up a crontab job, but I'm getting the following message: /bin/sh: /usr/bin/vi: No such file or directory. I see that vi exists in /bin/vi,... (3 Replies)
Discussion started by: kwalter
3 Replies
Login or Register to Ask a Question