1

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

name1,6.5.8.12,specs1,version1,['66.55.89.12']
name2,19.13.13.159,specs2,version2,['13.13.173.59'; '14.19.232.42']
name3,141.101.196.40,specs3,version3,['11.01.196.0']

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:

6.5.8.12
66.55.89.12
19.13.13.159
13.13.173.59
14.19.232.42
141.101.196.40
11.01.196.0

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 '18 at 12:21
  • If you have a delimited file, awk is a much easier choice. – Jeff Schaller Oct 20 '18 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. – user9371654 Oct 20 '18 at 12:30
1

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
11.01.196.0
13.13.173.59
141.101.196.40
14.19.232.42
19.13.13.159
6.5.8.12
66.55.89.12

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.

1

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}'
6.5.8.12
66.55.89.12
19.13.13.159
13.13.173.59
14.19.232.42
141.101.196.40
11.01.196.0

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

0

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

First, get the required fields

$ cut -d, -f2,5 ip.txt
6.5.8.12,['66.55.89.12']
19.13.13.159,['13.13.173.59'; '14.19.232.42']
141.101.196.40,['11.01.196.0']

Then, delete all unwanted characters

$ cut -d, -f2,5 ip.txt | tr -d "]'[ "
6.5.8.12,66.55.89.12
19.13.13.159,13.13.173.59;14.19.232.42
141.101.196.40,11.01.196.0

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
11.01.196.0
13.13.173.59
141.101.196.40
14.19.232.42
19.13.13.159
6.5.8.12
66.55.89.12
0

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. – user9371654 Oct 20 '18 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.