awk Question: How to remove lines in which $3 == $1 +4


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Question: How to remove lines in which $3 == $1 +4
# 1  
Old 02-11-2014
Scissors awk Question: How to remove lines in which $3 == $1 +4

Hi all,

I am trying to delete all lines from a file in which the value in 'column 3' is not the value of 'column 1' + 4. The code below that I tried doesn't work.


Code:
awk '$3 == $1 + 4 {print}' input > output

Example Input:-
Code:
1 xxx 2
3 xxx 26
4 xxx 8
2 xxx 9
7 xxx 11

(input file actually has 8 columns, this is just what the first 3 columns look like)

Example desired output:-
Code:
4 xxx 8
7 xxx 11

Any help would be very gratefully received!
# 2  
Old 02-11-2014
You were very close. Try:
Code:
awk '$3 == ($1 + 4) {print}' input > output

or, more simply:
Code:
awk '$3 == ($1 + 4)' input > output

# 3  
Old 02-11-2014
Wrench

Hi,

Thank you very much for your reply. I think I also tried with the brackets you suggest but it didn't work. (I have left my office now so I cant check until tomorrow morning, I'm on UK time.)

Do you know of any reason why it might not work and any other suggestions?

Many thanks,
Liv
# 4  
Old 02-11-2014
Note that I suggested parentheses, not brackets. But my suggestion and your original code should produce the same results with a version of awk that conforms to the standards.

What OS are you using?

You said the awk script wasn't working, but you didn't show us what output you were getting. If you would show us the output, it would make it a lot easier to evaluate what might be wrong.

If you're using a Solaris/SunOS system, try using /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of the default /usr/bin/awk. /usr/bin/awk on Solaris systems is an old version of awk that is to be used to run old scripts using a much earlier specification describing how awk should behave.
# 5  
Old 02-11-2014
I apologise for not being more specific.

I am using Linux, bash shell.

I get no error message when implementing, however the output file I get is completely blank, although I know for sure that there are lines in the input file that match the pattern.

I use awk in otherways by just typing "awk" and it works fine.


Many thanks,
Liv
# 6  
Old 02-11-2014
OK. Then we need to see what is actually in a few lines of your input file (including at least one line that should be selected and at least one line that should not be selected. Please also show us the output from the command:
Code:
od -bc input

for your sample input file.
# 7  
Old 02-11-2014
Sure, I'll post again tommorrow morning when I get back to my office. Thanks for your help.

Liv
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Using awk to remove lines from file that match text

I am trying to remove each line in which $2 is FP or RFP. I believe the below will remove one instance but not both. Thank you :). file 12 123 FP 11 10 RFP awk awk -F'\t' ' $2 != "FP"' file desired output 12 11 (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Remove lines from output in files using awk

I have two large files (~250GB) that I am trying to remove the where GT: 0/0 or 1/1 or 2/2 for both files. I was going to use a bash with the below awk, which I think will find each line but how do I remove that line is that condition is found? Thank you :). Input 20 60055 . A ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

[Solved] awk to remove lines

Hi, I have a file with contents. file1: <2013 tttaaa abc123 <2013 gggdddd <2013 sssssss <2013 eeeee I need to remove the lines which do not have the word "tttaaa" can some one help ? (7 Replies)
Discussion started by: giri_luck
7 Replies

5. Shell Programming and Scripting

Remove x lines form top and y lines form bottom using AWK?

How to remove x lines form top and y lines form bottom. This works, but like awk only cat file | head -n-y | awk 'NR>(x-1)' so remove last 3 lines and 5 firstcat file | head -n-3 | awk 'NR>4' (5 Replies)
Discussion started by: Jotne
5 Replies

6. Shell Programming and Scripting

remove duplicate lines using awk

Hi, I came to know that using awk '!x++' removes the duplicate lines. Can anyone please explain the above syntax. I want to understand how the above awk syntax removes the duplicates. Thanks in advance, sudvishw :confused: (7 Replies)
Discussion started by: sudvishw
7 Replies

7. Shell Programming and Scripting

How to remove lines before and after with awk / sed ?

Hi guys, I need to remove the pattern (ID=180), one line before and four lines after. Thanks. (5 Replies)
Discussion started by: ashimada
5 Replies

8. Shell Programming and Scripting

Awk to remove Blank lines in pipedelimited

Awk to remove Blank lines in pipedelimited Help me correct this. nawk -F"|" '{a=$0; gsub(FS, "",a); if(length(a)) print}' file1 nawk -F"|" '{gsub(FS, "",$0);if(length($0)) print}' file1 (4 Replies)
Discussion started by: pinnacle
4 Replies

9. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies

10. Shell Programming and Scripting

Remove all blank lines in shell or awk.

Hi there, I want to trim space between lines in unix. I have a file named abc.txt with 2,00,000 lines.But useful are only a few. Please tell me how to delete the blank lines.:o (1 Reply)
Discussion started by: tushar_tus
1 Replies
Login or Register to Ask a Question