Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have this input file in ubuntu:

146.14.142.96.17747 197.102.40.184.13748:
146.14.142.96.17747 197.102.40.184.13749: 
146.14.142.96.17747 197.102.40.184.13750:
146.114.142.96.17747 197.102.40.184.13751:
46.14.142.96.17747 197.102.40.184.13752:

and I'd like to have the output like this using shell scripting:

separate two IPs without ports number I mean delete ports

146.14.142.96 197.102.40.184
146.14.142.96 197.102.40.184 
146.14.142.96 197.102.40.184
146.114.142.96 197.102.40.184
46.14.142.96 197.102.40.184
share|improve this question
    
In linux, use "sed" tool with a grep. –  Diogo Apr 25 '13 at 16:55
    
@Diogo tnx. i'm not expert in regular expression –  Christopher Apr 25 '13 at 16:56

2 Answers 2

up vote 1 down vote accepted

For lines formatted exactly as shown in the question, this will do:

sed -E 's/\.[0-9]+[ :]/ /g' input-file

How it works:

  • The -E switch enables Extended Regular Expressions.

  • s/SEARCH/REPLACE/g globally (/g) replaces (s/) SEARCH with REPLACE.

  • \.[0-9]+[ :] matches a dot following any positive number of digits following a space or a colon.

However, this will break if the formatting varies even slightly. This approach may result robuster:

sed -E 's/(([0-9]+\.){3}[0-9]+)[^ ]+/\1/g' input-file

How it works:

  • ([0-9]+\.){3}[0-9]+ matches an IP (three digit groups followed by dots plus an additional digit group).

  • The surrounding parentheses declare the previous match as the first submatch (\1).

  • [^ ]+ matches any non-space character that follows the IP.

share|improve this answer
    
this is not working in sum ips it cuts the ip. –  Christopher Apr 25 '13 at 17:16
    
181.173.82.61 250.66.33.195 181.173.82.60 250.66.33.195 181.173.82 229.96.193 181.173.83 245.228.178 181.173.82.61 250.66.33.195 181.173.82.60 250.66.33.195 172.30.79 247.236.141 –  Christopher Apr 25 '13 at 17:16
    
172.30.79 247.236.141 this is not valid ip –  Christopher Apr 25 '13 at 17:17
1  
/ / replaces the first match with a space. / /g replaces all matches with a space. –  Dennis Apr 25 '13 at 17:44
1  
Is it intentional that HTTP/1.1 OK doesn't contain the status code? If it isn't grep 'HTTP/1.[01] 200 OK' will do. –  Dennis Apr 28 '13 at 0:12

Do a search and replace using regex

(\d+\.\d+\.\d+\.\d+)\.\d+(:?)

and replace text

\1

Various tools support regex search and replace though the dialect can be slightly different. The above works with Notepad++.

Or in vim you can do

:s/\(\d\+\.\d\+\.\d\+\.\d\+\)\.\d\+\(:\?\)/\1/g
share|improve this answer
    
this is not working grep '(\d+\.\d+\.\d+\.\d+)\.\d+(:?)' –  Christopher Apr 25 '13 at 16:57
    
for 1 ip i use this cut -d "." -f -4 and its working –  Christopher Apr 25 '13 at 16:59
    
but for 2 IPs i dont know what to do :( –  Christopher Apr 25 '13 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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