awk find and delete line from list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk find and delete line from list
# 1  
Old 11-21-2017
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:
Code:
4558980
5525628
3595233
2650083
2219411
3529741
4675897
3070869
0014685
6365902

file.txt:
Code:
one-two-three-4558980.txt
one-two-three-5525628.txt
one-two-three-3595233.txt
one-two-three-2650083.txt
one-two-three-2219411.txt
one-two-three-3529741.txt
one-two-three-4675897.txt
one-two-three-3070869.txt
one-two-three-0014685.txt
one-two-three-6365902.txt

this works but i would like pattern to be each line of input.txt i would like to put it in a bash file

Code:
#!/bin/bash
#
#

awk '!/pattern/' file.txt > temp && mv temp file


thank you in advance

Last edited by Don Cragun; 11-21-2017 at 09:35 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 11-21-2017
Why is awk a requirement here? Why not just use:
Code:
grep -f input.txt -v file.txt

# 3  
Old 11-21-2017
thanks Don Cragun;
i was just coming back to fix that


---------- Post updated at 08:56 PM ---------- Previous update was at 08:44 PM ----------

sorry Don Cragun
that did not work , it ran in bash with out any errors but did not change
file.txt

---------- Post updated at 09:06 PM ---------- Previous update was at 08:56 PM ----------

sorry Don Cragun
that did not work , it ran in bash without any errors but did not change
file.txt
# 4  
Old 11-21-2017
Quote:
Originally Posted by klein
thanks Don Cragun;
i was just coming back to fix that


---------- Post updated at 08:56 PM ---------- Previous update was at 08:44 PM ----------

sorry Don Cragun
that did not work , it ran in bash with out any errors but did not change
file.txt
I was just talking about the awk part. You still have to capture the output produced and copy it back to the target file. I just didn't understand why part of your code was using file and part of it was using file.txt.

You could either use:
Code:
grep -f input.txt -v file.txt > file

if you want what your script was doing, or you could use:
Code:
grep -f input.txt -v file.txt > file && mv file file.txt

if you wanted to replace file.txt with its original contents minus the deleted lines.

Of course, having an example where all of your input is removed by the operation means that you could get the same results just using the command:
Code:
> file.txt

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 11-21-2017
Don
both lines of code produce the fie but it is blank. i deleted a couple #'s in the input.txt file so that the file should have lines in it and just not blank .

Code:
grep -f input.txt -v file.txt > file

produces a blank file called file and

Code:
grep -f input.txt -v file.txt > file && mv file file.txt

produces a blank file called file it doesn't mv file to file.txt
that is a little confusing to me , it should .
ubuntu mate 16.04 64bit
# 6  
Old 11-22-2017
When I copy and paste your samples into a file it works fine. When I remove one of the lines of input.txt, the corresponding line in file.txt gets printed. Could you try that yourself?

Also, make sure there are no empty lines in input.txt
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-22-2017
Scrutinizer and Don Cragun i'm not sure what i did but after rebooting i tried it again and it did work. thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to find a string in a line in UNIX file and delete that line and previous 3 lines ?

Hi , i have a file with data as below.This is same file. But actual file contains to many rows. i want to search for a string "Field 039 00" and delete that line and previous 3 lines in that file.. Can some body suggested me how can i do using either sed or awk command ? Field 004... (7 Replies)
Discussion started by: vadlamudy
7 Replies

2. Shell Programming and Scripting

awk delete line if $5 contains string from list

(5 Replies)
Discussion started by: chrisjorg
5 Replies

3. Shell Programming and Scripting

Find specific line and delete line after.

I'm looking for a way to search a file, in this case init.rc for a specific match, service adbd /sbin/adbd, and delete the a specific line after it Original: # adbd is controlled by the persist.service.adb.enable system property service adbd /sbin/adbd disabled After deletion: #... (5 Replies)
Discussion started by: GabrialDestruir
5 Replies

4. Shell Programming and Scripting

Find and delete rest of the line

i need to find ' - ' in a line , if i found i need to rest of the line including '-' example libmm-2.0 libytytsunos-5.6 output libmm libytytsunos (7 Replies)
Discussion started by: girija
7 Replies

5. UNIX for Dummies Questions & Answers

Find Pattern delete line and next line

I am trying to delete the line with pattern and the next line. Found the below command in forum which also deleted the previous line. how should i modify that to make sure that only the line with pattern and the next line are deleted but not the previous line? awk '/key... (1 Reply)
Discussion started by: rdhanek
1 Replies

6. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

7. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies

8. UNIX for Advanced & Expert Users

Find and delete the line

Hi I have a text file like this name today.txt the request has been accepted the scan is successful at following time there are no invalid packages 5169378 : map : Permission Denied the request has been accepted Now what i want do is I want to search the today.txt file and if i... (8 Replies)
Discussion started by: gsusarla
8 Replies

9. Shell Programming and Scripting

find a line and delete it.

Hi All, I would like to fine some entry in a file, if it found, it should remove the same in the same file. Please some one help me. need to find and en entry 'bea' from hosts file. If it found it should write it same host file. but i am not geting it. If i am writing it in new file... (2 Replies)
Discussion started by: bullz26
2 Replies

10. UNIX for Dummies Questions & Answers

Find a word and delete the line

Hi I have a text file like this name today.txt the request has been accepted the scan is successful at following time there are no invalid packages 5169378 : map : Permission Denied the request has been accepted Now what i want do is I want to search the today.txt file and if i... (1 Reply)
Discussion started by: gsusarla
1 Replies
Login or Register to Ask a Question