I have a few hundred lines like below in connRefused.log:-

        2015-12-12 00:12:10,227 ERROR [Testing-KeepAlive-01] c.v.v.v.Connection [Connection.java : 001] failed to bind to {name=TestGW,direction=BOTH_WAY,username=espada,password=whatever,binds=1,keepAliveInterval=60000,params={Payload=0, useEXP=1},thisOne={id=1001,name=TestGw,ip=,port=88}}: Connection refused

and below is part of my script (simplified) to read the connRefused.log into array

IFS=$'\n' read -d '' -r -a lines < /path/log/connRefused.log
for xx in "${lines[@]}"
    ??? # what to do here?
    echo $Date
    echo $ID
    echo $Name
    echo $IP
    echo $Port

How can I take the data I need from the line above to store it in variable Date,ID,Name,IP,Port?


and for $Date, I only need time part.

  • The post is a bit confusing. Try to clarify it, one has to reread it several times to have an idea of what you are asking, and even then... Dec 16, 2015 at 4:09

1 Answer 1


You don't need to use an array. Since the input data appears to be very regular, I would convert the input data into shell assignment statements, then read them into the shell and evaluate. Like this:


sed '
    s/^[-0-9]*  */date=/
    s/,.*thisOne={/ /
    s/,/ /g
' "$@" |
while read line
    eval $line
    echo date=$date
    echo id=$id
    echo name=$name
    echo ip=$ip
    echo port=$port

The sed command converts the input line into this:

date=00:12:10 id=1001 name=TestGw ip= port=88

The while loop reads one such line at a time and eval $line causes the line to be executed into the shell, which results in the variables being set to the given values.

The script will process file names from the command line OR standard input (note the "$@" at the end of the sed command).

The sed command converts the line into shell assignment statements via a series of s (substitute) commands:

  1. Replace, only at the beginning of the line (^), any sequence of dashes and digits ([-0-9]*), followed by one or more spaces (*) with date=:

    s/^[-0-9]*  */date=/
  2. Replace a comma followed by any characters (.*) followed by thisOne= with a space:

    s/,.*thisOne={/ /
  3. Delete a closing brace (}) followed by any other characters (.*) to the (implied) end of the line:

  4. Replace all commas (,) with spaces:

    s/,/ /g

In the example script, I recommend temporarily deleting the pipe | to the end of the file and running only the sed command portion of the script so you can experiment and see how it works.

  • Can u advise how to convert the input data into shell assignment statements specifically?
    – Asif
    Dec 16, 2015 at 7:06
  • @Asif please see the additions I've made to the answer.
    – RobertL
    Dec 16, 2015 at 19:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .