cancel
Showing results for 
Search instead for 
Did you mean: 

how to extract from a file

SOLVED
Go to solution
federico_3
Honored Contributor

how to extract from a file


I have a long file in which there are some recursive strings. I need to find the last one and extract it with 6 lines before and 12 lines after. Please help me.. it's urgent
14 REPLIES
federico_3
Honored Contributor

Re: how to extract from a file

IF it's too difficult, could you help me to exstract only the last recursive string and the 12 lines after ?


Thanks
Carlos Fernandez Riera
Honored Contributor

Re: how to extract from a file

Hi Federico:


can you put a little example of your file and desired output?

unsupported
federico_3
Honored Contributor

Re: how to extract from a file


Example: STRING is the recursive string


tguyt
khhjh
STRING
reqrteqw
qewrtqrew
qwertqew
qwerrrrrrrrr
gyf
wert2w
q4ewt4te
tqtrew
jirt
reuii
gfsiu
iewutoiqu
tyql
STRING
ytuyltlyu
rwhrw
dthy
weryw
wqeyte
eqr
qeryt
ywyt
reytwqe
tqety42
tqer
qtet2
2tyty
yqeyq
qeyt2qet
qeyrwyewq
qyeyqy
qeyyyqeyt
STRING
yutuytlyu
]htgytyu
htuyt
uyiuyiu
iuiou'
uy;uiyu
juyh;ouiy
uyuy
1345
gaqaq
qre
qrew
rtw
t341
1434t31


I Should get :

2tyty
yqeyq
qeyt2qet
qeyrwyewq
qyeyqy
qeyyyqeyt
STRING
yutuytlyu
]htgytyu
htuyt
uyiuyiu
iuiou'
uy;uiyu
juyh;ouiy
uyuy
1345
gaqaq
qre
qrew
rtw


Carlos Fernandez Riera
Honored Contributor

Re: how to extract from a file

I think this will work ( more or less):



awk ' $1 == "STRING" { print NR-6","NR+12"p"}' file > sed_file

sed -n -f sedfile file


Goood luck.
unsupported
Olav Baadsvik
Esteemed Contributor

Re: how to extract from a file


Hi,

I do not quite understand what you mean
by recursive string.
But if you can use grep to find the line
number for the last occurence of the string,
I have a program that can read a sequence
of lines from a start-point to an endpoint
(linenumbers)
I used this to extract data from very large
logfiles from ccm.

Olav
federico_3
Honored Contributor

Re: how to extract from a file

Carlos, your script does not work...

Can you try and tell me what can i do ?

federico
Carlos Fernandez Riera
Honored Contributor
Solution

Re: how to extract from a file

Federico:


The question is to get linenumbers and create a file that sed command can undestand.

I have copy/paste your example and it not works because the ocurrence of "STRING" is in line #3, so sed says :

sed: -3,15p is not a recognized function

but this is only because a negative linenumber.


Try this new

awk ' $1 == "STRING" { print NR-6","NR+12"p"}' 77 > sed_77
sed -n -f sed_77 77 >77_out


Then edit sed_77 file and change -3 by 1, and run sed command.

It works for your example
unsupported
Robin Wakefield
Honored Contributor

Re: how to extract from a file

Federico/Carlos,

You could put a conditional operator in the awk statement, to force the start line to be greater than zero:

# awk ' $1 == "STRING" { printf (NR>7) ? NR-6 : 1 ;print","NR+12"p"}' file | tail -1 > sedfile
# sed -n -f sedfile file

Rgds, Robin
H.Merijn Brand (procura
Honored Contributor

Re: how to extract from a file

l1:/tmp 113 > perl -e '$/="STRING";@r=(<>);@p=split/(?<=\n)/,$r[-2];@n=split/(?<=\n)/,$r[-1];@p>6&&splice@p,0,-7;@n>12&&splice@n,13;print @p,@n' file
2tyty
yqeyq
qeyt2qet
qeyrwyewq
qyeyqy
qeyyyqeyt
STRING
yutuytlyu
]htgytyu
htuyt
uyiuyiu
iuiou'
uy;uiyu
juyh;ouiy
uyuy
1345
gaqaq
qre
qrew
l1:/tmp 114 >
Enjoy, Have FUN! H.Merijn
Carlos Fernandez Riera
Honored Contributor

Re: how to extract from a file

Hi Procura:


I think Perl i will like Perl too much, it is a very human readable :-)))
unsupported
H.Merijn Brand (procura
Honored Contributor

Re: how to extract from a file

Well, it *is* readable if you use long variable names and formatting, but this is a one-liner. If the original autor (or anyone else) would like an explanation, I could create it as a file with formatting, indents and comments to make the onscure become obvious.
Enjoy, Have FUN! H.Merijn
Trond Haugen
Honored Contributor

Re: how to extract from a file

It's not neat and it don't do anny error checking but it works:

FILE=MyLargeFile
lastSTRING=$( grep -n STRING $FILE | tail -1 | cut -f1 -d:)
from=$(expr $lastSTRING + 5)

tail -$from $FILE | head -19


Regards,
Trond
Trond Haugen
Honored Contributor

Re: how to extract from a file

Sorry, I was too quick with my answer. It should read:

FILE=MyLargeFile
linesinfile$( wc -l $FILE) lastSTRING=$( grep -n STRING $FILE | tail -1 | cut -f1 -d:)
from=$(expr $lastSTRING + 6)
TAIL=$(expr $linesinfile - $from)
tail -$from $FILE | head -19


Regards,
Trond
Trond Haugen
Honored Contributor

Re: how to extract from a file

Do I get anything right today!?
Missing linefeed. This should be right:
FILE=MyLargeFile
linesinfile$( wc -l $FILE)
lastSTRING=$( grep -n STRING $FILE | tail -1 | cut -f1 -d:)
from=$(expr $lastSTRING + 6)
TAIL=$(expr $linesinfile - $from)
tail -$from $FILE | head -19

Regards,
Trond