Using sed to remove paragraphs with variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using sed to remove paragraphs with variables
# 1  
Old 07-03-2008
Using sed to remove paragraphs with variables

Hi everyone,

I have a file with multiple entries and I would like to remove the ones that contain either /A"> or /A/, where A can be any letter of the alphabet. Here's an example of the entries:
<Topic r:id="Top/World/Fran">
<catid>476</catid>
<link r:resource="http://fr.news.yahoo.com/"/>
<link r:resource="http://news.google.fr/"/>
<link r:resource="http://actualite.free.fr"/>
</Topic>
<Topic r:id="Top/World/Fran/Act/A_la_Une">
<catid>32293</catid>
<link r:resource="http://www.pluralworld.com/"/>
<link r:resource="http://www.webdopresse.ch/"/>
</Topic>
<Topic r:id="Top/World/Fran/A">
<catid>32069</catid>
</Topic>
<Topic r:id="Top/World/Fran/B/Stuff">
<catid>32069</catid>
</Topic>
So, in this case, I want to have a new file that does not keep the last two entries as the first contains /A"> and the second contains /B/.

I have tried with the following code, but it removes everything!

#!/bin/sh
# Removes topics that begin with a certain value
inputFile=$1

tempFile=$inputFile.tmp

# A number of indexing categories exist that have to be removed
ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
n=0

while [ $n -lt ${#ALPHABET} ]
do
sed -n '/\/${ALPHABET:n:1}\">/,/<\/Topic>/!p' $tempFile.start > $tempFile.end
mv $tempFile.end $tempFile.start

sed -n '/\/${ALPHABET:n:1}\//,/<\/Topic>/!p' $tempFile.start > $tempFile.end
mv $tempFile.end $tempFile.start

n=$(( $n + 1 ))
done


Can anyone out there please help?
# 2  
Old 07-03-2008
The following works for ksh93
Code:
#!/bin/ksh93

while read line
do
   [[ "$line" = ~(E)(\/[[:alpha:]]\/|\/[[:alpha:]]\"\>$) ]] || print $line
done < file

exit 0

Output is
Code:
<Topic r:id="Top/World/Fran">
<catid>476</catid>
<link r:resource="http://fr.news.yahoo.com/"/>
<link r:resource="http://news.google.fr/"/>
<link r:resource="http://actualite.free.fr"/>
</Topic>
<Topic r:id="Top/World/Fran/Act/A_la_Une">
<catid>32293</catid>
<link r:resource="http://www.pluralworld.com/"/>
<link r:resource="http://www.webdopresse.ch/"/>
</Topic>
<catid>32069</catid>
</Topic>
<catid>32069</catid>
</Topic>

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to have local shell variables in a ksh script seen on remove server in SSH block?

I have googled this and found many solutions, but none of them are working for me. I am in a korn shell, most others reference bsh, maybe that is the issue? Anyway, all I am trying to do is use a variable I have declared in my main script in a remote shell I am running through ssh. So I have a... (8 Replies)
Discussion started by: DJR
8 Replies

2. Shell Programming and Scripting

Remove last '1' in list of variables

Hi folks, I have a list of variables as follows: CDBTEST1 messdba1 sat11cru1 s12tgts1 sa12ss1 I need to remove the last '1' so I can use the remaining variables in a for loop: CDBTEST messdba sat11cru s12tgts sa12ss Something like this: (3 Replies)
Discussion started by: jonnyd
3 Replies

3. Shell Programming and Scripting

Need help with sorting in paragraphs

I am very new to shell scripting, current try to do a sorting of a text file in paragraphs with ksh script. example: File content: A1100001 line 1 = "testing" line 2 = something, line 3 = 100 D1200003 line 1 = "testing" line 2 = something, line 3 = 100 B1200003 line 1 =... (3 Replies)
Discussion started by: gavin_L
3 Replies

4. Shell Programming and Scripting

Split text into paragraphs

Hi all! I want to make a code to split sentences into paragraphs maybe 4-5 sentences into one <p>text</p> there are no new lines in the text string any ideas with AWK, SSH? Thank you! (5 Replies)
Discussion started by: sanantonio7777
5 Replies

5. Shell Programming and Scripting

Extract paragraphs under conditions

Hi all, I want to extract some paragraphs out of a file under certain conditions. - The paragraph must start with 'fmri' - The paragraph must contain the string 'restarter svc:/system/svc/restarter:default' My input is like that : fmri svc:/system/vxpbx:default state_time Wed... (4 Replies)
Discussion started by: Armoric
4 Replies

6. Shell Programming and Scripting

Remove ^? characters in shell variables on using backspace

Friends, I observed a peculiar problem in shell. if I set a variable using standard input and backspace was used by the user, then the variable get ^? characters embedded in the variable. ### echo "Enter value for X=" read X echo $X expr $X + 1 ### If the variable is echoed, then there... (3 Replies)
Discussion started by: sachinverma
3 Replies

7. Shell Programming and Scripting

deleting text records with sed (sed paragraphs)

Hi all, First off, Thank you all for the knowledge I have gleaned from this site! Deleting Records from a text file... sed paragraphs The following code works nearly perfect, however each time it is run on the log file it adds a newline at the head of the file, run it 5 times, it'll have 5... (1 Reply)
Discussion started by: Festus Hagen
1 Replies

8. Shell Programming and Scripting

fetching paragraphs with SED

hi, i am a SED newbie and i need some help. i have a log file as shown below. and i want to search specific Error Code, and fetch the whole paragraph. ... ... ................. ....ErrCode... ................. ... ... ... ................. ....ErrCode... ... (4 Replies)
Discussion started by: ipat
4 Replies

9. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

10. Shell Programming and Scripting

how to filter out some paragraphs in a file

Hi, I am trying to filter out those paragraphs that contains 'CONNECT', 'alter system switch logfile'. That means say the input file is : ------------------------------------------------------- Wed Jun 7 00:32:31 2006 ACTION : 'CONNECT' CLIENT USER: prdadm CLIENT TERMINAL: Wed Jun 7... (7 Replies)
Discussion started by: cnlhap
7 Replies
Login or Register to Ask a Question