Operating System - HP-UX
1754152 Members
3338 Online
108811 Solutions
New Discussion юеВ

Re: data extraction from file - help

 
Praveen Hari
Advisor

data extraction from file - help

I have file format as given below...
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
10 REPLIES 10
V.Tamilvanan
Honored Contributor

Re: data extraction from file - help



x=`awk -F : '/Xref/{print $3}' `
John Carr_2
Honored Contributor

Re: data extraction from file - help

cat filename | awk -F : '/Xref/{print $3}' > newfilename

John.
Mark Grant
Honored Contributor

Re: data extraction from file - help

grep Xref | awk -F: '{ print $3 }'
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: data extraction from file - help

Sorry, didn't notice we'd all done the same thing!

how about I do it with perl then

perl -n -e 'if(/Xref.+:(\d+)/){print "$1\n"}'
Never preceed any demonstration with anything more predictive than "watch this"
H.Merijn Brand (procura
Honored Contributor

Re: data extraction from file - help

Mark, one-liner code uses command line options :)

# perl -nle'/^Xref.+:\s*(\d+)/&&print$1' file

in a real script, it could be as

--8<---
#!/opt/perl/bin/perl

while (<>) {
m/^Xref\s*:.*:\s*(\d+)/ or next;
print "$1\n";
}
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Elmar P. Kolkman
Honored Contributor

Re: data extraction from file - help

Part of the problem in your other post... It depends on whether it is always the third or last column, you can use awk or cut...

If it is always the 3rd column:
grep -i '^xref:' | cut -d: -f3

If it is always the last column:
awk -F: '/XRef/ {print $NF}'

Mind one thing: awk will not match any line now, because XRef != Xref, that's why I used the liberty to use the -i option to grep.
Every problem has at least one solution. Only some solutions are harder to find.
H.Merijn Brand (procura
Honored Contributor

Re: data extraction from file - help

Elmar, please mail me at h dot m dot brand at procura dot nl

no points please :)
Enjoy, Have FUN! H.Merijn
Praveen Hari
Advisor

Re: data extraction from file - help

When I run
awk -F: '/XRef/ {print $NF}' filename > filename1
It writes correct output into filename2.
But if I put the same command on a script file named 1.csh and run it, it doesn't out the results into filename2.

Can some one help me why it doesn't work ???
john korterman
Honored Contributor

Re: data extraction from file - help

Hi,
make a small correction, change:
XRef
to
Xref


regards,
John K.
it would be nice if you always got a second chance