0

I have a log that returns thousands of lines of data, I want to extract a few values from that.

In the log there is only one line containing the unquie unit reference so I can grep for that using:

grep "unit=Central-C152" logfile.txt

That produces a line of output similar to the following:

a3cd23e,85d58f5,53f534abef7e7,unit=Central-C152,locale=32325687-8595-9856-1236-12546975,11="School",1="Mr Green",2="Qual",3="SWE",8="report",5="channel",7="reset",6="velum"

The format of the line may change in that the order of the values won't always be in the same position.

I'm trying to work out how to get the value of 2 and 7 in to separate variables. I had thought about cut on , or = but as the values aren't in a set order I couldn't work out that best way to do it.

I' trying to get:

var state=value of 2 without quotes var mode=value of 7 without quotes

Can anyone advise on the best way to do this ?

Thanks

3 Answers 3

2

Could you please try following to create variable's values.

state=$(awk '/unit=Central-C152/ && match($0,/2=\"[^"]*/){print substr($0,RSTART+3,RLENGTH-3)}' Input_file)
mode=$(awk '/unit=Central-C152/ && match($0,/7=\"[^"]*/){print substr($0,RSTART+3,RLENGTH-3)}'  Input_file)

You could print them too by doing following.

echo "$state"
echo "$mode"

Explanation: Adding explanation of command too now.

awk '                                           ##Starting awk program here.
/unit=Central-C152/ && match($0,/2=\"[^"]*/){   ##Checking condition if a line has string (unit=Central-C152) and using match using REGEX to check from 2 to till "
  print substr($0,RSTART+3,RLENGTH-3)           ##Printing substring starting from RSTART+3 till RLENGTH-3 characters.
}
' Input_file                                    ##Mentioning Input_file name here.
Sign up to request clarification or add additional context in comments.

Comments

1

You are probably better off doing all of the processing in Awk.

awk -F, '/unit=Central-C152/ {
    for(i=1;i<=NF;++i)
        if($i ~ /^[27]="/) {
            b[++k] = $i
            sub(/^[27]="/, "", b[k])
            sub(/"$/, "", b[k])
            gsub(/\\/, "", b[k])
        }
    print "state " b[1] ", mode " b[2]
    }' logfile.txt

This presupposes that the fields always occur in the same order (2 before 7). Maybe you need to change or disable the gsub to remove backslashes in the values.

If you want to do more than print the values, refactoring whatever Bash code you have into Awk is often a better approach than doing this processing in Bash.

Comments

1

Assuming you already have the line in a variable such as with:

line="$(grep 'unit=Central-C152' logfile.txt | head -1)"

You can then simply use the built-in parameter substitution features of bash:

f2=${line#*2=\"} ; f2=${f2%%\"*} ; echo ${f2}
f7=${line#*7=\"} ; f7=${f7%%\"*} ; echo ${f7}

The first command on each line strips off the first part of the line up to and including the <field-number>=". The second command then strips everything off that beyond (and including) the first quote. The third, of course, simply echos the value.

When I run those commands against your input line, I see:

Qual
reset

which is, from what I can see, what you were after.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.