how to match IP address with 4 octets or with 3 octets in one command?target - match  xxx.xxx.xxx or xxx.xxx.xxx.xxx ( syntax should fit for Linux and Solaris )how to merge the following commands in one command ? echo $IP_ADDRESS  | grep  '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' echo $IP_ADDRESS  | grep  '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' I also try this ( But its not work )        echo $IP_ADDRESS | egrep [0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}|[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}
linux + match IP ADDRESS with 3 octets or with 4 octets
linux;grep;solaris;regular expression;ksh
 if expr .$IP_ADDRESS : '\(\.[0-9]\{1,3\}\)\{3,4\}$' > /dev/null  then echo match else echo no match fiYou can't use grep here because grep is used to match lines, not strings. You can't use echo either as it does substitutions on its arguments. You can't leave variables unquoted either as otherwise filename generation and word splitting occurs.If you want to find the lines that match that pattern in an input file, then that's where you want to use grep. Like: grep -Ex '[0-9]{1,3}(\.[0-9]{1.3}){2,3}' < input.fileWould report all the lines that consist exactly (-x option) in a 3 or 4 up to 3-digit decimal numbers.If you want to extract all the 3 or 4 up-to-3-digit-decimal-numbers IP addresses in a text file, then probably the best option would be to resort to perl:perl -lne 'print for grep /^\d{1,3}(\.\d{1,3}){2,3}$/, /[\d.]+/g'Would output:111.222.3331.2.3.4For an input like:foo 1.1234.2.3 1.2 1.2.3.4.5 111.222.333 bar1.2.3.4@Though you could also do:tr -cs 0-9. '[\n*]' < input.file |  grep -Ex '[0-9]{1,3}(\.[0-9]{1.3}){2,3}'