Splitting a string into exactly two parts in awk -


in awk (gnu awk 4.1.3 on ubuntu 16.04) , best way split string 2 substrings @ first occurrence of separator character (here :), leaving second half is, if contains more separators?


what have looks (simplified) (linebreaks added visibility):

awk '/^[^=]+:/ {          split($0, a, ":") ;          system("echo part 1: "a[1]) ;          print "part 2: "a[2] ;      }' 

i need call external application first part argument (using echo here example) , print second part is, without first colon separates part 1 , 2, otherwise untouched.

the problem here input lines contain more 1 colon, resulting in getting split array more 2 elements. approach above, ignore after second colon.

just use 3rd argument match():

$ echo 'one:two:three:four' | awk ' match($0,/([^:]+):(.*)/,a) {     system("echo part 1: "a[1])     print "part 2: "a[2] }' part 1: 1 part 2: two:three:four 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -