-F option and - V in a single awk statment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting -F option and - V in a single awk statment
# 1  
Old 02-27-2009
-F option and - V in a single awk statment

Please let me know if I can use -F option and - V in a single awk statment. I want to import some comma separated shell variables using -F option and defining some static variables inside awk using -v option.
# 2  
Old 02-27-2009
Please let me know if I can use -F option and - V in a single awk statment. I want to import some comma separated shell variables using -F option and defining some static variables inside awk using -v option.

Here is your solution :

Code:
a="any value"      #variable
 
awk -F "," var=$a '{if($2 ~ var) ; print}' file.name

~ is matched operator.

Hope it helps, other wise give ur exact problem that u r facing.
# 3  
Old 02-27-2009
Thanks for your reply.

My input csv file format is as below that contains 1000s of rows. I just need a 6 unique cases of rows.

Code:
1434455221	-	15.12		25.13	85221	65221
1111234222	-	15.13		25.13	94122	64122
1438785223		15.14		25.14	85223	65223
1116652224		15.15		25.15	94124	64124
1116652225	-	0		25.16	94125	64125
1116652226		0		0	94126	64126

to iterate through each row I uses shell variables. But I could not compare the scond column "-" within awk.So I need to pass the shell variables inside awk using as comma separted variables with -F option. To filter 6 cases, I used 6 counter shell variables. But that is getting reset for each iteration. So I need to use static variables using -V option.

If I use all the variables as -v opion , everything is fine but the row is not iterating, that is the coumn values are also treated as static. If I use -F option for all the variables, the counters are not getting reset for each iteration, ie not static. So I need to use -F option for shell variables and -v option for static variables. If there is a work around, please let me know.

Please see the code with -F option and let me know if you want the code that I tried with other options.

#!/bin/bash -x

statfile=test.csv
case1=0
case2=0
case3=0
case4=0
case5=0
case6=0

cat $statfile | while read line

	do
		COLUMN1=$(echo $line | awk -F, '{print $1'})
		COLUMN2=$(echo $line | awk -F, '{print $2'})
		COLUMN3=$(echo $line | awk -F, '{print $3'})
		COLUMN4=$(echo $line | awk -F, '{print $4'})
		COLUMN5=$(echo $line | awk -F, '{print $5'})
		COLUMN6=$(echo $line | awk -F, '{print $6'})
		COLUMN7=$(echo $line | awk -F, '{print $7'})

		newline=$(echo $COLUMN1,$COLUMN2,$COLUMN3,$COLUMN4,$COLUMN5,$COLUMN6,$COLUMN7,$case1,$case2,$case3,$case4,$case5,$case6 | awk -F, '{	
		
		if ( $3 > 0.00 ) {
			if ( $2 != "-" ){
				if ( $8 == 0 ){
					if ( $5 > 0.00 ){
						printf("%.0f,",$1)
						printf("%c,",$2)
						printf("%.2f,",$3)
						printf("%c,",$4)
						printf("%.2f,",$5)
						printf("%s,",$6)
						printf("%s,",$7)
						$8=($8+1)
						printf("%s,","case1")
						printf("%d\n",$8)
					}
										
				}
				else if ( $8 != 0 ){
					if ( $5 == 0.00 ) {
						printf("%.0f,",$1)
						printf("%c,",$2)
						printf("%.2f,",$3)
						printf("%c,",$4)
						printf("%.2f,",$5)
						printf("%s,",$6)
						printf("%s,",$7)
						$10=($10+1)
						printf("%s,","case3")
						printf("%d\n",$10)
					}
				}
			}
			else if ( $2 == "-" ){
				if ( $9 == 0 ){
					if ( $5 > 0.00 ){
						printf("%.0f,",$1)
						printf("%c,",$2)
						printf("%.2f,",$3)
						printf("%c,",$4)
						printf("%.2f,",$5)
						printf("%s,",$6)
						printf("%s,",$7)
						$9++
						printf("%s,","case2")				
						printf("%d\n",$9)
					
					}
				}
				else if ( $11 == 0 )
				{
					if ( $5 == 0.00 ){
						printf("%.0f,",$1)
						printf("%c,",$2)
						printf("%.2f,",$3)
						printf("%c,",$4)
						printf("%.2f,",$5)
						printf("%s,",$6)
						printf("%s,",$7)
						$11++
						printf("%s,","case4")				
						printf("%d\n",$11)					
					}
				}
			}
		}
					
		else if ( $3 == 0.000 ){
			if ( $12 == 0 ){
				if ( $5 > 0.00 ){
					printf("%.0f,",$1)
					printf("%c,",$2)
					printf("%.2f,",$3)
					printf("%c,",$4)
					printf("%.2f,",$5)
					printf("%s,",$6)
					printf("%s,",$7)
					$12++
					printf("%s,","case5")
					printf("%d\n",$12)
				}
			}
			else if ( $13 == 0 ){
				if ( $5 == 0.00 ){
					printf("%.0f,",$1)
					printf("%c,",$2)
					printf("%.2f,",$3)
					printf("%c,",$4)
					printf("%.2f,",$5)
					printf("%s,",$6)
					printf("%s,",$7)
					$13++
					printf("%s,","case6")
					printf("%d",$13)
				}
			}
		}
	}')
									
		echo $newline
		
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

AWK FS with ignore space option

Hi, i have in a situation to get the variables which are having a "|" delimiter and each variable may or may not have spaces, so how can i skip the spaces on the string and print the string as it is. For example: line = "|Hello world|Hello|Hi Guys| read x y z <<<$(echo "$line" | awk -F"|"... (5 Replies)
Discussion started by: tprabhaker
5 Replies

2. Shell Programming and Scripting

Wildcard in a tcsh if statment

Hello everyone I was hoping someone could tell me whether I can use a wildcard inside an tcsh if statement. I am trying to test the argument the user has fed the script and whether it is a .txt file. The Ides behind it is the following if (`echo $1` != *.txt) then echo "wrong... (6 Replies)
Discussion started by: smarones
6 Replies

3. Shell Programming and Scripting

How to implement the logical express --- A(B+C) within single "if" statment in shell script?

On Linux OS, bash environment, I want implement the following code: if && ( || ) A,B,C represents some compare conditions. How to realize it ? Thanks! (1 Reply)
Discussion started by: qcmao
1 Replies

4. Shell Programming and Scripting

bad option(s) with awk

Hi All I am trying to split a string which is like below -p/usr/home/dfusr/adm/props/Comments.properties_-iPEL17 i want to split "_" as delimiter. Please find the code below #!/bin/ksh cd /usr/home/dfusr/backup... (6 Replies)
Discussion started by: rajeshorpu
6 Replies

5. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

6. Red Hat

How to update all rpms using single command option

Can any one help me to know the command option that will update all rpms Thanks in advance.. (2 Replies)
Discussion started by: RajendraKumar
2 Replies

7. UNIX for Dummies Questions & Answers

Clarification for egrep statment

Can someone tell me what exactaly the following command is doing - pid_cmd="/usr/ucb/ps -axww | /usr/bin/egrep '${SUNMC2OSS_PATH}/SunMC2OSS\.jar.* sunmc2oss\.SunMC2OSS\$' | /usr/bin/egrep -v egrep | /usr/bin/nawk '{print \$1}'" Is the egrep is to check "sunmc2oss.SunMC2OSS" process inside... (2 Replies)
Discussion started by: puneet1983
2 Replies

8. Shell Programming and Scripting

awk system date with -d option

Hi I get problems when using the following command : cat logs | awk -F";" '{ system("date -d "1970-01-01 UTC+0100 $1 seconds""); }' date: date invalide `1968641199401200' date: date invalide `1968641199381709' this is what i have in my log file : cat logs 1199401200;a... (3 Replies)
Discussion started by: arag0rn
3 Replies

9. Shell Programming and Scripting

what is wrong with my awk statment?

I am looking to find something in the hour(in field $2) of 03:00:07 and 04:00:07 and 05:00:07 and must contain something in field 4... why doesn't below command work? I try to use grep .. but since I am running this in loop, it's best I use the awk .. can someone please advise.. I am pretty... (1 Reply)
Discussion started by: hankooknara
1 Replies

10. UNIX for Advanced & Expert Users

hwo do I print ' in an awk statment?

hi 2 all, I'm trying 2 print " ' " in an awk statement but the sign deosn't show. The only way I came with is 2 declare the ' as a variable and call that variable. I'm trying 2 do: awk '{printf("insert into ba_memo_01 values ('%s');",$1)}' and get the output: insert into ba_memo_01 values... (7 Replies)
Discussion started by: baraka
7 Replies
Login or Register to Ask a Question