Searching for pattern and remove the lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for pattern and remove the lines
# 1  
Old 04-04-2013
Code Searching for pattern and remove the lines

Hi ,
I want to remove the specific pattern and remove those lines from file using shell script.

i want to remove these lines

Code:
<?xml version='1.0' encoding='UTF-8'?>
<row_set>
</row_set>

my input file has content like this.


file name: sample.xml

Code:
<?xml version='1.0' encoding='UTF-8'?>
<row_set>
<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record>
<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record>
</row_set>

required output:
file: new.xml

<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record>
<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record>

Please someone help me onthis.am new to unix.

Thanks,
Madhu

Last edited by Corona688; 04-04-2013 at 01:30 PM..
# 2  
Old 04-04-2013
Code:
awk '!/<?xml/&&!/<row_set/&&!/<\/row_set/' file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 04-05-2013
Hi Yoda,

Thank you very much.its working fine.But actual my input file is like below.
Code:
<?xml version='1.0' encoding='UTF-8'?>
<row_set>
<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record>
<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record></row_set><?xml version='1.0' encoding='UTF-8'?>
<row_set>
<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record>
<row_record>
    <a>testing1</a>
    <b>testing2</b>
    <c>testing3</c>
</row_record>
</row_set>

So it is replacing entire line when string eligible.

I want to replace exact string with out deleting other string.

how can i achieve this.

Thanks,
Madhu
# 4  
Old 04-05-2013
Code:
 
sed -e "s:<?xml version='1.0' encoding='UTF-8'?>::" -e "s:</*row_set>::g" -e "/^$/d"

This User Gave Thanks to 116@434 For This Post:
# 5  
Old 04-05-2013
Hi
Thank you yoda and 116@434.

With your help i have solved my probelm.

here is Solution for my probelm.

Code:
Solution1:
sed -e 's/<\?xml version='"'"'1.0'"'"' encoding='"'"'UTF-8'"'"'?>//' -e 's/<Cond_Tbl_Data_Set>//' -e 's/<\/Cond_Tbl_Data_Set>//'   ${FULL_DATA_PATH} >> ${OUT_FILE}
sed '/^$/d' <${OUT_FILE} >> ${OUT_FILE1}

Solution2:

sed -e "s:<?xml version='1.0' encoding='UTF-8'?>::" -e "s:</*Cond_Tbl_Data_Set>::" -e "/^$/d" ${FULL_DATA_PATH} >> ${OUT_FILE}

Solution 3 with AWK

awk '!/<?xml/&&!/<row_set/&&!/<\/row_set/'  INPUT_FILE.

But solution 3 is removing the entire line with the selected criteria.in my case sol1 & 2 working fine only by removing selected string.

These are not my solutions.

Thanks,
Madhu
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to remove pattern and lines above pattern

In the awk below I am trying to remove all lines above and including the pattern Test or Test2. Each block is seperated by a newline and Test2 also appears in the lines to keep but it will always have additional text after it. The Test to remove will not. The awk executed until the || was added... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

sed to remove next 15 lines after pattern in Solaris

Team, I am trying to use sed to delete 15 lines, after pattern patch, which includes the pattern as well in Solaris. I used the below command, as we do it Linux, but it's not working as expected in Solaris. I am getting the error as "garbled".sed '/\/table/,+15d' status.html sed: command... (8 Replies)
Discussion started by: Nagaraj R
8 Replies

3. Shell Programming and Scripting

How to remove the lines with this pattern?

Hello everyone, I have a sample data like this: Glyma.10G051100 Glyma.02G036000 89.91 228 23 0 1 228 1 228 1e-78 294 Glyma.10G051100 Glyma.09G023700 87.28 228 29 0 1 228 1 228 1e-68 261 Glyma.10G285200 Glyma.20G103800 96.33 1663 55 4 1 1657 1 1663 0.0 2728 Glyma.10G285200 Glyma.05G093700 95.02... (2 Replies)
Discussion started by: nengcheng
2 Replies

4. Shell Programming and Scripting

Remove multiple lines that match pattern

Not sure how I can accomplish this. I would like to remove all interfaces that have the commands I would like to see: switchport port-security, spanning-tree portfast. One line is no problem. interface FastEthernet0/8 spanning-tree portfast interface FastEthernet0/9 spanning-tree... (4 Replies)
Discussion started by: mrlayance
4 Replies

5. UNIX for Dummies Questions & Answers

Add lines after searching for a pattern

Hi, I have 2 files like below. File A: apple mango File B: start abc def apple ghi end start cba fed (4 Replies)
Discussion started by: jayadanabalan
4 Replies

6. Shell Programming and Scripting

Remove the footer from a flat file by searching a pattern

Hi, I have more than 30,000 records in a flat file. I want to remove footer from the file by searching a string pattern for the footer. Example.... let the flat file test.dat contains below records. record1 record2 record3 .. .. .. record31000 Total records 31000 I want to remove the... (6 Replies)
Discussion started by: gani_85
6 Replies

7. Shell Programming and Scripting

NAWK to remove lines that matches a specific pattern

Hi, I have requirement that I need to split my input file into two files based on a search pattern "abc" For eg. my input file has below content abc defgh zyx I need file 1 with abc and file2 with defgh zyx I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies

8. Shell Programming and Scripting

Remove lines from batch of files which match pattern

I need to remove all lines which does not match the pattern from a text file (batch of text files). I also need to keep the header line which is the first line of the file. Please can you provide an example for that. I used this to solve half of my work. I was unable to keep the first line of... (3 Replies)
Discussion started by: talktobog
3 Replies

9. Shell Programming and Scripting

shell script to remove all lines from a file before a line starting with pattern

hi,, i hav a file with many lines.i need to remove all lines before a line begginning with a specific pattern from the file because these lines are not required. Can u help me out with either a perl script or shell script example:- if file initially contains lines: a b c d .1.2 d e f... (2 Replies)
Discussion started by: raksha.s
2 Replies

10. Shell Programming and Scripting

grep for a particular pattern and remove few lines above top and bottom of the patter

grep for a particular pattern and remove 5 lines above the pattern and 6 lines below the pattern root@server1 # cat filename Shell Programming and Scripting test1 Shell Programminsada asda dasd asd Shell Programming and Scripting Post New Thread Shell Programming and S sadsa ... (17 Replies)
Discussion started by: fed.linuxgossip
17 Replies
Login or Register to Ask a Question