Shell script help

I have some input, which containes some entries which are comma separated. Eg.

a,b,id=999],d
d,f,g,id=345],x
x,y,x,s,id=677],y

I run a loop to read the lines one by one. What i want is to extract the value on the right of id=. I cannnot do it by Awk, since the column number is not fixed. So what i need is to first search and match “id=” pattern, and then print the number following it, until ‘]’ is encountered.
Any way to do this?
Help will be very much appreciated.

not sure how to do this in shell. In C, you can use strstr() to search for a substring and strtok() to extract a part from the line.

You should better post this query in some linux forums. Try this:
Programming - LinuxQuestions.org

you can try using awk by extracting one column at a time and comparing it. If it doesn’t match then read the next column until all columns are read.

If expr.txt is the file you have the data in, try this

$ cat expr.txt | sed -e ‘s/^.id/id/g’ -e 's/].//g’ | awk -F= ‘{ print $2 }’

This will display all the values of id no matter where it is. I suppose the regex is self-explanatory.

Hope this helps

viridian

try to use the cut function

echo $string|cut 1- -d=|cut -1 -d]

Throws up syntax errors on my system.
Try cut --help' for more information. cut: invalid option -- 1 Try cut --help’ for more information.

oops

do this

echo $string|cut -f2- -d=|cut -f1 -d]

man for details on the switches

Yep, nukeu666’s method is far more simpler and cleaner than mine. Works great
:grinning_face_with_smiling_eyes: