Visit Our UNIX and Linux User Community


Grep a string from input file and delete next three lines including the line contains string in xml


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep a string from input file and delete next three lines including the line contains string in xml
# 1  
Old 09-28-2011
Grep a string from input file and delete next three lines including the line contains string in xml

Hi,

1_strings file contains
Code:
$ cat 1_strings
/home/$USER/Src
/home/Valid
/home/Review

Code:
$ cat myxml 
<projected value="some string" path="/home/$USER/Src">
<input 1/>
<estimate value/>
<somestring/>
</projected>
<few more lines >
<projected value="some string" path="/home/$USER/check">
<input 1/>
<estimate value/>
<somestring/>
</projected>
....
<few more>
<projected value="some string" path="/home/Valid">
<input 1/>
<estimate value/>
<somestring/>
</projected>
<few more>
<few more>

Code:
output: 
<few more lines >
<projected value="some string" path="/home/$USER/check">
 <input 1/>
 <estimate value/>
 <somestring/>
 </projected>
....
<few more>
<few more>

When i run grep -A3 i get the lines to be removed but i couldnt remove lines in xml file.
Appreciate your help.

Last edited by greet_sed; 09-28-2011 at 05:23 AM.. Reason: updated output sample
# 2  
Old 09-28-2011
Try grep -v -A3 infile > newfile.
# 3  
Old 09-28-2011
With GNU sed you can try something like this (a sketch) :
Code:
while read line; do
  sed -i '/'"$line"'/,+4d' myxml
done <1_strings

# 4  
Old 09-28-2011
Or you can try this approach:
Code:
awk ' -F\"
NR==FNR{a[$0]=$0;next}
/<projected value=/ && $(NF-1) in a {f=1}
/<\/projected>/ && f {f=0; next}
!f
' 1_strings myxml

# 5  
Old 09-28-2011
hi,
How did i miss grep -v Smilie
i will test all the solutions to begin with.
Thanks to everyone.

Previous Thread | Next Thread
Test Your Knowledge in Computers #556
Difficulty: Easy
Making mistakes in programming is inevitable.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all lines except a line starting with string

Shell : bash OS : RHEL 6.8 I have a file like below. $ cat pattern.txt hello txt1 txt2 txt3 some other text txt4 I want to remove all lines in this file except the ones starting with txt . How can I do this ? (4 Replies)
Discussion started by: omega3
4 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

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

4. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

5. Shell Programming and Scripting

Grep line with all string in the lines and not space.

I want to write the syntax so does not count line with no space. So currerntly it is showing lines as 5, but i want to show 4. # cat /tmp/mediacheck | sort -u | grep -vi " " | awk '{print $1}' | wc -l BA7552 BAA002 BAA003 BAA004 (6 Replies)
Discussion started by: Junes
6 Replies

6. UNIX for Dummies Questions & Answers

Delete a line containing a string from user input

I have code that accepts input from a user, and when the user hits enter it is supposed to delete that whole line from the file. echo "Which record? " read record sed '/$record/d' file However, it does not delete it. Any help? (1 Reply)
Discussion started by: itech4814
1 Replies

7. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

10. Shell Programming and Scripting

Grep a string in a range and delete the line

Hi, i need to delete a lines after searching a particular string but this searching should only happen after the 4th line.. basically imagine a file like this From: abcd.yahoo.com To: cdeb.yahoo.com Subject: hi all sdfsd sadasd asdasd dfsdf From: abcd.yahoo.com To:... (3 Replies)
Discussion started by: depakjan
3 Replies

Featured Tech Videos