awk to output lines less than number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to output lines less than number
# 1  
Old 07-30-2015
awk to output lines less than number

I am trying to output all lines in a file where $7 is less than 30. The below code does create a result file, but with all lines in the original file. The original file is tab deliminated is that the problem? Thank you Smilie.

Code:
 awk 'BEGIN{FS=OFS=","} $7 < 30 {print}' file.txt > result.txt

file.txt
Code:
chr1    40539722    40539865    chr1:40539722-40539865    PPT1    1    159
chr1    40539722    40539865    chr1:40539722-40539865    PPT1    2    161
chr1    40539722    40539865    chr1:40539722-40539865    PPT1    3    161
chr1    40539722    40539865    chr1:40539722-40539865    PPT1    3    28

Desired result.txt
Code:
 chr1    40539722    40539865    chr1:40539722-40539865    PPT1    3    28

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

It was the FS=OFS="'," .... should be OFS="/t" , guess I need to pay more attention. FS is Field seperator and OFS is Output Field Seperator, right? Thank you Smilie.

Last edited by cmccabe; 07-30-2015 at 02:14 PM.. Reason: added desired result
# 2  
Old 07-30-2015
Yes, if input file is tab separated, then you have to set FS - input field separator to \t not OFS - output field separator.
Code:
awk -F'\t' '$7+0 < 30' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-30-2015
You write "The original file is tab deliminated" but set FS to ",". That doesn't fly. As <TAB> is one of the default field separators,
Code:
awk ' $7 < 30' file
chr1    40539722    40539865    chr1:40539722-40539865    PPT1    3    28

will do.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 07-30-2015
Thank you Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to output non-number lines with grep?

I want to check my data quality. I want to output the lines with non-number. I used the grep command: grep '' myfile.csv Since my file is csv file, I don't want to output the lines with comma. And I also don't want to output "." or space. But I still get the lines like the following:... (8 Replies)
Discussion started by: twotwo
8 Replies

2. Shell Programming and Scripting

awk - Skip x Number of Lines in Counter

Hello, I am new to AWK and in UNIX in general. I am hoping you can help me out here. Here is my data: root@ubuntu:~# cat circuits.list WORD1 AA BB CC DD Active ISP1 ISP NAME1 XX-XXXXXX1 WORD1 AA BB CC (9 Replies)
Discussion started by: tattoostreet
9 Replies

3. Shell Programming and Scripting

Getting awk Output as number instead of String

Hi All, I have a file a.txt, content as mentioned below: 22454750 This data in this control file and I have a variable called vCount which contains a number. I need to extract the 22454750 from the above file and compare with the variable vCount. If match fine or else exit. ... (5 Replies)
Discussion started by: Arun Mishra
5 Replies

4. Shell Programming and Scripting

awk number output

Hi, I have a problem when doing calculations in awk. I want to add up a few numbers and output the result. testfile: 48844322.87 7500.00 10577415.87 3601951.41 586877.64 1947813.89 $ awk '{x=x+$1};END{print x}' testfile 6.55659e+07The problem is the number format. It should show... (3 Replies)
Discussion started by: Subbeh
3 Replies

5. Shell Programming and Scripting

Awk number of lines

How do I get the last NR of a csv file? If I use the line awk -F, '{print NR}' csvfile.csv and there are 42 lines, I get: ... 39 40 41 42 How do I extract the last number, which in this case is 42? ---------- Post updated at 11:05 AM ---------- Previous update was at 10:57 AM... (1 Reply)
Discussion started by: locoroco
1 Replies

6. Shell Programming and Scripting

How to (n)awk lines of CSV with certain number of fields?

I have a CSV file with a variable number of fields per record. How do I print lines of a certain number of fields only? Several permutations of the following (including the use of escape characters) have failed to retrieve the line I'm after (1,2,3,4)... $ cat myfile 1,2,3,4 1,2,3 $ # Print... (1 Reply)
Discussion started by: cs03dmj
1 Replies

7. Shell Programming and Scripting

How do I print out lines with the same number in front using awk?

Hi, I need help in printing out the dates with the largest value in front of it using awk. 436 28/Feb/2008 436 27/Feb/2008 436 20/Feb/2008 422 13/Feb/2008 420 23/Feb/2008 409 21/Feb/2008 402 26/Feb/2008 381 22/Feb/2008 374 24/Feb/2008 360... (7 Replies)
Discussion started by: SIFA
7 Replies

8. Shell Programming and Scripting

awk, ignore first x number of lines.

Is there a way to tell awk to ignore the first 11 lines of a file?? example, I have a csv file with all the heading information in the first lines. I want to split the file into 5-6 different files but I want to retain the the first 11 lines of the file. As it is now I run this command: ... (8 Replies)
Discussion started by: trey85stang
8 Replies

9. Shell Programming and Scripting

awk - Counting number of similar lines

Hi All I have the input file OMAK_11. OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000002EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE 1341 OMAK 000003EXCLUDE ... (8 Replies)
Discussion started by: dhanamurthy
8 Replies

10. Shell Programming and Scripting

How to print number of lines with awk ?

Can some body tell me how to print number of line from a particular file, with sed. ? Input file format AAAA BBBB CCCC SDFFF DDDD DDDD Command to print line 2 and 3 ? BBBB CCCC And also please tell me how to assign column sum to variable. I user the following command it... (1 Reply)
Discussion started by: maheshsri
1 Replies
Login or Register to Ask a Question