[Solved] awk to remove lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] awk to remove lines
# 1  
Old 03-27-2013
[Solved] awk to remove lines

Hi,

I have a file with contents.

file1:
Code:
<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 ?

Last edited by Scrutinizer; 03-27-2013 at 06:09 PM.. Reason: code tags
# 2  
Old 03-27-2013
Code:
grep tttaaa file > newfile

# 3  
Old 03-27-2013
You should be more precise in your requirements - Can tttaaa occur anywhere in the line, or always in field 2, or always at the end of the line, or...

If it's field 2, you could do:
Code:
awk '$2 ~ /tttaaa/' inputfile.txt

(or $0 or just '/tttaaa/' for anywhere in the line - although for that case grep is probably simpler)
# 4  
Old 03-27-2013
If -i option is supported with sed version, try:
Code:
sed -i '/tttaaa/!d' infile

For exact word match, try:
Code:
sed -i '/\btttaaa\b/!d' infile


Last edited by rdrtx1; 03-27-2013 at 05:21 PM..
# 5  
Old 03-28-2013
Code:
<2013 abc frgv tttaaa
abc123
<2013 bec gggdddd
<2013 cdf sssssss
<2013 frt eeeee

Thanks for the replies. I need one more. And if at i wanted to remove all the lines which matches the word "2013" and do no match "tttaaa" ?

In this case i need the output as

Code:
<2013 abc frgv tttaaa
abc123

Can this be done ?
# 6  
Old 03-28-2013
Code:
awk '/2013/ && /tttaa/ || !/^</' file

This User Gave Thanks to Yoda For This Post:
# 7  
Old 03-28-2013
I think this is the correct logic for "remove lines that match 2013 and do not contain tttaaa". The way the line starts with < is just a coincidence:
Code:
$ sed "/2013/ { /tttaaa/ ! d }" file
<2013 abc frgv tttaaa
abc123

This User Gave Thanks to hanson44 For This Post:
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

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

3. Shell Programming and Scripting

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. awk '$3 == $1 + 4 {print}' input > output Example Input:- 1 xxx 2 3 xxx 26 4 xxx 8 2 xxx 9 7 xxx 11 (input file... (9 Replies)
Discussion started by: livbaddeley
9 Replies

4. UNIX for Dummies Questions & Answers

[Solved] How remove leading whitespace from xml (sed /awk?)

Hi again I have an xml file and want to remove the leading white space as it causes me issues later in my script I see sed is possible but cant seem to get it to work I tried sed 's/^ *//' file.xml output <xn:VsDataContainer id="1U104799" modifier="update"> ... (10 Replies)
Discussion started by: aniquebmx
10 Replies

5. UNIX for Dummies Questions & Answers

[SOLVED] remove lines that have duplicate values in column two

Hi, I've got a file that I'd like to uniquely sort based on column 2 (values in column 2 begin with "comp"). I tried sort -t -nuk2,3 file.txtBut got: sort: multi-character tab `-nuk2,3' "man sort" did not help me out Any pointers? Input: Output: (5 Replies)
Discussion started by: pathunkathunk
5 Replies

6. Shell Programming and Scripting

[Solved] awk calculating between lines

Hey guys, maybe you can help me with this... I want to read input.dat line by line, while doing a simple calculation between the second column value of the current line and the second column value of the next line (like a difference). input is something like this: 0 3.945757 1 ... (1 Reply)
Discussion started by: origamisven
1 Replies

7. Emergency UNIX and Linux Support

[Solved] AWK to parse adjacent matching lines

Hi, I have an input file like F : 0.1 : 0.002 P : 0.3 : 0.004 P : 0.5 : 0.008 P : 0.1 : 0.005 L : 0.05 : 0.02 P: 0.1 : 0.006 P : 0.01 : 0.08 F : 0.02 : 0.08 Expected output: (2 Replies)
Discussion started by: vasanth.vadalur
2 Replies

8. 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

9. 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

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