I have this output from a find command:


where abc,def and geh are device names and could be of any length and others are IP address belong to devices. Likeabc, for device abc. IP shall be next to comma delimiter.

How can I use sed, grep or awk to print the associated IP when I grep for a device name? In short I want the IP to be displayed next to device name.


I am more comfortable with awk, so I would like to present two solutions in awk.

Solution 1

$ echo abc,,def,,geh, | awk -F, '{for (i=1; i<NF; i+=2) if ($i == "def") print $(i+1)}'

In this case, I am looking for a machine name "def", if found, print the next column.

Solution 2

$ echo abc,,def,,geh, | tr , \\n | awk '/def/ {getline;print}'

In this solution, I use the tr command to convert commas to new line, search for "def" and print the line that follows. I hope these solutions work for you.

  • Thanks you so much Mr Hai Vu for your kind assitance both the solution worked in my case.... – user28011 Nov 29 '12 at 9:29

Assuming you have GNU grep with Perl-compatible regular expressions:

grep -oP '(?<=\b'"$device"',).*?(?=,|$)'

Which means: find the content between "def," on one side and "a comma or end-of-line" on the other

EDIT: added a word boundary to the RE to find the given device more precisely.

  • What if there's both a def and xdef device? – Stéphane Chazelas Nov 29 '12 at 21:35
  • @StephaneChazelas (?:(?<=,def,)|(?<=^def,)) – Gilles Nov 29 '12 at 22:43

Without using any command but shell builtins:


case $s in
  (*,"$dev",*) ip=${s#*",$dev,"}; ip=${ip%%,*};;
  (*) ip=unknown

printf '%s\n' "$ip"

With zsh, it's easy to map that string to an associative array:

typeset -A ip
print -r -- $ip[def]

Using grep(GNU) and cut:

$ echo abc,,def,,geh, | grep -o 'abc,[^,]*'  | cut -d, -f2
  • Thanks for the response Guru but its showing for a .... What is the significance of a ? – user28011 Nov 29 '12 at 9:31
  • ohh..i have this habit of using filenames like a, b , etc..its just the file name..removing it.... – Guru Nov 29 '12 at 9:34
  • You command has a double quote " that should not be there. – Hai Vu Nov 29 '12 at 14:19
  • the problems of editing at the last minute :) thanks Hai Vu. – Guru Nov 29 '12 at 14:21

Substitute every IP-address [0-9\.]\{7,15\} using sed. An IP address consists of numbers and dots, varying from 7 to 15 positions.

Replace every IP-address with itself followed by a newline \n, which basically means that every device,IP-combination will have its own line.

echo "abc,,def,,geh," | sed 's/\([0-9\.]\{7,15\}\),/\1\n/g' 

echo "abc,,def,,geh," | sed 's/\([0-9\.]\{7,15\}\),/\1\n/g'  | grep def

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.