Using awk, print all the lines where field 8 is equal to x


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk, print all the lines where field 8 is equal to x
# 1  
Old 11-13-2011
Using awk, print all the lines where field 8 is equal to x

Using awk, print all the lines where field 8 is equal to x

I really did try, but this awk thing is really hard to figure out.

file1.txt
Code:
"Georgia","Atlanta","2011-11-02","x","","","",""
"California","Los Angeles","2011-11-03","x","","","",""
"Georgia","Atlanta","2011-11-04","x","x","x","x","x"
"Georgia","Atlanta","2011-11-05","x","x","","",""
"Georgia","Atlanta","2011-11-06","x","","","",""
"Rhode Island","Providence","2011-11-07","x","","","","x"

***OUTPUT***
result1.txt
Code:
"Georgia","Atlanta","2011-11-04","x","x","x","x","x"
"Rhode Island","Providence","2011-11-07","x","","","","x"

I tried all the following with different combinations. They either printed everything or nothing. None of the following is achieving the results. What am I missing here?
Code:
awk '{$8 == "x"} {print}' file1.txt
awk -F',' '($8 == "x") {print}' file1.txt
awk '$8 == "x"' file1.txt
awk '($8 == "x")' file1.txt
awk '{$8 == "x"}' file1.txt > result1.txt
awk -F',' '($8 == "x") {print $0}' file1.txt
awk '$8 = "x"' file1.txt
awk '$8 = /x/' file1.txt
awk -F'/",/"' '{$8 == "x"}' file1.txt
awk -F'",/"' '{$8 == "x"}' file1.txt
awk -F'",/"' '{$8 == "x"}' file1.txt

Also would be great to do field 6.

Thank You.
# 2  
Old 11-13-2011
You are so close:

Code:
awk -F , ' $8 == "\"x\"" {print;}' input-file

If you want to capture lines where either is an x:

Code:
awk -F , ' $8 == "\"x\"" || $6 == "\"x\"" { print; }'

# 3  
Old 11-13-2011
OMG, This is like a live chat; fast reponse. Less than 5 minutes.

Wow, I was beating myself up. Your the best.

Works!

And thanks for the || operator, and how to apply it. I will use this knowledge.

Thanks A Bunch

One more thing........

Just wanted to confirm, the opposite, which is to print only lines which don't contain x.
Code:
awk -F , '!($8 == "\"x\"") {print;}'

It works, but just want to get confirmation if this is the correct way of doing it. Is this correct?

Last edited by charles33; 11-13-2011 at 03:56 PM.. Reason: Ask for confirmation
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk - If field value of consecutive records are the identical print portion of lines

I have some data that looks like this: PXD= ZW< 1,6 QR> QRJ== 1,2(5) QR> QRJ== 4,1(2) QR> QRJ== 4,2 QRB= QRB 4,2 QWM QWM 6,2 R<C ZW< 11,2 R<H= R<J= 6,1 R>H XZJ= 1,2(2) R>H XZJ= 2,6(2) R>H XZJ= 4,1(2) R>H XZJ= 6,2 RDP RDP 1,2 What I would like to do is if fields $1 and $2 are... (5 Replies)
Discussion started by: jvoot
5 Replies

2. Shell Programming and Scripting

awk to print lines based on text in field and value in two additional fields

In the awk below I am trying to print the entire line, along with the header row, if $2 is SNV or MNV or INDEL. If that condition is met or is true, and $3 is less than or equal to 0.05, then in $7 the sub pattern :GMAF= is found and the value after the = sign is checked. If that value is less than... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

Print field after pattern in all lines

data: hello--hello1--hello2--#growncars#vello--hello3--hello4--jello#growncars#dello--gello--gelloA--gelloB#growncars# I want to be able to print all the values that are found between the patterns "#growncars#" and the next "#growncars#" on the same line. so the output should be: ... (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

5. Shell Programming and Scripting

Print ALL lines except if field is 999

Hi All!!! :-) I need a command that will print each line of a text file UNLESS the 3rd field of that line is equal to the number 999. (space seperated fields) Solaris10/BASH SHELL: INPUT.TXT aaa bbb 111 222 ccc ddd 333 444 eee fff 999 555 ggg hhh 666 777 aaa bbb 999 222 ccc ddd 333... (7 Replies)
Discussion started by: ajp7701
7 Replies

6. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

7. Shell Programming and Scripting

awk to print record not equal specific pattern

how to use "awk" to print any record has pattern not equal ? for example my file has 5 records & I need to get all lines which $1=10 or 20 , $2=10 or 20 and $3 greater than "130302" as it shown : 10 20 1303252348212B030 20 10 1303242348212B030 40 34 1303252348212B030 10 20 ... (14 Replies)
Discussion started by: arm
14 Replies

8. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

9. Shell Programming and Scripting

awk field equal something, then add something to the field

Hi Everyone, a.txt a b c 1 e e e e e a b c 2 e e e e e the output is a b c 1 e e e e e a 00b c 2 e e e e e when 4th field = '2', then add '00' in the front of 2nd field value. Thanks (9 Replies)
Discussion started by: jimmy_y
9 Replies

10. Shell Programming and Scripting

Print lines where there's no indent on the first field

Hi All, I need a code to print those lines where there's NO indents on the 1st field Example shown below. I tried to use the below codes but i am not able to see the expected result. Can any expert give any advise ? My Code cat filename| awk '$1 ~ /^+$/ {print $0}' Input 1199 ... (7 Replies)
Discussion started by: Raynon
7 Replies
Login or Register to Ask a Question