Problem Using If & For loop in AWK Command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem Using If & For loop in AWK Command
# 1  
Old 03-22-2012
Problem Using If & For loop in AWK Command

I am parsing file for the fields using awk command, first i check 26th field for two characters using substr function if it matches then using for loop on array i search 184th field for 4 chars if it matches then i print the required fields but on execution i get the error, please help...
Code:
filename=tempfile.txt
tatafilename=tatamscspc.txt
 
arraycounter=0
while read line; do
  TATAMSC[$arraycounter]=$line
  arraycounter=$((arraycounter+1))
done < $tatafilename
 
awk -F, '{if((substr($1,26,2)=="02")||(substr($1,26,2)=="04") for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)= ${TATAMSC[$i]}) print substr($1,16,10)}' $filename

On executing i get the following error:
Code:
awk: {if((substr($1,26,2)=="02")||(substr($1,26,2)=="04") for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)= ${TATAMSC[$i]}) print substr($1,16,10)}
awk:                                                      ^ syntax error
awk: {if((substr($1,26,2)=="02")||(substr($1,26,2)=="04") for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)= ${TATAMSC[$i]}) print substr($1,16,10)}
awk:                                                                    ^ syntax error


Last edited by Franklin52; 03-22-2012 at 07:19 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 03-22-2012
Quote:
Originally Posted by siramitsharma
awk -F, '{if((substr($1,26,2)=="02")||(substr($1,26,2)=="04")) for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)== ${TATAMSC[$i]}) print substr($1,16,10)}' $filename
another bracket needed for the condition at the start?
# 3  
Old 03-22-2012
New Code:
Code:
awk -F, '{if((substr($1,26,2)=="02")||(substr($1,26,2)=="04")) for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)= ${TATAMSC[$i]}) print substr($1,16,10)}'

Thanks..seems i missed but on doing the changes again i am getting the following error
Code:
awk: {if((substr($1,26,2)=="02")||(substr($1,26,2)=="04")) for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)= ${TATAMSC[$i]}) print substr($1,16,10)}
awk:                                                                     ^ syntax error

Can you please help on this as well

Last edited by Franklin52; 03-22-2012 at 07:20 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 03-22-2012
I guess,

in awk,

you cannot use the {#TATAMSC[@]};
# 5  
Old 03-22-2012
awk

Hi,

You have some syntax errors, we wont use '$' prefix for variables unless thats a system variable in awk(like $0,$1...).

Code:
for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)= ${TATAMSC[$i]})

If You can want to use the shell vartiable inside awk, you have pass the variables by using -v option in awk
Code:
awk -v len=${#TATAMSC[@]} '{print "Length:",len;}' $filename

I dont know your requirement, this is just a hint.

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 6  
Old 03-22-2012
dawonderer,
My new code is as follows, also attaching sample file on which this code:
Code:
awk -F, '{if((substr($1,26,2)=="02")||(substr($1,26,2)=="04")) for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)== ${TATAMSC[$i]}) print substr($1,16,10)}'

Thanks..seems i missed but on doing the changes again i am getting the following error
Code:
awk: {if((substr($1,26,2)=="02")||(substr($1,26,2)=="04")) for (i=0; i< ${#TATAMSC[@]}; i++ ) if(substr($1,184,4)== ${TATAMSC[$i]}) print substr($1,16,10)}
awk:                                                                     ^ syntax error


Last edited by Franklin52; 03-22-2012 at 07:20 AM.. Reason: Please use code tags for code and data samples, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

awk command with if & mail

Hi Experts, I want to check in table that if third column value is "bhu" & if it is greater than 500 it will send mail other wise there will be no mail. but as per my script mails are still coming even value is below 500 with one blank mail(without any content) with subject line"test mail". I... (12 Replies)
Discussion started by: dravi_laxmi
12 Replies

3. Linux

Problem with my loop and awk script

Sorry if this is a super simple issue, but am extremely new to this and am trying to teach myself as I go along. But can someone please help me out? I have a data file similar to this for many samples, for all chromosomes Sample Chr bp p roh Sample1 1 49598178 0 1... (14 Replies)
Discussion started by: vuvuzelo
14 Replies

4. Shell Programming and Scripting

awk & if loop

Dear All, I have a file with 1s and 0s with tab delimited file of approximately 535 lines and 1000 columns. I would like to print the lines, which are having 1s in it and i don't want to print the links with complete 0s. Is it possible to do the above one using awk with some looping. Need... (2 Replies)
Discussion started by: Fredrick
2 Replies

5. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

6. Shell Programming and Scripting

bash & Ksh loop problem

hi i was trying to optimize one script and i came across this problem .. i am putting some pseudo code here $ >cat a.sh ls | while read I do i=$(($i + 1)) done echo "total no of files : " $ >ksh a.sh total no of files : $ >bash a.sh total no of files : why is... (1 Reply)
Discussion started by: zedex
1 Replies

7. UNIX for Dummies Questions & Answers

loop & awk

I cut specific cell(row=2, column=1) in my excel file and created new file using following script. awk -F"," 'NR ==2 {print $1}' file1.csv > file1_new.csv awk -F"," 'NR ==2 {print $1}' file2.csv > file2_new.csv and so on.... The problem is that I have thousand files and I have to... (4 Replies)
Discussion started by: yonhk
4 Replies

8. Shell Programming and Scripting

awk & cut record separator problem

Hi All, I've got some strange behaviour going on when trying to manipulate a file that contains spaces. My input file looks something like this: xxxxxxxxx,yyyy,sss sss sss,bbbbbbb If I use awk: When running from the command line I get: sss sss sss But when running from a... (7 Replies)
Discussion started by: pondlife
7 Replies

9. Shell Programming and Scripting

awk and loop problem

Good morning, Sir's i would like to ask for help regarding to my awk and loop problem, a script that will check my files a and b then if it will see there was a time below 3am it will echo the file that contains below 3am file, for this example it will redirect the file a to an output. $ cat a... (3 Replies)
Discussion started by: invinzin21
3 Replies

10. Shell Programming and Scripting

awk & sed problem

Hello, I am new to shell scripting. I want to optimize my one of the script. I have one file and i want to remove selected zones for domains from that file.In this file i have almost 3500 zones for domains.Sample data for the file.... named.backup... (0 Replies)
Discussion started by: nrbhole
0 Replies
Login or Register to Ask a Question