How to find all matches of IP v4 addresses and subnets in a text file and print matches line by line with given prefix using only one command?

Viewed 79

I would like to replace this pipeline of three commands with one. I think that it is possible to achieve a similar result with only awk or sed.

Current solution with cat, greep and awk

cat textfile | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}(\/[0-9]{1,2}|)" | awk '{print "prefix "$0}'

Text file example:

192.168.1.1 10.100.20.0/24 some text  2a05:d014:d13:26aa:f493:ef87:bb60:d85f
10.15.12.11, text "10.10.0.0/16" =25.0.0.0/12 etc

Output:

prefix 192.168.1.1
prefix 10.100.20.0/24
prefix 10.15.12.11
prefix 10.10.0.0/16
prefix 25.0.0.0/12
2 Answers

For any given shell command, cat file | command can always be replaced with command file if the command can take a file argument or command < file (or the equivalent < file command if you like specifying the file name to the left of the command rather than to the right of it) no matter whether the command can take a file argument or not.

Using GNU awk for multi-char RS and RT:

$ awk -v RS='([0-9]{1,3}[.]){3}[0-9]{1,3}(/[0-9]{1,2})?' 'RT{print "prefix", RT}' file
prefix 192.168.1.1
prefix 10.100.20.0/24
prefix 10.15.12.11
prefix 10.10.0.0/16
prefix 25.0.0.0/12

or with any awk:

$ awk '{
    while ( match($0,"([0-9]{1,3}[.]){3}[0-9]{1,3}(/[0-9]{1,2})?") ) {
        print "prefix", substr($0,RSTART,RLENGTH)
        $0 = substr($0,RSTART+RLENGTH)
    }
}' file
prefix 192.168.1.1
prefix 10.100.20.0/24
prefix 10.15.12.11
prefix 10.10.0.0/16
prefix 25.0.0.0/12

The only changes I made to your regexp were cleanup rather than functional - you don't need to escape . within a bracket expression nor / in a regexp (unless you're using /s as the regexp delimiters which we aren't) and I just think (/[0-9]{1,2})? is clearer than (/[0-9]{1,2}|)

Using sed

$ sed -z 's/["=]\?\([0-9][^[:space:],"]*\)[a-z ",\n]*/prefeix \1\n/g' input_file
prefeix 192.168.1.1
prefeix 10.100.20.0/24
prefeix 10.15.12.11
prefeix 10.10.0.0/16
prefeix 25.0.0.0/12
Related