awk filter based on column value (variable value)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk filter based on column value (variable value)
# 1  
Old 07-01-2013
awk filter based on column value (variable value)

Hi,

I have a requirement to display/write the 3rd column from a file based on the value in the column 3.

Ex: Data in the File (comma delimited)
Code:
ID,Value,Description
1,A,Active
1,I,Inactive
2,S,Started
1,N,None
2,C,Completed
2,F,Failed

I need to first get a list of all Unique IDs in the file (In this case 1,2), Loop for each ID and Write all values in Column 3 with the ID to a file.

So, I tried to handle this with the below script -

Code:
#!bin/bash
IDList=`awk -F"," '{print $1}' File1.txt | sort -u`
for i in $IDList
do
description=`awk -F"," 'if ($1 == $i) {print $3}' File1.txt | sort -u`
echo $description > myfile$i.txt
done

The awk command for the variable description is unable to parse the variable in it. The same works if I have a constant there -
Code:
description=`awk -F"," 'if ($1 == 1) {print $3}' File1.txt | sort -u`

If you have handled a similar requirement please share your thoughts.

Thanks!

Last edited by Scott; 07-01-2013 at 11:52 PM.. Reason: Added more code tags
# 2  
Old 07-01-2013
You could do:
Code:
awk -F, 'NR>1{F="myfile" $1 ".txt";print $3 >> F;close(F)}' File1.txt

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-02-2013
Thanks a lot Yoda for your expert command, could you please explain me the same a bit I have tried to run and I got the deired Output as user metioned above.

But I want to undertstand it clearly, so if you can explain me, will be grateful to you.


Thanks,
R. Singh
# 4  
Old 07-02-2013
awk print and printf can be used to redirect output to file instead of standard output.
Code:
awk '
        NR > 1 {                        # If total records read > 1 (Skip Header)
                F = "myfile" $1 ".txt"  # Set variable F = "myfile" $1 ".txt" (Construct file name)
                print $3 >> F           # Append output to file (F)
                close(F)                # Close the open file
        }
' File1.txt

For further reference check: Redirecting Output of print and printf
# 5  
Old 07-02-2013
Yoda, Thank you so much for you help.
Is there a way I can just print it out on the screen without writing to the file.

Ex: Output on screen
RuleID 1:
Active
Inactive
None

RuleID 2:
Started
Completed
Failed

Appreciate your help.
# 6  
Old 07-02-2013
Code:
awk -F, '
        NR > 1 {
                A[$1] = A[$1] ? A[$1] RS $3 : $3
        }
        END {
                for ( k in A )
                        print "RuleID" k ":" RS A[k]
        }
' File1.txt

This User Gave Thanks to Yoda For This Post:
# 7  
Old 07-03-2013
Yoda, It would very helpful if you could help me understand the way this functions.
Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter tab file based on column value

Hello I have a tab text file with many columns and have to filter rows ONLY if column 22 has the value of '0', '1', '2' or '3' (out of 0-5). If Column 22 has value '0','1', '2' or '3' (highlighted below), then remove anything less than 10 and greater 100 (based on column 5) AND remove anything... (1 Reply)
Discussion started by: nans
1 Replies

2. Shell Programming and Scripting

awk to filter file based on seperate conditions

The below awk will filter a list of 30,000 lines in the tab-delimited file. What I am having trouble with is adding a condition to SVTYPE=CNV that will only print that line if CI= must be >.05 . The other condition to add is if SVTYPE=Fusion, then in order to print that line READ_COUNT must... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Filter Row Based On Max Column Value After Group BY

Hello Team, Need your expertise on following: Here is the set of data: C1|4|C1SP1|A1|C1BP1|T1 C1|4|C1SP2|A1|C1BP2|T2 C2|3|C2SP1|A2|C2BP1|T2 C3|3|C3SP1|A3|C3BP1|T2 C2|2|C2SP2|A2|C2BP2|T1 I need to filter above date base on following two steps: 1. Group them by column 1 and 4 2.... (12 Replies)
Discussion started by: angshuman
12 Replies

4. Shell Programming and Scripting

UNIX command -Filter rows in fixed width file based on column values

Hi All, I am trying to select the rows in a fixed width file based on values in the columns. I want to select only the rows if column position 3-4 has the value AB I am using cut command to get the column values. Is it possible to check if cut -c3-4 = AB is true then select only that... (2 Replies)
Discussion started by: ashok.k
2 Replies

5. Shell Programming and Scripting

Filter column using awk

Hi all: How do I use an `awk` like: awk '{print $1 "\t" $2}' input on the following input: <head>medium-n</head> nmod+ns+ns-map-n <head>future-n</head> of+n-the+ns-map-n to achieve the following desired output: medium-n nmod+ns+ns-map-n future-n of+n-the+ns-map-n (3 Replies)
Discussion started by: owwow14
3 Replies

6. Linux

Filter a .CSV file based on the 5th column values

I have a .CSV file with the below format: "column 1","column 2","column 3","column 4","column 5","column 6","column 7","column 8","column 9","column 10 "12310","42324564756","a simple string with a , comma","string with or, without commas","string 1","USD","12","70%","08/01/2013",""... (2 Replies)
Discussion started by: dhruuv369
2 Replies

7. Shell Programming and Scripting

AWK filter by column error

I want to found the parent process of the current parent process. I use the following script. ps -ef|grep -v grep |awk $3==$PPID print {$8} It is prompt the following error message: awk: 0602-500 Quitting The source line is 1. I am using AIX 6.1. Would you tell me what wrong of the... (2 Replies)
Discussion started by: cstsang
2 Replies

8. Shell Programming and Scripting

Filter the column and print the result based on condition

Hi all This is my output of the some SQL Query TABLESPACE_NAME FILE_NAME TOTALSPACE FREESPACE USEDSPACE Free ------------------------- ------------------------------------------------------- ---------- --------- ---------... (2 Replies)
Discussion started by: jhon
2 Replies

9. Shell Programming and Scripting

intent: df -kh | filter based on capacity (used space) column where % > 85

I want to accomplish this in sh, however if the capability exists only in other shells elsewhere that's acceptable. % df -kh Filesystem size used avail capacity Mounted on ... /dev/dsk/c0t0d0s1 103G 102G 23M 100% /export/DISK15 ... # output... (5 Replies)
Discussion started by: ProGrammar
5 Replies

10. Shell Programming and Scripting

filter based on column value

I have a file with colon separated values.. the sample is attached below. No of fields in each record/line is dependent on the value of field53. What I need to do is to design a special filter based on specific requirement of some match of values in particular column or combination of columns. ... (2 Replies)
Discussion started by: rraajjiibb
2 Replies
Login or Register to Ask a Question