0

I am a new to shell script. This is just a variation of some previous questions, but I still cannot find answers. I have the following txt file (one line and no space):

;100=Raspberry;101=Apple;102=Orange;103=Kiwi;104=grape;

I like to pick anything with 101=* or 103=* as an output. So the output needs to look like the following:

;101=Apple;103=Kiwi

I was trying to write a command by modifying

grep -o -m 1 ';101=.*;\|;103=.*' file.txt

But it picks everything after 101=.*. I know that's what the commands says, but still not sure how to change it. I am using Ubuntu 16.

2 Answers 2

2

.* tries to match as many characters as possible, that's why you get the rest of the line. So instead of . (any char) you have to use [^;] (any char except the semicolon):

grep -o ';101=[^;]*\|;103=[^;]*'

I'm not sure what to tried to achieve with -m 1 in a one-liner. Anyhow you need to combine your output in one line to get the desired result. But I bet you can find out how to do it yourself (in the end it's your exercise, not ours).

0

I have used below awk command to achieve the same

 awk -F ";" '{print ";"$3";"$5}' filename

output

;101=Apple;103=Kiwi

You must log in to answer this question.

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