Skip to content
Advertisement

Grouping some lines into one

I have an hosts file and I need to group multiple lines into one, using Linux standard commands; starting with this:

127.0.0.1 name1
127.0.0.1 name2
127.0.0.1 name3
...
127.0.0.1 nameN

I want this:

127.0.0.1 name1 name2 name3
127.0.0.1 name4 name5 name6
...
127.0.0.1 ... nameN

The ip address is always the same, and I can easily remove it to simplify the work; the file can reach 30K+ lines, so my goal is to reduce its size using as few resources as possible (I’m working on a router with OpenWRT). Using tail and head I can get a group of lines, but then I don’t know how to put them on a single line (substite n with a space doesn’t seem to work).

Advertisement

Answer

How about this:

(sed 's/127.0.0.1 //' | fmt -w 25 | sed 's/^/127.0.0.1 /') < inputfile > outputfile
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement