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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: print lines with one of multiple pattern in the same field (column)
# 8  
Old 01-20-2014
great! I knew there had to be a simple and elegant way!

---------- Post updated at 11:46 AM ---------- Previous update was at 11:43 AM ----------

Quote:
Originally Posted by Yoda
If you are looking for pattern match and not for a strict comparison, use ~ operator:
Code:
awk '$3 ~ /A/ || $3 ~ /B/' file.txt

So it looks like this one will return lines with "AB" and "BA" as well as "A" and "B."

Whereas

awk '$3=="A";$3=="B" {print $0}' file.txt

will return only lines that exactly match "A" and "B."

Getting "AB" and "BA" also is actually exactly what I needed (without realizing it initially!). Thanks, all!
# 9  
Old 01-20-2014
Quote:
Originally Posted by elgo4
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. "A" or "B") appears in a column.

I've gotten this far:
awk '$3 == "A" {print $0}' file.txt > newfile.txt
awk '$3 == "B" {print $0}' file.txt >> newfile.txt

but I really want to be able to specify in one step "A" OR "B"...

Many thanks!
Solutions if the order does not matter:
Code:
awk '
$3=="A" {print $0}
$3=="B" {print $0}
' file.txt

Code:
awk '
$3=="A" || $3=="B" {print $0}
' file.txt

Code:
awk '
$3~/^(A|B)$/ {print $0}
' file.txt

(line start/end boundaries required to be equavilent to ==)
If the order matters, first all A matches then all B matches:
Code:
awk '
NF==FNR && $3=="A" {print $0}
NF!=FNR && $3=="B" {print $0}
' file.txt file.txt

Code:
awk '
$3=="A" {print $0} # print immediately
$3=="B" {B[++b]=$0} # store for later
END {
 for (i=1; i<=b; i++) print B[i]
}
' file.txt

---------- Post updated at 12:01 PM ---------- Previous update was at 11:56 AM ----------

Quote:
Originally Posted by elgo4
great! I knew there had to be a simple and elegant way!

---------- Post updated at 11:46 AM ---------- Previous update was at 11:43 AM ----------



So it looks like this one will return lines with "AB" and "BA" as well as "A" and "B."

Whereas

awk '$3=="A";$3=="B" {print $0}' file.txt

will return only lines that exactly match "A" and "B."

Getting "AB" and "BA" also is actually exactly what I needed (without realizing it initially!). Thanks, all!
Then it's shorter
Code:
awk '$3 ~ /[AB]/ {print $0}' file.txt

Note that {print} is short for {print $0}
And that after a condition without {} there is an implicit {print}:
Code:
awk '$3 ~ /[AB]/' file.txt

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Search Pattern and Print lines in Single Column

Hi Experts I have small query where I request the into a single file Suppose: File1: {Unique entries} AA BB CC DD FileB: AA, 123 AA, 234 AA, 2345 CC, 123 CC, 5678 DD,123 BB, 7890 (5 Replies)
Discussion started by: navkanwal
5 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. UNIX for Dummies Questions & Answers

awk to sum column field from duplicate row/lines

Hello, I am new to Linux environment , I working on Linux script which should send auto email based on the specific condition from log file. Below is the sample log file Name m/c usage abc xxx 10 abc xxx 20 abc xxx 5 xyz ... (6 Replies)
Discussion started by: asjaiswal
6 Replies

6. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

7. Shell Programming and Scripting

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"Georgia","Atlanta","2011-11-02","x","","","","" "California","Los Angeles","2011-11-03","x","","","",""... (2 Replies)
Discussion started by: charles33
2 Replies

8. UNIX for Dummies Questions & Answers

print multiple lines from text file based on pattern list

I have a text file with a list of items/patterns: ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig12238 ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig34624... (1 Reply)
Discussion started by: Oyster
1 Replies

9. Shell Programming and Scripting

counting lines containing two column field values with awk

Hello everybody, I'm trying to count the number of consecutive lines in a text file which have two distinctive column field values. These lines may appear in several line blocks within the file, but I only want a single block to be counted. This was my first approach to tackle the problem (I'm... (6 Replies)
Discussion started by: origamisven
6 Replies
Login or Register to Ask a Question