awk if one field has more characters than another


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers awk if one field has more characters than another
# 1  
Old 08-08-2012
awk if one field has more characters than another

Hi all, I've been stuck on how to write a command in awk that looks at two columns and only prints entries where one of the columns has more than or less than a 3 character difference. I need to be able to compare any two columns preferably, but as an example, comparing columns 3 and 4

example input
Code:
1    2    3    4        5
b    c    AA   G        d
b    c    AA   GGG      d
b    c    AA   GGGGGG   d
b    c    AAAA G        d
b    c    A    G        d

desired output
Code:
b    c    AA   GGGGGG   d
b    c    AAAA G        d

Appreciate the help!!!
# 2  
Old 08-08-2012
Code:
$ awk '{for(i=1;i<=NF;i++)if(length($i)>3){print;next}}' input.txt
b    c    AA   GGGGGG   d
b    c    AAAA G        d

# 3  
Old 08-08-2012
Thanks so much. Question: Is the code comparing field 3 and 4? I think this will look at more than just 3 and 4?? I need to be able to only compare two columns at a time, as there will be over 50 columns with full text.
Thanks
# 4  
Old 08-08-2012
try this for ( 3rd and 4th column )

Code:
$ awk 'length($3)>3 || length($4)>3' input.txt
b    c    AA   GGGGGG   d
b    c    AAAA G        d

my first post was to check whether any of the field length is greater than 3
# 5  
Old 08-08-2012
Oh ok. I think what I need the difference between two columns. To provide some context the A's and G's are biological genetic mutation data. One column is the reference, and the other is a tumor sample. I am trying to extract all entries where there is an insertion OR deletion of at least 3base pairs (A's G's C's or T's).

The best way to do this is to look at the reference and tumor column and ask if there is at least a difference of 3 letters.
# 6  
Old 08-08-2012
awk

Hi,

Try this one,
Code:
awk '{l=length($3)-length($4);if(l>=3||l<=-3){print;}}' file

Cheers,
Ranga:-)

Last edited by rangarasan; 08-08-2012 at 02:08 PM..
# 7  
Old 08-08-2012
Thanks for your reply, I will try the code.
Not sure I want the largest with respect to one column, just the ones where there is a magnitude difference of 3.

example
Code:
col3   col4
A      AA       # difference of 1
AAA    A        # difference of 2
A      AAAAA    # difference of 4 PRINT
AAAA   A        # difference of 3 PRINT

And it has to ignore other columns
Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

Using awk to add length of matching characters between field in file

The awk below produces the current output, which will add +1 to $3. However, I am trying to add the length of the matching characters between $5 and $6 to $3. I have tried using sub as a variable to store the length but am not able to do so correctly. I added comments to each line and the... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to update field using matching value in file1 and substring in field in file2

In the awk below I am trying to set/update the value of $14 in file2 in bold, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

How can awk ignore the field delimiter like comma inside a field?

We have a csv file as mentioned below and the requirement is to change the date format in file as mentioned below. Current file (file.csv) ---------------------- empname,date_of_join,dept,date_of_resignation ram,08/09/2015,sales,21/06/2016 "akash,sahu",08/10/2015,IT,21/07/2016 ... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

6. Shell Programming and Scripting

awk to parse field and include the text of 1 pipe in field 4

I am trying to parse the input in awk to include the |gc= in $4 but am not able to. The below is close: awk so far: awk '{sub(/\|]+]++/, ""); print }' input.txt Input chr1 955543 955763 AGRN-6|pr=2|gc=75 0 + chr1 957571 957852 AGRN-7|pr=3|gc=61.2 0 + chr1 970621 ... (7 Replies)
Discussion started by: cmccabe
7 Replies

7. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

10. Shell Programming and Scripting

awk for presence of characters in a field

I have a text file like: "10","server","","11111",JLB, I need a way to say if the 3rd field has data in it, move the contents of the entire line to a new file. Any ideas? Thanks, (5 Replies)
Discussion started by: scriptr2be
5 Replies
Login or Register to Ask a Question