Equally Tab Spaced Columns Through sed

  • Follow


Hi!

I have a somehow long file (almost 3000 lines) and I would like to
space each column using tabs, but I can't figure how to do that
through sed. Here you are an excerpt from the data I have:

Madagascar	1995	51.6
Malawi	1995	54.7
Saint_Vincent_and_the_Grenadines	2010	66.9
Samoa	2010	60.4
S=E3o_Tom=E9_and_Pr=EDncipe	2010	48.8
Saudi_Arabia	2010	64.1

I would like that each line could be as follows:

Madagascar			1995	51.6
Malawi				1995	54.7
Saint_Vincent_and_the_Grenadines	2010	66.9
Samoa				2010	60.4
S=E3o_Tom=E9_and_Pr=EDncipe		2010	48.8
Saudi_Arabia			2010	64.1

As you can see, the problem is the first column with countries' names.

How could I do that using sed?

Many thanks in advance!

Marcelo
0
Reply Nosophorus 9/5/2010 7:52:23 PM

Hi!

Please. click on the link below to see how the data should be:

http://img39.imageshack.us/img39/2898/image2vw.png

Google Groups converts tabs to spaces. Sorry for the mess in the
previous post.

Thank You!

Marcelo
0
Reply Nosophorus 9/5/2010 8:01:06 PM


On 2010-09-05, Nosophorus <nosophorus@gmail.com> wrote:
> Hi!
>
> I have a somehow long file (almost 3000 lines) and I would like to
> space each column using tabs, but I can't figure how to do that
> through sed. Here you are an excerpt from the data I have:
>
> Madagascar	1995	51.6
> Malawi	1995	54.7
> Saint_Vincent_and_the_Grenadines	2010	66.9
> Samoa	2010	60.4
> S�o_Tom�_and_Pr�ncipe	2010	48.8
> Saudi_Arabia	2010	64.1
>
> I would like that each line could be as follows:
>
> Madagascar			1995	51.6
> Malawi				1995	54.7
> Saint_Vincent_and_the_Grenadines	2010	66.9
> Samoa				2010	60.4
> S�o_Tom�_and_Pr�ncipe		2010	48.8
> Saudi_Arabia			2010	64.1
>
> As you can see, the problem is the first column with countries' names.
>
> How could I do that using sed?
>
> Many thanks in advance!
>
> Marcelo

s/  */	/g
two spaces after the first slash, a tab after the second.  Or instead of sed,
you can use awk and printf, if what you really want are spaces to make the
columns line up.



-- 
CEOs are paid hundreds of times what the workers earn, plus million dollar
bonuses, and when the company goes bankrupt, they blame labor unions.
0
Reply Bill 9/5/2010 10:21:03 PM

On 9/5/2010 2:52 PM, Nosophorus wrote:
> Hi!
>
> I have a somehow long file (almost 3000 lines) and I would like to
> space each column using tabs, but I can't figure how to do that
> through sed.

Do you really NEED the solution to be sed or is sed just the tool you think you 
might have to use?

	Ed.

  Here you are an excerpt from the data I have:
>
> Madagascar	1995	51.6
> Malawi	1995	54.7
> Saint_Vincent_and_the_Grenadines	2010	66.9
> Samoa	2010	60.4
> S�o_Tom�_and_Pr�ncipe	2010	48.8
> Saudi_Arabia	2010	64.1
>
> I would like that each line could be as follows:
>
> Madagascar			1995	51.6
> Malawi				1995	54.7
> Saint_Vincent_and_the_Grenadines	2010	66.9
> Samoa				2010	60.4
> S�o_Tom�_and_Pr�ncipe		2010	48.8
> Saudi_Arabia			2010	64.1
>
> As you can see, the problem is the first column with countries' names.
>
> How could I do that using sed?
>
> Many thanks in advance!
>
> Marcelo

0
Reply Ed 9/6/2010 12:11:50 AM

Hi!

Marcum, I tried your suggestion and it did not work. I used the sed
command you told ( sed "s/  */\t/g" ) and got this as a result:

http://img812.imageshack.us/img812/2535/imagesq.png

As you can see, the columns are not equally tab spaced. Maybe, the
word "equally" is not well suited for what I really want. What I want
is something like this:

http://img810.imageshack.us/img810/9004/image2i.png

A nicely tab spaced set of columns. Don't worry about the numbers.

Ed, you may give the reply in another bash magic if you want to. Don't
hesitate to do that.

Thank you so much for your replies!

Marcelo
0
Reply Nosophorus 9/6/2010 12:44:33 AM

Nosophorus <nosophorus@gmail.com> writes:

> What I want is something like this:
>
> http://img810.imageshack.us/img810/9004/image2i.png

Since you clearly care about the horizontal position, why are you
looking for a solution involving U+0009 HORIZONTAL TAB characters? They
are notorious for inconsistent presentation in different environments.

You might have a good reason for specifying U+0009, but you'll need to
share that with us.

> A nicely tab spaced set of columns. Don't worry about the numbers.
>
> Ed, you may give the reply in another bash magic if you want to. Don't
> hesitate to do that.

It's not clear whether your input data is delimited with U+0020, U+0009,
or some other delimiter. I'll assume U+0009.

You can use ‘awk(1)’ for tasks like this, where you want control over
how the input lines is mogrified and formatted for output lines. I'll
assume you want the first column left-justified within 40 characters,
and the subsequent columns right-justified within 8 characters.

=====
$ cat /tmp/foo.txt
Madagascar      1995    51.6
Malawi  1995    54.7
Saint_Vincent_and_the_Grenadines        2010    66.9
Samoa   2010    60.4
São_Tomé_and_Príncipe   2010    48.8
Saudi_Arabia    2010    64.1

$ cat /tmp/foo.txt | awk '
    {
        first_column_format = "%-40s";
        subsequent_column_format = "%8s";
        printf(first_column_format, $1);
        for (f = 2 ; f <= NF ; f++) {
            printf(subsequent_column_format, $f);
        };
        printf("\n");
    }'
Madagascar                                  1995    51.6
Malawi                                      1995    54.7
Saint_Vincent_and_the_Grenadines            2010    66.9
Samoa                                       2010    60.4
São_Tomé_and_Príncipe                       2010    48.8
Saudi_Arabia                                2010    64.1
=====

The AWK program can, of course, be put into a separate file (e.g.
‘foo.awk’) and invoked with ‘awk -f foo.awk’.

-- 
 \       “The generation of random numbers is too important to be left |
  `\                                    to chance.” —Robert R. Coveyou |
_o__)                                                                  |
Ben Finney
0
Reply Ben 9/6/2010 2:24:34 AM

On Sun, 05 Sep 2010 12:52:23 -0700, Nosophorus wrote:
> I have a somehow long file (almost 3000 lines) and I would like to space
> each column using tabs, but I can't figure how to do that through sed.
> Here you are an excerpt from the data I have:
> 
> Madagascar	1995	51.6
> Malawi	1995	54.7
> Saint_Vincent_and_the_Grenadines	2010	66.9 
> Samoa	2010	60.4
> São_Tomé_and_Príncipe	2010	48.8
> Saudi_Arabia	2010	64.1
> 
> I would like that each line could be as follows:
> 
> Madagascar			1995	51.6
> Malawi				1995	54.7
> Saint_Vincent_and_the_Grenadines	2010	66.9
> Samoa				2010	60.4
> São_Tomé_and_Príncipe		2010	48.8
> Saudi_Arabia			2010	64.1
[...]
> How could I do that using sed?

If you align with spaces rather than tabs, you could use 'column' 
instead of the awk program that was posted a couple of months ago.
(Man page column(1) says column command appeared in 4.3BSD-Reno.)

Eg:       column -t somedata
will treat each line from file somedata as a whitespace-delimited 
set of fields, and print fields in aligned columns; eg:
Madagascar                        1995  51.6
Malawi                            1995  54.7
Saint_Vincent_and_the_Grenadines  2010  66.9
Samoa                             2010  60.4
São_Tomé_and_Príncipe             2010  48.8
Saudi_Arabia                      2010  64.1

-- 
jiw
0
Reply James 11/8/2010 8:29:34 PM

Ben Finney <ben+u...@benfinney.id.au> wrote:

> $ cat /tmp/foo.txt | awk '
>     {
>         first_column_format = "%-40s";
>         subsequent_column_format = "%8s";
>         printf(first_column_format, $1);
>         for (f = 2 ; f <= NF ; f++) {
>             printf(subsequent_column_format, $f);
>         };
>         printf("\n");
>     }'

Hideous.  Let's pretend we know something about the language:

awk '{ printf "%-40s%s%8s\n", $1, $2, $3 }' file

0
Reply w_a_x_man 11/9/2010 6:09:12 AM

7 Replies
899 Views

(page loaded in 0.098 seconds)

Similiar Articles:








7/20/2012 5:28:37 PM


Reply: