I have a file (test.txt) that contains string such as:

name2,,specs2,version2,[''; '']

1) I want to capture all the strings in the 2nd and 5th columns which are formatted as four numbers separated by dots, and each number can be up to 3 digits.

2) There is one string in the 2nd column, but unlimited strings in the fifth column but they are separated by semicolon.

I tried to use this command to try to capture all fifth column strings (I still need to find out how to capture both 2nd and 5th column), but this command did not work in the first place. It produced empty file:

cat test.txt | cut -d ',' -f5 | grep -P -o '\d{1-3}\.\d{1-3}\.\d{1-3}\.\d{1-3}' > result.txt

How to capture the special format string in the 2nd and fifth column usng grep. Where the strings are four numbers (from 1 to 3 digits) separated by dots. There is only one string in the 2nd column, but unlimited strings in the 5th but separated by semicolon?

EDIT: The expected output:

Please also note that I want the results sorted and unique sort -u. I do not have repeated strings in the example, but I want to avoid repetition if found in my real file.

  • please add complete expected output for the 3 lines of sample input, it is a bit unclear what is required.. could be an interesting question..
    – Sundeep
    Oct 20, 2018 at 12:21
  • If you have a delimited file, awk is a much easier choice.
    – Jeff Schaller
    Oct 20, 2018 at 12:28
  • @Jeff Schaller file the file is comma separated. But the fifth colum contains an array of the numbers I want to extract separated by semicolon. It is a delimiter inside the delimited column. Do you get what I mean? can you see the expected output plz. Oct 20, 2018 at 12:30

4 Answers 4


Are there other fields that could hold your pattern (the sample doesn't have any)? If not, try

grep -Eo "([0-9]{1,3}\.){3}[0-9]{1,3}" file | sort -u

If you want it sorted numerically, use sort's -g option.

For your own solution given in the question, try -f2,5 for the cut fields.


Your avoidance of awk needs some rationale in the question, but anyways, here you go:

$ cut -d, -f2,5 file | egrep -wo '([0-9]{1,3}[.]){3}[0-9]{1,3}'

If you want the output sort+uniq'ed, you can add yourself a | sort -u at the end of the pipeline ;-)


Here's a step by step solution using cut+tr+sort

First, get the required fields

$ cut -d, -f2,5 ip.txt,[''],[''; ''],['']

Then, delete all unwanted characters

$ cut -d, -f2,5 ip.txt | tr -d "]'[ ",,;,

Then, translate , and ; to newline so that each value is on its own line and then sort it uniquely

$ cut -d, -f2,5 ip.txt | tr -d "]'[ " | tr ',;' '\n' | sort -u

Fixed my example, but it is not elegant.

Basically, awk separates out the fields, we use sed to get rid of unwanted chars and then we sort the output. Sorry, no grep in this answer.

cat test.txt |awk -F'[,;]' '{print $2"\n" $5}'  | sed 's/\(\[\|\]\)//g' |sed "s/'//g" |sort -r
  • Unfortunately, your answer does not consider the RegEx I need. Please, check my update. I prefer grep as much as possible. Oct 20, 2018 at 12:29

You must log in to answer this question.

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