Issue deleting all lines (having a specific string) in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue deleting all lines (having a specific string) in a file
# 1  
Old 03-27-2014
Issue deleting all lines (having a specific string) in a file

I'm trying to create a script.

There are 2 files - fileA.log & fileB.log
fileA.log has the below data :
aaaa
cccc
eeee


fileB.log has the below data :
cjahdskjah aaaa xyz
jhaskjdhas bbbb abc
ajdhjkh cccc abc
cjahdskjah dddd xyz
jhaskjdhas eeee abc


I need to read each line from fileA.log, check fileB.log for that string and delete the line from fileB.log, if that string exists

Finally the file fileB.log should have the below data :

jhaskjdhas bbbb abc
cjahdskjah dddd xyz


Below is the script that I've used :

Code:
File_A=/tmp/fileA.log
File_B=/tmp/fileB.log
while read line
        do
        sed -i "/\b$FileA\b/d" $File_B
done <$File_A

But unfortunately, it is deleting all the lines.

Please let me know what's wrong with my script.
# 2  
Old 03-27-2014
Code:
grep -vf fileA.log fileB.log

This User Gave Thanks to Yoda For This Post:
# 3  
Old 03-27-2014
Hi,
You must use $line versus $FileA in your line sed:
sed -i "/\b$line\b/d" $File_B

But, your solution read/write file2 as far as number line from file1

Better solution:
Code:
$ printf '/\\b%s\\b/d\n' $(<file1.txt) | sed -f - file2.txt
jhaskjdhas bbbb abc
cjahdskjah dddd xyz

or
Code:
$ sed -e 's#.*#/\\b&\\b/d#' file1.txt | sed -f - file2.txt
jhaskjdhas bbbb abc
cjahdskjah dddd xyz

Regards.
This User Gave Thanks to disedorgue For This Post:
# 4  
Old 03-27-2014
Should like cjahdskjah aaaaSTUFF xyz be deleted from fileB.log in your example?

If not modify Yoda's solution as follows:

Code:
grep -vwf fileA.log fileB.log

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 03-28-2014
Thanks Folks!!!

With the input you all gave I got it working with the below script :

Code:
while read line
       	do
	grep -v $line fileB.log > $TEMP_FILE
	cat /dev/null > fileB.log
	cat $TEMP_FILE > fileB.log
done <$fileA.log

# 6  
Old 03-28-2014
Quote:
Originally Posted by Pandee
Thanks Folks!!!

With the input you all gave I got it working with the below script :

Code:
while read line
       	do
	grep -v $line fileB.log > $TEMP_FILE
	cat /dev/null > fileB.log
	cat $TEMP_FILE > fileB.log
done <$fileA.log

That's massively inefficient. There's no need to invoke grep once per line.

If the lines in fileA.log are not regular expressions, use fixed string matching.

The first cat command it's pointless.

If there are slashes in your data, read will eat them. If there are trailing slashes, lines will be joined.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 03-28-2014
Thanks Alister. Have taken your inputs and made the changes as needed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting specific lines from text file via scripting

Hi, I'm trying to search for some number and from that line, i need to delete the 5th line exactly. Eg: Consider below as text file data: 10000 a b c d e . . . 10000 w q t (8 Replies)
Discussion started by: Gautham
8 Replies

2. Shell Programming and Scripting

Help required deleting specific lines from file

Hi, I have a file with 20 columns of data and hundreds of lines of the same format. Here is an example line. The data repeats underneath with the same format. 15 1 4 GLY - 1 65 LYSH 23 N - 24 H - 634 O 0.188 157.552 487 48.70I have been sorting this data by hand but I was wondering if I... (3 Replies)
Discussion started by: livbaddeley
3 Replies

3. Shell Programming and Scripting

Deleting lines in a fixed length file where there is a word at specific location

I have a big file having 100 K lines. I have to read each line and see at 356 character position whethere there is a word "W" in it. If it is their then don't delete the line otherwise delete it. There are two lines as one Header and one trailer which should remain same. Can somebody... (5 Replies)
Discussion started by: mohit kanoongo
5 Replies

4. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

5. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file filled with dates, such as: 04-08-2011 message 04-08-2011 message 03-08-2011 message 01-08-2011 message 31-07-2011 message 24-07-2011 message 15-07-2011 message 13-12-2008 message 26-11-2007 message And I want to delete those lines whose date is older than 10... (5 Replies)
Discussion started by: asanchez
5 Replies

6. Shell Programming and Scripting

deleting specific lines in a file

Hello, I have a file like: 26-07-2011 sunz02 message1 26-07-2011 sunz02 message2 26-07-2011 sunz02 message3 15-07-2011 sunz02 message1 15-07-2011 sunz02 message2 15-07-2011 sunz02 message3... (5 Replies)
Discussion started by: asanchez
5 Replies

7. Shell Programming and Scripting

Deleting specific lines in a file

Hello, I have a file like this one: 03-07-2011 sunz02 message1 03-07-2011 sunz02 message2 03-07-2011 sunz02 message3 01-07-2011 sunz02 message1 01-07-2011 sunz02 message2 01-07-2011 sunz02 ... (1 Reply)
Discussion started by: asanchez
1 Replies

8. Shell Programming and Scripting

deleting specific lines in a file

I want to delete all lines from a file (orig_file) that contain the regex values (bad_inv_list) I tried a for each loop with sed but it isn't working for file in `cat bad_inv_list`; do sed '/$file/d' orig_file > pared_down_file.1 mv pared_down_file.1 orig_file done I've added... (2 Replies)
Discussion started by: verge
2 Replies

9. Shell Programming and Scripting

Deleting a line from a file based on one specific string instance?

Hello! I need to delete one line in a file which matches one very precise instance of a string only. When searching the forum I unfortunately only found a solution which would delete each line on which a particular string occurs. Let's assume I have a file composed of thousands of lines... (4 Replies)
Discussion started by: Black Sun
4 Replies

10. Shell Programming and Scripting

Deleting specific lines in a file

I have a file which has some lines starting with a particular word. I would like to delete 5 lines before each such line containing that particular word. eg: line1 line2 line3 line4 line5 line6 "particular word"... I would like to delete line2-line6 and all such occurences in that... (4 Replies)
Discussion started by: ramu_1980
4 Replies
Login or Register to Ask a Question