How to make grep for a regex that appear multiple times in a line
6
votes
5
answers
2245
views
I want to grep a regex. The pattern I am searching for may appear multiple times in a line. If the pattern appeared many times, I want to separate each occurrence by a comma and print **the match only** not the full line in a new file. If it did not appear in a line I want to print **n.a.**
Example. I want to use this regex to find numbers in the pattern: [12.123.1.3].
grep -oh "\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]" 'filename'
input file (input.txt)
blabla [11.335.2.33] xyuoeretrete [43.22.11.88] jfdfjkfbs [55.66.77.88]
blabla [66.223.44.33]
foo bar
blabla [1.2.33.3] xyuoeretrete bla[1.32.2.4]
intended result in a new file (output.csv):
11.335.2.33,43.22.11.88,55.66.77.88
66.223.44.33
n.a.
1.2.33.3,1.32.2.4
**Note: I use Ubuntu**
Asked by randomname
(161 rep)
Jun 24, 2022, 09:08 AM
Last activity: Jun 24, 2025, 09:28 AM
Last activity: Jun 24, 2025, 09:28 AM