awk split var twice -


i split var twice awk, have got far.

awk -v p=1,3,8,25-27,4-16 '{split(p,t,",");for (i in t) if(t[i] ~ /-/) split(t[i],t1,"-") {print "-dfirstpage=" t1[1] ,"-dfirstpage=" t1[2]} else {print "-dfirstpage=" t[i] ,"-dfirstpage=" t[i]}}' >outfile 

output shall be

-dfirstpage=1 -dlastpage=1 -dfirstpage=3 -dlastpage=3 -dfirstpage=8 -dlastpage=8 -dfirstpage=25 -dlastpage=27 -dfirstpage=4 -dlastpage=16 

could pass value of p input awk? in case, way:

$ echo 1,3,8,25-27,4-16 | awk -f- -v rs=, '{printf "-dfirstpage=%s -dlastpage=%s\n", $1, $2?$2:$1}' -dfirstpage=1 -dlastpage=1 -dfirstpage=3 -dlastpage=3 -dfirstpage=8 -dlastpage=8 -dfirstpage=25 -dlastpage=27 -dfirstpage=4 -dlastpage=16 

how works:

  • i define comma record separator (-v rs=,) , hyphen field separator (-f-). so, input

    1,3,8,25-27,4-16 

    will equivalent to

    1 3 8 25 27 4 16 
  • then, use printf:

    '{printf "-dfirstpage=%s -dlastpage=%s\n", $1, $2?$2:$1}' 

    the first parameter first column. in second parameter, ask if second column $2 exists; if so, value of parameter $2; if no, value first column $1.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -