Select a pattern from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select a pattern from file
# 1  
Old 07-08-2009
Select a pattern from file

Hi,

I have a requirement to select only a specific pattern from a flat file and delete its occurance.
For eg.
If my file contains :
Code:
<A1>1234</A1>
<A2>5678</A2>
<ABC>1234</ABC>
<A3>0987</A3>

Then, i want to delete <ABC>1234</ABC> from the file and have the contents as :-
Code:
<A1>1234</A1>
<A2>5678</A2>
<A3>0987</A3>

How can i identify the occurance of this pattern?
Contents inside the tag<ABC>can change in various file.

Thanks in advance.

Last edited by Yogesh Sawant; 07-09-2009 at 05:35 AM.. Reason: added code tags
# 2  
Old 07-08-2009
Would you like to delete the lines that starts with <ABC>?

Code:
 
cat infile |  sed '/^<ABC>/d'

# 3  
Old 07-08-2009
Actually, my full requirement is :-
In a file, i have some data. Eg.
Code:
<A1>1234</A1><A2>5678</A2><ABC>1234</ABC><A3>0987</A3>
<B1>1234</B1><B2>5678</B2><ABC>1234</ABC><B3>0987</B3>
<C1>1234</C1><C2>5678</C2><ABC>1234</ABC><C3>0987</C3>

Now, i wanted to delete the occurance of pattern <ABC>1234</ABC> from each line.
The required output is :
Code:
<A1>1234</A1><A2>5678</A2><A3>0987</A3>
<B1>1234</B1><B2>5678</B2><B3>0987</B3>
<C1>1234</C1><C2>5678</C2><C3>0987</C3>

I can also manage to bring this pattern at the end of each line, so even deletion of contents after <ABC> will also suffice my purpose.

Thnx.

Last edited by Yogesh Sawant; 07-09-2009 at 05:34 AM.. Reason: added code tags
# 4  
Old 07-08-2009
This would remove tags <ABC> and digits included
Code:
sed -e 's#<ABC>[0-9]\+</ABC>##g'

# 5  
Old 07-10-2009
Not solved yet.

I tried this command:-

cat FileName | sed -e 's#<ABC>[0-9]\+</ABC>##g'

It's not working.
No effect on my FileName. Smilie
# 6  
Old 07-10-2009
Try this.



Code:
sed 's/<ABC>.*<\/ABC>//g' file

# 7  
Old 07-10-2009
Quote:
Originally Posted by DTechBuddy
I tried this command:-

cat FileName | sed -e 's#<ABC>[0-9]\+</ABC>##g'

It's not working.
No effect on my FileName. Smilie
You will have to direct this output to a new file and then rename that to FileName.
or use Tee.

Code:
cat FileName | sed -e 's#<ABC>[0-9]\+</ABC>##g' > new_file
mv new_file FileName

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I select certain columns with matching pattern and rest of the lines?

I want to select 2nd, 3rd columns if line has "key3" and print rest of the lines as is. # This is my sample input key1="val1" key2="val2" key3="val3" key4="val4" some text some text some text some text key1="val1" key2="val2" key3="val3" key4="val4" some text some text some text some... (3 Replies)
Discussion started by: kchinnam
3 Replies

2. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

3. Shell Programming and Scripting

Modifying the shell script to select pattern

Hello, I have script which work 70% of the desired task , the output from script.sh is following , however the desired output I require is following . Any piece of suggestion would be great.. thanks in advance, emily #!/bin/bash ... (8 Replies)
Discussion started by: emily
8 Replies

4. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

Select only the last line from the pattern

Hi, I am really new in the shell script, but it is really useful for me to learn. I have one question, I have a large text document (actually few of them), inside there are lines with information about energies, between 10 to 20 of this lines, varies from one doc to another one, my questions... (11 Replies)
Discussion started by: hmartine1983
11 Replies

6. Shell Programming and Scripting

Select everything between first and last occurrence of same pattern

Greetings, I am writing a script which requires as a part, selecting all the lines between the first and last occurrence of a pattern. I have an nawk alternative that is working. But thre should be a generic script that should run on all os viz, linux, sun , aix. The awk script that i... (25 Replies)
Discussion started by: usha rao
25 Replies

7. Shell Programming and Scripting

select some text from a test dependng on pattern

I have some absolute file location $INSTALL_BASEPATH/onereview-5.0/resources/commons-messages/commonmessages_default.properties $INSTALL_BASEPATH/onereview-5.0/orv-deploy/config-console.war/WEB-INF/classes/com/connectiva/configuration/console/resource/configurationBundle.properties I need to... (3 Replies)
Discussion started by: mnmonu
3 Replies

8. Shell Programming and Scripting

Select everything before a pattern

Hi I have i doubt, actually i have to select everything before a word(pattern).For that i am using sed i am using the below line of code but it is not working i am getting a blank instead.. sed -n '/regexp/{g;1!p;};h' file1 Can anyone help? Thanks (15 Replies)
Discussion started by: usha rao
15 Replies

9. Shell Programming and Scripting

extract/select pattern from input

Hey, examples of the input (text line): /bla/blMOasdn234.adanif24/blabla.rar /bla/blMOasdn234.adanif24/blabla23124.bin /bla/bla/bla/bla/bla/bla.bin and what I need to do is extract/select only the dir path so the output would be: /bla/blMOasdn234.adanif24/ /bla/blMOasdn234.adanif24/... (4 Replies)
Discussion started by: TehOne
4 Replies

10. UNIX for Dummies Questions & Answers

How to select lines in unix matches a pattern at a particular position

I have huge file. I want to copy the lines which have first character as 2 or 7, and also which has fist two characters as 90. I need only these records from file. How I can acheive this. Can somebody help me..... (2 Replies)
Discussion started by: cs_banda
2 Replies
Login or Register to Ask a Question