Awk to print on condition in input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk to print on condition in input file
# 1  
Old 05-15-2009
Awk to print on condition in input file

I need only those records which has $2 equal to "DEF" independent of case (i.e upper or lower)

Code:
nawk -F"," '$2 ~ /[Dd][Ee][Ff]/{print $0}' file

This returns 3rd record also which i dont want

I tried this but this doesnt work as expected.
Code:
nawk -F"," '$2 == "[Dd][Ee][Ff]"{print $0}' file

Quote:
file:
abc,def
abc,DEf
abc,kDEF
abc,dEF
abc,dEf

i dont need the following record.
abc,kDEF
Help is appreciated.
# 2  
Old 05-15-2009
Code:
nawk -F"," ' tolower($2) == "def" ' file

# 3  
Old 05-15-2009
the easiest thing should be grep, if it's an option for you

Code:
 grep -i ,def file

or

Code:
 grep -i ",def$" file1

just to make sure
# 4  
Old 05-15-2009
Code:
awk -F, '$2 ~ /^[dD][eE][fF]$/'

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 to add +1 to value based on condition in input

In the awk below I am trying to add a | that will adjust $2 in the ouput by adding +1 if the original value from file that was used in $3 had a - in it. Line 3 of file is an example of this. In my current awk I just subtract one but I am not sure how to only apply this to those values without a -.... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Beginners Questions & Answers

awk - print when condition is met

I have a file.txt containing the following: Query= HWI-ST863:386:C5Y8UACXX:3:2302:16454:89688 1:N:0:ACACGAAT Length=100 Score E Sequences producing significant alignments: (Bits) Value ... (2 Replies)
Discussion started by: tons92
2 Replies

3. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

4. Shell Programming and Scripting

awk if condition match and fix print decimal place

Hi All, I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6. See the sample : before: Sort Total Site Sort SortName Parts ... (3 Replies)
Discussion started by: horsepower
3 Replies

5. Shell Programming and Scripting

awk if condition match and print all

Hi, I am trying to do something like this ... I use awk to match a pattern, and then print out all col. My code is : awk '{if ($1 ==300) print $1,$2-'$sbin7',$3}' tmp.txt output= 300 2 whereby sbin7=2, The thing is, I want to print all col and row, not just the matched line/row only, but... (10 Replies)
Discussion started by: horsepower
10 Replies

6. Shell Programming and Scripting

awk question: How to print condition of NR & NF together.

Experts: LINE1 :This is line one The FIRST line of the file. LINE2 :This is line two LINE3 :This is line three with 8 fileds LINE4 :This is line four LINE5 :This is line five LINE6 :This is line six with 8 fileds I want to delete line 1, and then process the file and want to print lines... (2 Replies)
Discussion started by: rveri
2 Replies

7. Shell Programming and Scripting

[AWK script]Counting the character in record and print them in condition

.......... (1 Reply)
Discussion started by: Antonlee
1 Replies

8. Shell Programming and Scripting

Move input file based on condition

Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi (2 Replies)
Discussion started by: AngelMady
2 Replies

9. Shell Programming and Scripting

awk to print lines based on string match on another line and condition

Hi folks, I have a text file that I need to parse, and I cant figure it out. The source is a report breaking down softwares from various companies with some basic info about them (see source snippet below). Ultimately what I want is an excel sheet with only Adobe and Microsoft software name and... (5 Replies)
Discussion started by: rowie718
5 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