Parsing line with name:value pairs in shell script

HI ,
I have the following type of lines in a file and need to create a csv file that can be bcp'ed into a db

The problem that I have is the delimited of the <name :value> is a space but some of the values in the pairs have space . eg msg_src_time:03/05/13 10:40:17.919

Need sugesstions on parsing the file

sample input

INPUT

msg_status:FILL comments: usr_id:73 system_time:03/05/13 10:40:17.920 msg_src_time:03/05/13 10:40:17.919 flags: 0 flags2: 0 comment_index: 0
msg_status:FILL comments:dollar usr_id:73 system_time:03/05/13 10:40:51.352 msg_src_time:03/05/13 10:40:51.352 flags: 0 flags2: 0 comment_index: 0

Output

FILL ,  , 73 , 03/05/13 10:40:17.920 , :03/05/13 10:40:17.919 , 0 , 0 , 0
FILL , dollar , 73 , 03/05/13 10:40:51.352 , :03/05/13 10:40:51.352 , 0 , 0 , 0

My intial search in this forum found these but this will fail as my delimited is a space and 2 name value pairs have space in them

echo "Type:G,Instance:instance1,FunctionalID:funcid,Env:dev,AppName:appname" | sed 's/:/=/g' | awk 'BEGIN{FS=","} { print $2 print $3 print $4 print $5 }

How about:

awk -F'[[:alnum:]]*[_[:alpha:]]+[[:alnum:]]*:' '{$1=$1; sub(OFS,x)}1' OFS=" , " file

output:

FILL  ,   , 73  , 03/05/13 10:40:17.920  , 03/05/13 10:40:17.919  ,  0  ,  0  ,  0
FILL  , dollar  , 73  , 03/05/13 10:40:51.352  , 03/05/13 10:40:51.352  ,  0  ,  0  ,  0
1 Like

I get the following error when i run the above line

>awk -F'[[:alnum:]]*[_[:alpha:]]+[[:alnum:]]*:' '{$1=$1; sub(OFS,x)}1' OFS=" , " in.trade
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
awk -F"[a-z_]+([0-9])?:" '{for(i = 1; i <= NF; i++) printf("%s", $i == "" ? $i : $i "" (i == NF ? "\n" : ","))}' file

I get a similar error . I am using bash and other awk commands work

$>awk -F"[a-z_]+([0-9])?:" '{for(i = 1; i <= NF; i++) printf("%s",  $i == "" ? $i : $i "" (i == NF ? "\n" : ","))}' in.trade
awk: syntax error near line 1
awk: illegal statement near line 1

What is your OS?

1 Like
$>uname -a
SunOS gatxdev2 5.10 Generic_147441-19 i86pc i386 i86pc
$>awk 1 /dev/null
awk: syntax error near line 1
awk: bailing out near line 1
$>

Looks like I am using the old awk

according to Unix shell - View topic - How to find the version of awk?

awk is broken in SunOS / Solaris. Use nawk instead.

1 Like

tried with nawk and it works ...

---------- Post updated at 05:54 PM ---------- Previous update was at 05:54 PM ----------

@shamrock

if I use nawk it works. Thx

Although nawk works in many cases, it is usually preferable to use /usr/xpg4/bin/awk on Solaris, since it a POSIX compliant version.

$ /usr/xpg4/bin/awk -F'[[:alnum:]]*[_[:alpha:]]+[[:alnum:]]*:' '{$1=$1; sub(OFS,x)}1' OFS=" , " file
FILL  ,   , 73  , 03/05/13 10:40:17.920  , 03/05/13 10:40:17.919  ,  0  ,  0  ,  0
FILL  , dollar  , 73  , 03/05/13 10:40:51.352  , 03/05/13 10:40:51.352  ,  0  ,  0  ,  0