Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

how to select lines from file?

SOLVED
Go to Solution
Esteemed Contributor Esteemed Contributor

how to select lines from file?

Hi guys..
I have a file with lines like :

aabbddcc1234xxxx
aa1234cc2345yyyy
aa1234xx3456zzzz

How can i select the lines that have certain string in caracters 10-14 ?

1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

Re: how to select lines from file?

Hi Henk:

Using your data and using 'awk' [because that's one way] you could do:

# awk '{if (substr($0,9,4)==1234) {print}}' file
aabbddcc1234xxxx

Regards!

...JRF...
8 REPLIES
Honored Contributor Honored Contributor

Re: how to select lines from file?

man grep

grep aa1234cc /path/filename

Just a quick thought,
Rita
Esteemed Contributor Esteemed Contributor

Re: how to select lines from file?

oke .. I have to make it more clear..

aabbddcc1234xxxx
aa1234cc2345yyyy
aa1234xx3456zzzz

when i to select the line with caracters 8-11 to be 1234 , grep will also show other lines.. as grep does not use the place of the caracters in the line..

Can awk do this ?
What will be the command.. ?
regards
Trusted Contributor Trusted Contributor

Re: how to select lines from file?

Sorry qust: is not clear for me...
Honored Contributor Honored Contributor

Re: how to select lines from file?

How about:

grep '.......1234*' /path/filename

That's seven dots or periods then 1234 and anything else after that...

Rgrds,
Rita
Honored Contributor Honored Contributor

Re: how to select lines from file?

Hi Henk,

another quick thought:

grep -E "........1234*" ./infile


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

Re: how to select lines from file?

Hi Henk:

Using your data and using 'awk' [because that's one way] you could do:

# awk '{if (substr($0,9,4)==1234) {print}}' file
aabbddcc1234xxxx

Regards!

...JRF...
Esteemed Contributor Esteemed Contributor

Re: how to select lines from file?

Thanks James..
This was what I was looking for!!!

regards,
Henk
Highlighted
Acclaimed Contributor Acclaimed Contributor

Re: how to select lines from file?

>when I select the line with characters 8-11 to be 1234

(Typically line positions start with 1, your example seems to be 0.)
An old thread about it:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=76665

Or for your case for columns 9-12.
grep '^.\{8\}1234' file