Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Squeezing spaces between columns in Unix shell

Tags:

linux

shell

unix

sh

I want the spaces to be removed between two columns. After running a sql query from shell, I'm getting the output as below:

23554402243                       0584940772;2TZ0584940772001U;
23554402272                       0423721840;7TT0423721840001B;
23554402303                       0110770863;BBTU500248822001Q;
23554402305                       02311301;BTB02311301001J;
23554402563                       0550503408;PPTU004984208001O;
23554402605                       0457553223;Q0T0457553223001I;
23554367602                       0454542427;TB8U501674990001V;
23554378584                       0383071261;HTHU500374797001Y;
23554404965                       059792244;ST3059792244005C;
23554405503                       0571632586;QTO0571632586001D;

But the desired output should be like below:

23554400043     0117601738;22TU003719388001V;
23554402883     0823973229;TTT0823973229001C;
23554402950     024071080;MNT024071080001D;
23554405827     0415260614;TL20415260614001R;
23554405828     08119270800;TL2U003010407001G;
23554406553     011306895;VBT011306895001E;
23554406557     054121509;TL2054121509001M;
23554406563     065069209;TL2065069209005M;
23554409085     0803434328;QTO0803434328001B;
23553396219     062004063;G6T062004063001C;

Remember, there should be only one tabspace between two columns in the desired output.

like image 746
User123 Avatar asked Oct 18 '22 11:10

User123


1 Answers

Assuming you need to remove space between all the columns:

If you need tab spaced result between first two columns. Put g to apply changes between all the columns.

sed -r 's/\s+/\t/' inputfile 

if -r is not available:

sed 's/\s\+/\t/'

or If you need single space between every multi-space

tr -s ' '

like image 85
P.... Avatar answered Nov 04 '22 01:11

P....