Compress spaces between columns in a Unix shell

I want the spaces to be removed between the two columns. After running the sql query from the shell, I get the output, as shown 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 result should look like this:

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 that the desired output should have only one tablespace between two columns.

+4
source share
3 answers

Ease of use awk:

awk -v OFS='\t' '{$1=$1} 1' file
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;

As an alternative, this will also work tr:

tr -s ' ' < file | tr ' ' '\t'

or is it sed:

sed -i.bak $'s/ \{1,\}/\t/g' file
+1
source

how about the next perlone line?

perl -ne '/(.*?)\s+(.*)/; print "$1\t$2\n"' your_input_file
+1
source

, :

. g, .

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

-r :

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

or if you need a single space between each multi-space

tr -s ' '

+1
source

Source: https://habr.com/ru/post/1678763/


All Articles