Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 – Arash Apr 25 '13 at 16:56
up vote 3 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. – Arash 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 – Arash Apr 25 '13 at 17:16
    
172.30.79 247.236.141 this is not valid ip – Arash 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+(:?)' – Arash Apr 25 '13 at 16:57
    
for 1 ip i use this cut -d "." -f -4 and its working – Arash Apr 25 '13 at 16:59
    
but for 2 IPs i dont know what to do :( – Arash Apr 25 '13 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .