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)
# 1  
Old 01-20-2014
Linux 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. "A" or "B") appears in a column.

I've gotten this far:
Code:
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!

Last edited by Franklin52; 01-21-2014 at 04:16 AM.. Reason: Please use code tags
# 2  
Old 01-20-2014
It should be as follows:
Code:
awk '$3=="A";$3=="B" {print $0}' file.txt

Regards,
Srikanth

Last edited by Franklin52; 01-21-2014 at 04:17 AM.. Reason: Code tags
This User Gave Thanks to srikanth38 For This Post:
# 3  
Old 01-20-2014
If you are looking for pattern match and not for a strict comparison, use ~ operator:
Code:
awk '$3 ~ /A/ || $3 ~ /B/' file.txt


Last edited by Yoda; 01-20-2014 at 01:29 PM.. Reason: correction
This User Gave Thanks to Yoda For This Post:
# 4  
Old 01-20-2014
Code:
$ awk '$3 ~ /A|B/' file

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 01-20-2014
Code:
awk '$3=="A" || $3=="B"  {print $0}'  file.txt

This User Gave Thanks to blackrageous For This Post:
# 6  
Old 01-20-2014
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


Code:
$ cat <<test | awk '$1 ~ /A|B/'
A
C
B
BB
D
test
A
B
BB

$ cat <<test | awk '$1 ~ A || $1 ~ B' # Variable  ???
A
C
B
BB
D
test

A
C
B
BB
D

$ cat <<test | awk '$1 ~ /A/ || $1 ~ /B/'
A
C
B
BB
D
test

A
B
BB

$ cat <<test | awk '$1 ~ A || $1 ~ B' A=A B=B
A
C
B
BB
D
test

A
B
BB

$ cat <<test | awk '$1 ~ "A" || $1 ~ "B"'
A
C
B
BB
D
test

A
B
BB

# 7  
Old 01-20-2014
Hello,

with strict match:

Code:
awk -vs1="A" -vs2="B" '$3==s1 || $3==s2 {print}' file_name

with non-strict match:

Code:
awk -vs1="A" -vs2="B" '$3~s1 || $3~s2 {print}' file_name

Thanks,
R. Singh
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