Operating System - HP-UX
1753477 Members
4797 Online
108794 Solutions
New Discussion юеВ

Re: File manipulation help

 
Praveen Hari
Advisor

File manipulation help

#! rnews 0
Newsgroups: xxx
From: xxx
Subject: xxx
Date: xxx
Path: linux-nntp!forums-master!forums-1-dub!forums-1-dub!forums-master.sybase.com!forums-1-dub.sybase.com
Xref: linux-nntp sybase.test.xml:99
zzzzzzzzzzzzzzzzzzzzz

1)I need to replace "linux-nntp" in the line starting XRef: to "test".
2)I need to extract the number after sybase.text.xml from the line starting XRef: to a variable
3)Also I need to delete the text "linux-nntp!forums-master!" from the line starting "Path:"

Can soem one provide scripts for doing these operations.
Thanks
6 REPLIES 6
Rodney Hills
Honored Contributor

Re: File manipulation help

You could try the following using "sed"-

nbr=`sed -n -e '/^XRef/s/linux-nttp/test/' -e '/^Path: linux/d' -e 'w yournewfile' -e '/^XRef/s/^.*://p'`
By using "w" command under sed, you can copy the lines to "yournewfile" making the changes. The -n option tells sed not to automatically print each line. The only "p" command above is used to print the number you want off the "Xref" line.

HTH

-- Rod Hills
There be dragons...
Praveen Hari
Advisor

Re: File manipulation help

I got it partially working. I still need help on following part:
I have following line in the file:

Xref: linux-nntp sybase.test.xml:99


I need to extract the number 99 into a variable. I will be using this number to create a new file with that name. Please help....
Praveen Hari
Advisor

Re: File manipulation help

First I need to find the line contating XRef. Then Extract the number 99 from that line into a variable. I will use that variable to create a New file. Following lines are there in the original file.

#! rnews 0
Newsgroups: xxx
From: xxx
Subject: xxx
Date: xxx
Path: linux-nntp!forums-master!forums-1-dub!forums-1-dub!forums-master.sybase.com!forums-1-dub.sybase.com
Xref: linux-nntp sybase.test.xml:99
zzzzzzzzzzzzzzzzzzzzz
Michael Schulte zur Sur
Honored Contributor

Re: File manipulation help

Hi,

you dont need the number. Just run the sed commands or whatever comes up here, which is fine for you and then run the script of Jim from the other thread, you posted, to split the file.

greetings,

Michael
Elmar P. Kolkman
Honored Contributor

Re: File manipulation help

Seems like it is time to move to perl... Or run sed on the single lines in a shell script. In shell, it would become something like this:

outfile='default.output'
rnews 0 | while read line
do
part1=$(echo $line | cut -d: -f1)
case $part1 in
Xref)
line=$(echo $line | sed 's|linux-nntp|test|')
outfile=$(echo $line | awk FS=: '{print $NF}')
;;
Path)
line=$(echo $line | sed 's|linux-nntp!forums-master!||'
;;
*)
;;
esac
echo $line >> $outfile
done
Every problem has at least one solution. Only some solutions are harder to find.
Michael Schulte zur Sur
Honored Contributor

Re: File manipulation help

Hi Elmar,

no offense, but your approach is flawed. It writes all lines including rnews until the first xref into default.output. After that it divides the news at the wrong place into wrong files. You will have to make a major design change to get it work. I think, the csplit in his other thread is a great idea and combined with a few sed commands should work nicely.

greetings,

Michael