how to delete a matching line from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to delete a matching line from a file
# 1  
Old 12-17-2008
how to delete a matching line from a file

I have file A with 10 lines and file B with 15 lines

I have to read each line from file A and match it with lines in file B

If matching lines are found i need to delete it from file B

The ouput will be the deleted file B

I tried the following in the loop but the deleting part is not working

while read fileA

do
sed -e "/$fileA/d" fileB > new deleted file
OR
cat grep -Ev '$fileA' fileB > new deleted file


done

The problem here is i need to put the "new deleted file" as input in my sed or grep command...or how to do it recursively

Smilie
# 2  
Old 12-17-2008
A simple
Code:
grep -vf fileA fileB

might do the job. You don't need the while statement I guess.
# 3  
Old 12-17-2008
This is somehow not fulfilling my requirement

Since i am deriving the matching pattern from file A i need a loop

Thanks
# 4  
Old 12-17-2008
You might want to post here your sample input files and your expected output file.
# 5  
Old 12-17-2008
file A is like this

/path1/source/web/service,25,RRR,1.0
/path2/source/web/service,25,RRR,1.0
/path3/source/web/service,25,RRR,1.0
/path4/source/web/service,25,RRR,1.0

file B is like this

/path1/source/web/service 2.0
/path2/source/web/service 3.0
/path3/source/web/service 4.0
/path4/source/web/service 3.0
/path5/source/web/service 4.4

i am matching with the first column in file A to file B

eg /path1/source/web/service.....

so that my output is

/path5/source/web/service which doent match....
# 6  
Old 12-17-2008
What I have in mind is a nasty solution..

1. extract the 1st columns from both files using cut or awk the redirect the output to a temp file.
2. use the suggested grep command above.

Code:
yongitz@wolfgang:/tmp$ cat a.txt
/path1/source/web/service,25,RRR,1.0
/path2/source/web/service,25,RRR,1.0
/path3/source/web/service,25,RRR,1.0
/path4/source/web/service,25,RRR,1.0

yongitz@wolfgang:/tmp$ cat b.txt
/path1/source/web/service 2.0
/path2/source/web/service 3.0
/path3/source/web/service 4.0
/path4/source/web/service 3.0
/path5/source/web/service 4.4

yongitz@wolfgang:/tmp$ cut -d, -f1 a.txt > x.txt
yongitz@wolfgang:/tmp$ cut -d" " -f1 b.txt > y.txt

yongitz@wolfgang:/tmp$ grep -vf x.txt y.txt
/path5/source/web/service

This is a nasty solution and I guess awk can compare certain columns of two files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all lines from file matching a string

I wish to search and delete all lines in /app/Jenkins/deploy.txt having this filename string /app/Jenkins/file2.mrt as entry: I'm using : colon as delimiter in sed command as I'm dealing with file paths. Below is the command I was expecting to work. sed -i ":/app/Jenkins/file2.mrt:d"... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

3. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

4. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

5. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

6. Shell Programming and Scripting

delete lines in file matching a pattern

I have a text file, a sample of which is as follows: r/- * 0: WINDOWS/Microsoft.NET/Framework/v2.0.50727/ASP.NETWebAdminFiles/Images/headerGRADIENT_Tall.gif r/- * 0: WINDOWS/SoftwareDistribution/Download/cf8ec753e88561d2ddb53e183dc05c3e/backoff.jpg r/- * 0: ... (2 Replies)
Discussion started by: stumpyuk
2 Replies

7. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. UNIX for Dummies Questions & Answers

How can you delete records in a file matching a pattern?

I am curious if the following can be done in a file in unix. Let's say I have a flat file with the following data AAA,12,2,,,, BBB,3,1,,,, CCC,,,,, DDD,2,,,,, SQQ,,,,, ASJ,,3,5 I only want to capture the data with values into a new file. If the data contains the pattern ,,,,, as in... (2 Replies)
Discussion started by: mode09
2 Replies

10. UNIX for Dummies Questions & Answers

how to delete line with matching text and line immediately after

hello experts, I have a file: File1 Sample Test1 This is a Test Sample Test2 Another Test Final Test3 A final Test I can use sed to delete the line with specific text ie: sed '/Test2/d' File1.txt > File2.txt How can I delete the line with the matching text and the line immediately... (6 Replies)
Discussion started by: orahi001
6 Replies
Login or Register to Ask a Question