linux - AWK to to find first occurrence of string and assign to variable for compare -


i have written following line of code explodes string first occurrence of string after delimiter.

echo "$line" | awk -f':' '{ st = index($0,":");print "field1: "$1 "     =>  " substr($0,st+1)}'; 

but don't want display it. want take both occurrences in variable tried following code

explodetext="$line" | awk -f':' '{ st = index($0,":")}'; 

sample data:

id:1 url:http://test.com 

expected output be:

key=id val=1  key=url val=http://test.com 

but not working expected.any solution?

thanks

your code, expanded:

echo "$line" \ | awk -f':' '     {       st = index($0,":")       print "field1: " $1 " => " substr($0,st+1)     }' 

the output of appears merely split line according first colon. sample data you've provided, seems lines contain 2 fields, separated first colon found. means can't safely use awk's field separator find data (though can use field names), making index() reasonable approach.

one strategy might place input array, assessment:

#!/usr/bin/awk -f  begin {   fs=":" }  {   record[$1]=substr($0,index($0,":")+1); }  end {   if (record["id"] > 0) {     printf("record id %d had value of %s.\n", record["id"], record["url"])   } else {     print "no valid records found."   } } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -