my question is regarding text processing:

In a list I have the IP addresses and computer names in the following format:

IP address:
Computer name: computer7office
IP address:
Computer name: computer22office
IP address:
Computer name: computer12personal

The output I need:

This computer ip address is xxx.xxx.x.xx and is under the name zzzzzzzzzz

How can I automatically copy the IPs and names from the list to the output file using the command line? Could you please explain your command because it is a pity when I have to copy/paste things that I don't understand.


In sed, assuming your list is in a file called file, you could use:

sed -n '/ss: /N;s/\n//;s/IP address:/This computer ip address is/;s/Computer name:/ and is under the name/p' file
  • -n don't print anything until we ask for it
  • /ss: / find the pattern ss: (to match the lines with IP address:)
  • N read the next line too so we can join them
  • ; separates commands, like in the shell
  • s/old/new/ replace old with new
  • s/\n// delete the newline between the two lines
  • p print the lines we've worked on

When you see what you want, repeat the command adding > newfile at the end of it to write the modified file to newfile

More readably:

sed -n '{
    /ss: /N
    s/IP address:/This computer ip address is/
    s/Computer name:/ and is under the name/p
}' file | tee newfile

(tee helpfully writes to the newfile and displays the output at the same time)

  • Your code works great! Now I realize that I did something stupid, I forgot to mention (I am so sorry for that!) that the list contains also the date/name of the scanner/domain name and other things. Thus, the real list has more information and the only things I need are IP/Computer name. Is there a way to delete all other irrelevant information and only keep these? Maybe first we can take the needed information to another file using grep or something and then write your command on the new file? Dec 17 '16 at 22:38
  • @Never.too.old.to.learn hey no worries :) I have edited my answer (sed has a helpful option) but please let me know if this does not fix it - you can edit your question or paste up a more complete sample of the file and post a link so I can see exactly what is happening, if necessary
    – Zanna
    Dec 17 '16 at 22:46

There are probably a dozen ways to do this, using various text-processing utilities (awk, perl) and/or stream editors (sed, ed)

One way would be to cut the list at the colon delimiters (-d:), retaining only the second field (-f2), and then use xargs to pass pairs of lines (-l2) as arguments to printf:

$ cut -d: -f2 list.txt | xargs -l2 printf 'This computer ip address is %s and is under the name %s\n'
This computer ip address is and is under the name computer7office
This computer ip address is and is under the name computer22office
This computer ip address is and is under the name computer12personal
  • Thank you very much! Your code is great! I forgot to mention (thought it is not important, my bad, sorry!) that the list contains also the date / scanner name / and domain. Thus, the list generated was a little bit chaotic Dec 17 '16 at 22:40

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.