Delete strings in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete strings in a file
# 1  
Old 11-08-2007
Delete strings in a file

Hi,

I have a file named status.txt that looks like the file below. What I want to do is to delete the part <status> and </status> and just leave the number and print each number per line. How can I do it? If I will use sed or awk how can I do it? I tried with sed but it didn't work. Maybe I just don't know how to do it. Thanks!

<status>29</status><status>29</status><status>1</status><status>29</status><status>201</status><status>201</status><status>29</statu
s><status>1</status><status>29</status><status>29</status><status>1</status><status>29</status><status>253</status><status>1</status
><status>1</status><status>29</status><status>201</status><status>1</status><status>1</status><status>201</status><status>29</status
><status>201</status><status>201</status><status>1</status><status>1
</status><status>201</status><status>253</status>

expected output:
29
29
1
29
.... etc.
# 2  
Old 11-08-2007
try:
Code:
sed 's#<status>##g' filename | sed 's#</status>##g' > newfilename

# 3  
Old 11-08-2007
Delete strings in a file

Hi Jim,

I've tried the command that you've sent me but the output file status2.txt is empty. Please help me. Thanks a lot for taking time to answer my post.


sed 's#<status>##g' status.txt | sed 's#</status>##g' > status2.txt
# 4  
Old 11-08-2007
Try:

Code:
sed 's/<status>//g' status.txt|awk 'BEGIN {RS="</status>"}{print $1}' > status2.txt

Regards
# 5  
Old 11-08-2007
Defining RS as a string doesn't work for all versions of awk.
Another awk solution :
Code:
awk -ORS='' '
{
   gsub(/<status>/, "");
   gsub(/<\/status>/, "\n" );
   print $0;
}
' input_file

Jean-Pierre.
# 6  
Old 11-08-2007
Quote:
Originally Posted by jim mcnamara
try:
Code:
sed 's#<status>##g' filename | sed 's#</status>##g' > newfilename

And an alternative would be
Quote:
sed 's#<status>##g' status.txt | sed 's#/status>##g'|tr '<' ' '
ie, leave a character that You can replace with a field separator, since otherwise You would have output that is hard to interpret.
# 7  
Old 11-08-2007
here is another solution based on the fact that the y command can take a
newline on the RHS.

sed -e 's/\<string\>//g' -e 's/\<\/string\>/~/g' -e 'y/~/\n/' infile > outfile
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. UNIX for Dummies Questions & Answers

Delete specific strings in a file

Hi, My file has a numerous sttrings.I want to retain those strings which start with stt and delete entries with >C For eg: my infile is >C4603985... (7 Replies)
Discussion started by: sa@@
7 Replies

3. Shell Programming and Scripting

How to delete strings in a file?

hi, i have a big file like this: >s31 length=12 numreads=6 gene=isotig454 status=igo ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk ldfddfdfdfdkkkkkkfdfdkkkksdfdkkkkkkkkkksdfd dfdfdfldfdkdffdlfddflfdjkkkkkkfdgkkgfhghfgkkk >c2 length =344... (4 Replies)
Discussion started by: the_simpsons
4 Replies

4. Shell Programming and Scripting

Delete lines starting with these strings

Platform : RHEL 5.8 I have text file called myapplication.log . In this file, I have around 800 lines which start with the followng three strings PWRBRKER-3493 PWRBRKER-7834 SCHEDULER-ERROR How can I delete these lines in one go ? (13 Replies)
Discussion started by: omega3
13 Replies

5. Shell Programming and Scripting

Delete 2 strings from 1 line with sed?

Hi guys, I wonder if it's possible to search for a line containing 2 strings and delete that line and perhaps replace the source file with already deleted line(s). What I mean is something like this: sourcefile.txt line1: something 122344 somethin2 24334 45554676 line2: another something... (6 Replies)
Discussion started by: netrom
6 Replies

6. Shell Programming and Scripting

Delete chunk of text if contains certain strings

Using awk how to delete chunk of text if it contains certain strings? As in the following, delete a reference chunk, i.e. everything from <reference attribute = "value"> to </reference> inclusive, if within it "Group ID" value is 7 or 96 or 103 or 1005. <reference attribute = "value"> ... (3 Replies)
Discussion started by: pioavi
3 Replies

7. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

8. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

9. Shell Programming and Scripting

Delete Strings that are present in another file

HI, if a String is present in file1.txt, i want to delete that String from file2.txt. How can i do this?? I am sure that the file1.txt is a subset of file2.txt. (2 Replies)
Discussion started by: jathin12
2 Replies

10. Shell Programming and Scripting

recursively delete the text between 2 strings from a file

i have 200000bytes size of a unix file i need to delete some text between two strings recursively using a loop with sed or awk . these two strings are : 1st string getting from a file :::2 nd string is fi...its constant . can anyone help me sed -n'/<1 st string >/,/fi/' <input_filename> is the... (2 Replies)
Discussion started by: santosh1234
2 Replies
Login or Register to Ask a Question