awk search column, print line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk search column, print line
# 1  
Old 12-03-2009
awk search column, print line

Hello.

I've been banging my head against walls trying to search a comma delimited file, using awk. I'm trying to search a "column" for a specific parameter, if it matches, then I'd like to print the whole line.

I've read in multiple texts:

awk -F, '{ if ($4 == "string") print $0 }' filename
OR
awk -F, '{ $4 == "string" }' filename

And nothing seems to work? What am I doing wrong?

Info:
OS - Ubuntu 9.10, using bash


BTW, this is my first post on these forums, please forgive any misused etc. etc.. I've been using these forums heavily to learn how to shell script etc.. This is an amazing community, us n00bs are lucky to have something like this. Thank you all so much.

If my question has been answered in another post (which I believe it has, but it didn't work see https://www.unix.com/unix-dummies-que...int-lines.html )... please forgive me.


Thanks again
# 2  
Old 12-03-2009
Quote:
awk -F, '{ if ($4 == "string") print $0 }' filename
OR
awk -F, '{ $4 == "string" }' filename
If you are literally searching for the word "string" this should work fine. But
VAL=string
awk -F, '{ if ($4 == "$VAL") print $0 }' filename

would not work. Are you actually trying something like that? Post your actual code and a sample of your data.
# 3  
Old 12-04-2009
solved

6 mins later I had a reply? Wow! Thanks!

As for my problem... I figured it out.

Sorry, I have no code yet to past, as this was one of the first step to start the process. Should I have asked this question in another forum? I had "script" on the brain.

FWIW:
I need to "ensure" that the comma delimited file I'm working w/, is 100% accurate w/ the data that I'm grabbing from it and massaging. That said, it's a 60,000+ file, and I have to break the rows up by year/period. It has to be perfect. This was my solution:

awk -F, '{ if ( $1 == "\"200301\"" ) print $0 }' <filename>

(egrep -v '"200301"' <filename> worked... but it if that patterned showed up in another column I was screwed)

Sorry for the weak post, I was so frustrated at this last yesterday, I actually stopped lurking on the boards.

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

3. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

String search and print next all lines in one line until blank line

Dear all I want to search special string in file and then print next all line in one line until blank lines come. Help me plz for same. My input file and desire op file is as under. i/p file: A1/EXT "BSCABD1_21233G1" 757 130823 1157 RADIO X-CEIVER ADMINISTRATION BTS EXTERNAL FAULT ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

6. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

awk strings search + print next column after match

Hi, I have a file filled with search strings which have a blank in between and look like this: S. g. Ehr. o. Jg. v. d. Chijs g. Ehr. Now i would like to search for the strings and it also shall return the next column after the match. awk -v FILE="search_strings.txt" 'BEGIN {... (10 Replies)
Discussion started by: sdf
10 Replies

8. Shell Programming and Scripting

awk print second line after search string

I have multiple config files where I need to pull the ip address from loopback3. The format is the same in every file, the ip is the second line after interface loopback3. interface loopback2 loopback description router ID ip address 192.168.1.1 interface loopback3 loopback description... (3 Replies)
Discussion started by: numele
3 Replies

9. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

10. Shell Programming and Scripting

Awk+Grep Input file needs to match a column and print the entire line

I'm having problems since few days ago, and i'm not able to make it works with a simple awk+grep script (or other way to do this). For example, i have a input file1.txt: cat inputfile1.txt 218299910417 1172051195 1172070231 1172073514 1183135117 1183135118 1183135119 1281440202 ... (3 Replies)
Discussion started by: poliver
3 Replies
Login or Register to Ask a Question