my question is regarding text processing:
In a list I have the IP addresses and computer names in the following format:
IP address: 192.168.1.25
Computer name: computer7office
IP address: 192.168.1.69
Computer name: computer22office
IP address: 192.168.1.44
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.
Best Answer
In
sed
, assuming your list is in a file calledfile
, you could use:-n
don't print anything until we ask for it/ss: /
find the patternss:
(to match the lines withIP address:
)N
read the next line too so we can join them;
separates commands, like in the shells/old/new/
replaceold
withnew
s/\n//
delete the newline between the two linesp
print the lines we've worked onWhen you see what you want, repeat the command adding
> newfile
at the end of it to write the modified file tonewfile
More readably:
(
tee
helpfully writes to thenewfile
and displays the output at the same time)