For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > Re: bash to awk









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: bash to awk
Ed Morton

2004-03-19, 8:23 pm



davide moro wrote:

> Today I made this bash-script, can you help me to translate it to awk-code?
> Its prototype is:
> ls -l | ./script 1 7 9 12
> It prints the 1st, 7th, 9th and 12th record of any stream of characters (for
> example: ls -l, cat file1, ecc)

<snip>
> AWK:
> example: ls -l | ./script.awk 1 7 10
> I have this idea
> {
> if (count == ???????)
> print;
> count++;
> }
>
> but I don't know what I should write instead of ?????.
> Can anyone help me?


Here's one way:

lineNrs="1 7 10"
awk -v lineNrString="$lineNrs" 'BEGIN {
split(lineNrString,i2LNs)
for (i in i2LNs) lineNrs[i2LNs[i]] = i
}
NR in lineNrs { print }'

Regards,

Ed.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com