awk if statement not printing entire field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk if statement not printing entire field
# 1  
Old 06-29-2012
awk if statement not printing entire field

I have an input that looks like this:


Code:
chr1    mm9_knownGene   utr3    3204563 3206102 0       -       .       gene_id "Xkr4"; transcript_id "uc007aeu.1";
chr1    mm9_knownGene   utr3    4280927 4283061 0       -       .       gene_id "Rp1"; transcript_id "uc007aew.1";
chr1    mm9_knownGene   utr3    4333588 4334680 0       -       .       gene_id "Rp1"; transcript_id "uc007aex.2";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007aey.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007aez.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afa.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afb.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afc.1";
chr1    mm9_knownGene   utr3    4763279 4766544 0       -       .       gene_id "Mrpl15"; transcript_id "uc007aff.2";
chr1    mm9_knownGene   utr3    4763279 4764532 0       -       .       gene_id "Mrpl15"; transcript_id "uc007afd.2";

I am changing columns 4 and 5 with this awk line:

Code:
awk '{FS=OFS="\t"} {if($5-$4<=200) print $0; else if($5-$4>200) print $1,$2,$3,$4,$4+200,$6,$7,$8,$9}'

Which gives this output:

Code:
chr1    mm9_knownGene   utr3    3204563 3204763 0       -       .       gene_id
chr1    mm9_knownGene   utr3    4280927 4281127 0       -       .       gene_id "Rp1"; transcript_id "uc007aew.1";
chr1    mm9_knownGene   utr3    4333588 4333788 0       -       .       gene_id "Rp1"; transcript_id "uc007aex.2";
chr1    mm9_knownGene   utr3    4481009 4481209 0       -       .       gene_id "Sox17"; transcript_id "uc007aey.1";
chr1    mm9_knownGene   utr3    4481009 4481209 0       -       .       gene_id "Sox17"; transcript_id "uc007aez.1";
chr1    mm9_knownGene   utr3    4481009 4481209 0       -       .       gene_id "Sox17"; transcript_id "uc007afa.1";
chr1    mm9_knownGene   utr3    4481009 4481209 0       -       .       gene_id "Sox17"; transcript_id "uc007afb.1";
chr1    mm9_knownGene   utr3    4481009 4481209 0       -       .       gene_id "Sox17"; transcript_id "uc007afc.1";
chr1    mm9_knownGene   utr3    4763279 4763479 0       -       .       gene_id "Mrpl15"; transcript_id "uc007aff.2";
chr1    mm9_knownGene   utr3    4763279 4763479 0       -       .       gene_id "Mrpl15"; transcript_id "uc007afd.2";

It handles columns 4 and 5 fine, but truncates column 9 for only the first line. If I use this awk line, the output is fine:

Code:
awk '{FS=OFS="\t"} {print $0}'

However, this awk line duplicates the column 9 truncation error:

Code:
awk '{FS=OFS="\t"} {print $1,$2,$3,$4,$5,$6,$7,$8,$9}'

I can add more columns and get more of that first line, which indicates it is treating the first line differently than the rest. I have manually edited the input file to ensure column 9 of the first line does not have any tabs. I have also moved the first line to the end of the file and the new first line shows the same truncation.
Any suggestions? What am I doing wrong here?
# 2  
Old 06-29-2012
I tried on an AIX box and it doesnt get truncated:
Code:
n12:/sm/bin/wks $ resize
COLUMNS=142;
LINES=32;
export COLUMNS LINES;
n12:/sm/bin/wks $ cat testfile001 |awk '{FS=OFS="\t"} {if($5-$4<=200) print $0; \
else if($5-$4>200) print $1,$2,$3,$4,$4+200,$6,$7,$8,$9}'
chr1    mm9_knownGene   utr3    3204563 3206102 0       -       .       gene_id "Xkr4"; transcript_id "uc007aeu.1";
chr1    mm9_knownGene   utr3    4280927 4283061 0       -       .       gene_id "Rp1"; transcript_id "uc007aew.1";
chr1    mm9_knownGene   utr3    4333588 4334680 0       -       .       gene_id "Rp1"; transcript_id "uc007aex.2";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007aey.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007aez.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afa.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afb.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afc.1";
chr1    mm9_knownGene   utr3    4763279 4766544 0       -       .       gene_id "Mrpl15"; transcript_id "uc007aff.2";
chr1    mm9_knownGene   utr3    4763279 4764532 0       -       .       gene_id "Mrpl15"; transcript_id "uc007afd.2";

# 3  
Old 06-29-2012
I think will solve your query. Its self explanatory.


Code:
 awk '{FS=" "} {if($5-$4<=200) print $0; else if($5-$4>200) print $1,$2,$3,$4,$4+200,$6,$7,$8,$9,$10,$11,$12}' test.txt

# 4  
Old 06-29-2012
Quote:
Originally Posted by mayursingru
I think will solve your query. Its self explanatory.


Code:
 awk '{FS=" "} {if($5-$4<=200) print $0; else if($5-$4>200) print $1,$2,$3,$4,$4+200,$6,$7,$8,$9,$10,$11,$12}' test.txt

I apologize, I should have been more clear. This is what I meant when I said "I can add more columns and get more of that first line" in my original post. I'm more concerned about why the first line is being treated differently than all the others.

---------- Post updated at 12:05 PM ---------- Previous update was at 12:02 PM ----------

Quote:
Originally Posted by vbe
I tried on an AIX box and it doesnt get truncated:
Code:
n12:/sm/bin/wks $ resize
COLUMNS=142;
LINES=32;
export COLUMNS LINES;
n12:/sm/bin/wks $ cat testfile001 |awk '{FS=OFS="\t"} {if($5-$4<=200) print $0; \
else if($5-$4>200) print $1,$2,$3,$4,$4+200,$6,$7,$8,$9}'
chr1    mm9_knownGene   utr3    3204563 3206102 0       -       .       gene_id "Xkr4"; transcript_id "uc007aeu.1";
chr1    mm9_knownGene   utr3    4280927 4283061 0       -       .       gene_id "Rp1"; transcript_id "uc007aew.1";
chr1    mm9_knownGene   utr3    4333588 4334680 0       -       .       gene_id "Rp1"; transcript_id "uc007aex.2";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007aey.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007aez.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afa.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afb.1";
chr1    mm9_knownGene   utr3    4481009 4481796 0       -       .       gene_id "Sox17"; transcript_id "uc007afc.1";
chr1    mm9_knownGene   utr3    4763279 4766544 0       -       .       gene_id "Mrpl15"; transcript_id "uc007aff.2";
chr1    mm9_knownGene   utr3    4763279 4764532 0       -       .       gene_id "Mrpl15"; transcript_id "uc007afd.2";

Thanks for letting me know. I'm using an LSF cluster running RHEL5.3. Now I have a bit more to go to my admins with.
# 5  
Old 06-29-2012
The problem is quite simple. You need to change your awk program to the following one:

Code:
awk 'BEGIN{FS=OFS="\t"} {if($5-$4<=200) print $0; else if($5-$4>200) print $1,$2,$3,$4,$4+200,$6,$7,$8,$9}' inputfile

Without the BEGIN pattern, the first line is read with the default FS and not YOUR FS. After the first line, your FS kicks in.
This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 06-30-2012
Quote:
Originally Posted by elixir_sinari
The problem is quite simple. You need to change your awk program to the following one:

Code:
awk 'BEGIN{FS=OFS="\t"} {if($5-$4<=200) print $0; else if($5-$4>200) print $1,$2,$3,$4,$4+200,$6,$7,$8,$9}' inputfile

Without the BEGIN pattern, the first line is read with the default FS and not YOUR FS. After the first line, your FS kicks in.
Thank you! I'm glad it was something so simple.
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: count unique elements in a field and sum their occurence across the entire file

Hi, Sure it's an easy one, but it drives me insane. input ("|" separated): 1|A,B,C,A 2|A,D,D 3|A,B,B I would like to count the occurence of each capital letters in $2 across the entire file, knowing that duplicates in each record count as 1. I am trying to get this output... (5 Replies)
Discussion started by: beca123456
5 Replies

2. UNIX for Beginners Questions & Answers

Continued trouble matching fields in different files and selective field printing ([g]awk)

I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in awk and would very much appreciate some help. I have data as follows: File1 PS012,002 PRQ 0 1 1 17 1 0 -1 3 2 1 2 -1 ... (7 Replies)
Discussion started by: jvoot
7 Replies

3. UNIX for Dummies Questions & Answers

Using awk to find max and printing entire line

Hi folks, I am very new to awk. I have what is probably a very simple question. I'm trying to get the max value of column 1, but also print column 2. My data looks like this: 0.044|2000-02-03 14:00:00 5.23|2000-02-03 05:45:00 5.26|2000-02-03 11:15:00 0|2000-02-01 18:30:00 So in this case... (2 Replies)
Discussion started by: amandarobe
2 Replies

4. Shell Programming and Scripting

awk - printing nth field based on parameter

I have a need to print nth field based on the parameter passed. Suppose I have 3 fields in a file, passing 1 to the function should print 1st field and so on. I have attempted below function but this throws an error due to incorrect awk syntax. function calcmaxlen { FIELDMAXLEN=0 ... (5 Replies)
Discussion started by: krishmaths
5 Replies

5. Shell Programming and Scripting

Printing entire field, if at least one row is matching by AWK

Dear all, I have been trying to print an entire field, if the first line of the field is matching. For example, my input looks something like this. aaa ddd zzz 123 987 126 24 0.650 985 354 9864 0.32 0.333 4324 000 I am looking for a pattern,... (5 Replies)
Discussion started by: Chulamakuri
5 Replies

6. Shell Programming and Scripting

awk is Printing folders with only numbers as expected. But can't explain 'total' statement.

I am trying to get folder names that contain only numbers. Can someone explain why following command is printing 'total 450' as part of output.. $> ls -lt | awk '$9 ~ /^*$/' | more total 450 drwxr-x--x 3 user1 group1 512 Mar 9 2008 329227163 drwxr-x--x 3 user1 group1 ... (17 Replies)
Discussion started by: kchinnam
17 Replies

7. UNIX for Advanced & Expert Users

Printing Field with Delimiter in AWK/cut

Hello, I had posted earlier about printing fields using AWK, but now I have a slightly different problem. I have text files in the format: 1*2,3,4,5 and wish to print the first, third, and fifth fields, including the asterisk and commas. In other words, after filtering it should look... (1 Reply)
Discussion started by: Jahn
1 Replies

8. UNIX for Dummies Questions & Answers

grep entire statement not just line

(extract from SQL binlog file...) # at 4960 #080801 14:35:31 server id 4 end_log_pos 195 Query thread_id=63121426 exec_time=0 error_code=0 use d_jds; SET TIMESTAMP=1217581531; UPDATE bid_details set bidding = 3170.37 ,deduction=if((3170.37 < 37.43),0,deduction) where... (3 Replies)
Discussion started by: shantanuo
3 Replies

9. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies

10. Shell Programming and Scripting

Printing the invert of the last field of awk

in csh set x = "/home/usr/dir1/file1" if i do: echo $x | awk -F\/ '{print $NF}' will result to: "file1" how do i invert the output to: "/home/usr/dir1" :confused: (2 Replies)
Discussion started by: jehrome_rando
2 Replies
Login or Register to Ask a Question