Using awk to delete a certain line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using awk to delete a certain line
# 8  
Old 09-27-2012
Try this then (could not test my previous code 'cause I was on my smartphone)
Code:
awk '!($0~x)' x="$3" file

This User Gave Thanks to tukuyomi For This Post:
# 9  
Old 09-27-2012
Quote:
Originally Posted by tukuyomi
Try this then (could not test my previous code 'cause I was on my smartphone)
Code:
awk '!($0~x)' x="$3" file

this worked half way i think when i ran my script it displayed this on my screen
Code:
1 2 3
7 8 9

but my file still contains all three
# 10  
Old 09-27-2012
Quote:
Originally Posted by austing5
just sends the the contents of the file to my screen. still contains the line i want deleted Smilie
see this..

Code:
$ cat file
1 2 3
4 5 6
7 8 9

$ p=5

$ awk  -v x="$p"  '$0 !~ x' file
1 2 3
7 8 9

$ awk  -v x="$p"  '$2 != x' file
1 2 3
7 8 9

# 11  
Old 09-27-2012
Code:
x=5
perl -ni_backup -e 'next if /\b'"$x"'\b/;print' num.txt

Please note that sed and awk are filters; they will read from the input source (standard input or files supplied as arguments) and throw back whatever you asked to output to standard output. They will not modify the input source.

Last edited by elixir_sinari; 09-27-2012 at 03:17 AM..
# 12  
Old 09-27-2012
Quote:
Originally Posted by pamu
see this..

Code:
$ cat file
1 2 3
4 5 6
7 8 9

$ p=5

$ awk  -v x="$p"  '$0 !~ x' file
1 2 3
7 8 9

$ awk  -v x="$p"  '$2 != x' file
1 2 3
7 8 9

yeah it displays it but my file still contains all three lines for some reason
# 13  
Old 09-27-2012
check your $3 is getting assigned correctly or not..
just do echo $3 and check..

And it wont change your original file content...

for changing original file.. do .
Code:
awk  -v x="$3"  '$0 !~ x' file > temp_file
mv temp_file file

This User Gave Thanks to pamu For This Post:
# 14  
Old 09-27-2012
Quote:
Originally Posted by pamu
check your $3 is getting assigned correctly or not..
just do echo $3 and check..

And it wont change your original file content...

for changing original file.. do .
Code:
awk  -v x="$3"  '$0 !~ x' file > temp_file
mv temp_file file

woot swoop that worked i was just trying file >> file to see if it would over right but didn't work, shooting it to a temp worked perfect!!! thank you so much!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk find and delete line from list

good day i have a list of numbers in input.txt, i would like to compare to file.txt and delete the line that number appears in file.txt . input.txt: 4558980 5525628 3595233 2650083 2219411 3529741 4675897 3070869 0014685 6365902 file.txt: one-two-three-4558980.txt... (7 Replies)
Discussion started by: klein
7 Replies

2. Shell Programming and Scripting

If the value > 100 delete the line with awk or sed

if the value > 100 delete the line delay time: 42.978 ms delay time: 43.684 ms delay time: 41.082 ms delay time: 44.845 ms delay time: 40000.057 ms delay time: 41.158 ms delay time: 42.239 ms delay time: 50.579 ms delay time: 46.334 ms to (7 Replies)
Discussion started by: yanglei_fage
7 Replies

3. Shell Programming and Scripting

awk delete line if $5 contains string from list

(5 Replies)
Discussion started by: chrisjorg
5 Replies

4. UNIX for Dummies Questions & Answers

Need an awk command to delete a line

Hi all, As of now am using an awk command to check the number of columns in a file that has 10 lakh rows. Is it possible to remove that particular line having an extra column and copy the remaining lines to a new file ? YOUR HELP IS HIGHLY APPRECIATED. THANKS IN ADVANCE (5 Replies)
Discussion started by: Rahul619
5 Replies

5. UNIX for Dummies Questions & Answers

Awk match on columns and delete line

Hi, I have a file like this a 1 2 b 2 2 c 2 3 d 4 5 f 5 6 output a 1 2 c 2 3 d 4 5 f 5 6 Basically, I want to delete the whole line if $2 and $3 are the same. Thanks (5 Replies)
Discussion started by: jacobs.smith
5 Replies

6. Shell Programming and Scripting

Delete specific line in awk

Hi, I'm totally new to awk, so thanks in advance for your help. I want to make a csv file out of a text file I've exported from a database I want to do two things: 1. Change pipes ("|") to commas (",") to make a text file into CSV 2. Remove the second line of the file, which is useless junk. ... (3 Replies)
Discussion started by: pip56789
3 Replies

7. Shell Programming and Scripting

delete more than one line awk or perl

Hi all! How can a file be rid of three lines in sequence like the sample below: ... </s> <s> <w></w> </s> <s> ...to get: ... </s> <s> ... Note that the digits between square brackets may be more than one, comprising a comma, or a full-stop; and that the string between brackets... (1 Reply)
Discussion started by: mjomba
1 Replies

8. Shell Programming and Scripting

Delete line with sed or awk (with specified condition)

Hello. I'm trying to delete the lines of a file does not contain the letter "T " (for example) at position 26. So far, I could only print the result: awk '{if (substr ($ 1,1,26)! ~ / T /) print}' file.txt How I can do to eliminate the lines that meet this condition? Help please. ... (4 Replies)
Discussion started by: </kida>
4 Replies

9. Shell Programming and Scripting

how to delete line with awk

Hi I have a multi -line file which is sorted by the 1-st colomn in the following format: 400 0000 0001 1000 1010 0111 0000 1000 0000 402 1101 0000 1100 1010 0111 1000 1000 0000 403 1001 0000 1100 1010 0111 0000 1000 0000 495 1000 0000 1100 1010 0111 0000 1000 0000 500 0000 0001 1000 0010... (2 Replies)
Discussion started by: aoussenko
2 Replies

10. Shell Programming and Scripting

awk : delete ^M at the end of the line

Hi all, I'm newbi in scripting. could someone tell how to delete the ^M at the end of the linie with an awk command. many thanks in advance. (2 Replies)
Discussion started by: liliput
2 Replies
Login or Register to Ask a Question