1833461 Members
3124 Online
110052 Solutions
New Discussion

Re: ksh script

 
SOLVED
Go to solution
David_246
Trusted Contributor

ksh script

Hi,

Does anyone know how to match a pattern within a text-file for specific linenumbers ?

As an example :

/tmp/text :
bla die bla
line 2 is filled
line 3 is filled
555
line 4 is filled

Now I want to catch the all numbers from lines 3 and 4.

so : var=`cat /tmp/text | ?? parse only lines 3 and 4?? | ?? grep [0-9] ??`

Now I know this looks very silly, but I have Perl in my mind and cannot translate it into ksh. So two questions, how do I only parse specific lines, how do I filter out only numbers for those 2 lines.

Any help will be much apreciated.

Regs David
@yourservice
9 REPLIES 9
Bernhard Mueller
Honored Contributor

Re: ksh script

Simply do grep -n -e "expression"

Regards,
Bernhard
Massimo Bianchi
Honored Contributor

Re: ksh script



cat file | awk ' /line 3/,/line4/ '

Quick and dirty ?

Massimo
David_246
Trusted Contributor

Re: ksh script

-n option doesn't work with linenumbers, it only shows the the linennumbers where it grep'ed the -e match from.

The awk option only works if you know what is on line 3 and 4, as this is your goal it doesn't help.

Thanks for your try.

Regs David
@yourservice
James R. Ferguson
Acclaimed Contributor
Solution

Re: ksh script

Hi David:

# awk 'NR>=3 && NR<=4 && /[[0-9]]*/ {print}' filename

Regards!

...JRF...
Bernhard Mueller
Honored Contributor

Re: ksh script

More closer to what you demand might be:

grep -n -e "[0-9]" /tmp/text | grep -E "^3|^4"

man grep gives you all the info you need

Regards,
Bernhard

David_246
Trusted Contributor

Re: ksh script

Good work James !!

I knew I could count on you :)


Regs David
@yourservice
Massimo Bianchi
Honored Contributor

Re: ksh script

cat file | head -n 5 | tail -n 1

Second try
Francisco J. Soler
Honored Contributor

Re: ksh script

Hi David,

Sorry but i don't understand very well your problem.
Why line4 is 5th line in the file?
The 555 number is the line 4 or is part of the line 3?

If i parse only line 3 and line 4 that you say, i have no matter to catch the 555 number.

Please tell me more accuratelly that you want, perhaps i can help you.

Frank.

Linux?. Yes, of course.
Michael Kelly_5
Valued Contributor

Re: ksh script

David,
if you only want to pick out the numbers from lines 3 & 4, try this (I'm assuming that all the numbers are integers):

cat text.awk
NR == 3 || NR == 4 {
for (i=1; i<=NF; i++) {
if (int($i) == $i) {
printf("%d ", $i)
}
}
printf("\n")
}

cat /tmp/text | awk -f text.awk
3
555


HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.