Convert lines to tab separated columns

Hello Dear All,
Operating system: Ubuntu 20.04
My question is about how to replace \nhttp by \thttp
Sample file:

Client1
http://dd.ff.gg
user1:pass1

Client2
http://aa.bb.cc
user2:pass2

Client3
http://xx.yy.zz
user3:pass3

This is what I am trying to do:
Get the line starting with http, move to previous line ( tab separated)
Get next line coming after http and locate it into the same line of http (tab separated)

Desired Output (tab separated):

Client1 http://dd.ff.gg user1:pass1
Client2 http://aa.bb.cc user2:pass2
Client3 http://xx.yy.zz user3:pass3

As far as I remember, it's not possible to do it with sed command.
Also I could not have found the correct notation to do it with tr
My last attempt was:

paste -s -d '\t\t\n' input_file > output_file

I'd appreciate if you could help me out.

Thank you so much
Boris

Well, almost, because it's:

paste -s -d '\t\t\t\n' input_file > output_file

But it leaves a trailing \t at the end of each (except for the last) line.

2 Likes
$ awk '$1=$1' RS=  OFS='\t' myInput.txt
Client1 http://dd.ff.gg user1:pass1
Client2 http://aa.bb.cc user2:pass2
Client3 http://xx.yy.zz user3:pass3
4 Likes

I see that you have a solution, but another option that I've found useful is to use awk to match parts of (relatively) fixed parts of lines (records) and upon match store other parts of the line (record) into a variable. Have a different match for each type of line and then when you see a blank line (and END{...}) print the variables in the form that you want. -- You need to do one last print in the END{...} block because of the last set not having a blank line to cause the print.

awk '($1 ~ /^Client/){CLIENT=$1}($1 ~ /^http:/){URL=$1}($1 ~ /:/ && $1 !~ /^http:/){USER=$1}($0==""){print CLIENT,URL,USER}END{print CLIENT,URL,USER}'

One of the nice things about using awk to do this is that you can do some computation / manipulation / processing on things more than just basic text manipulation.

1 Like

a thing of beauty