[grep awk cut] > awk

Hi, I'm very new to scripting.

grep $s $filename | awk '{print $2}' | cut -c 1-8

How can I optimize this using a single awk?

I tried:
awk '/$s/ {print $2}' $filename | cut -c 1-8

However didn't work, I think the awk is not recognizing $s and the verbal is something else.

Can you tell us about your requirement.. What exactly are you trying to do.

Are you looking for something like this

grep $s $filename| awk '{print substr($2,1,8)}'

May be,

awk -v var=$s '/$0 ~ var/ {print substr($2,1,8)}' $filename

further in a single awk

var=some_pattern

( the value of s is assigned here )

awk -v var=$a '/'$a'/ { print substr($2, 1, 8) }' filename

If not this,

post the sample input and sample output :slight_smile:

Not exactly an awk solution. I believe this should work

sed -n -e "/$s/s/^[^ ]* \(.\{8\}\).*/\1/p" $filename

Try...

awk '$0~s {print substr($2,1,8)}' s=$s $filename

Thanks all.

I got the stuff worked. This was my first posting in unix.com. I just loved you people.

Thanks again.