Find the answer to your Linux question:
Results 1 to 7 of 7
i have a file with data in that looks something like this: Code: 7 213.105.172.13 64.139 ms 51.771 ms 51.922 ms 8 62.253.184.2 52.075 ms 52.214 ms 55.384 ms 9 ...
  1. #1
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241

    Text formating.

    i have a file with data in that looks something like this:

    Code:
    7 213.105.172.13 64.139 ms 51.771 ms 51.922 ms
    8 62.253.184.2 52.075 ms 52.214 ms 55.384 ms
    9 193.159.225.237 49.361 ms 52.573 ms 52.688 ms
    10 217.239.40.173 64.264 ms 64.204 ms 64.795 ms
    11 217.6.48.246 64.797 ms 64.983 ms 65.299 ms
    12 194.126.123.58 77.388 ms 77.950 ms 82.695 ms
    13 194.126.123.53 114.337 ms 126.156 ms 122.287 ms
    14 194.126.123.46 123.318 ms 144.008 ms 141.843 ms
    15 195.250.170.38 142.046 ms 130.792 ms 128.414 ms
    How do i go about making sure that they align up correctly column by column? so that it is easier to read on the eye..
    or if possible, would it be able to export to a openoffice spreadsheet with all data correctly in each shell....

    thanks.

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    If you converted it to CSV (comma-separated-value; by going in and using quotes to indicate what a field is, and using commas to separate fields (outside the quotes)), I am rather certain that you could import a CSV document into an OpenOffice spreadsheet. Alternatively, you could write a script to align them in columns in plain text.

    Both of these could be done in a script. You know what each column looks like, so I think that I would write a regular expression that captures the data from each line and puts each piece into the correct variable. You could then either use printf to format the output in plain text, or print out a CSV document.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241
    Quote Originally Posted by Cabhan View Post
    If you converted it to CSV (comma-separated-value; by going in and using quotes to indicate what a field is, and using commas to separate fields (outside the quotes)), I am rather certain that you could import a CSV document into an OpenOffice spreadsheet. Alternatively, you could write a script to align them in columns in plain text.

    Both of these could be done in a script. You know what each column looks like, so I think that I would write a regular expression that captures the data from each line and puts each piece into the correct variable. You could then either use printf to format the output in plain text, or print out a CSV document.
    so say i wanted to make a small script, i would write an expression for it and output that to another file..

    but how would i write the expression.. with quotes?

    i.e

    "number" , "ip", "time/ms", "time/ms", "time/ms"

    is that how? but then how would it differentiate between numbers and text?

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    If you really just want to pretty-print the data the simplest way would be something like:
    Code:
    cat datafile | while read x ip t1 u1 t2 u2 t3 u3
    do
        printf "%3s  %-16s  %10s%s %10s%s %10s%s\n" $x $ip $t1 $u1 $t2 $u2 $t3 $u3
    done
    Adjust the spacing to suit and you're done!

  5. #5
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    here's one way
    Code:
    awk '
    { 
        printf "%-3s" ,$1
        printf "%-15s" ,$2
        printf "%10s" ,$3
        print ""
    }' "file"
    output:
    Code:
     # ./test.sh
    7  213.105.172.13     64.139
    8  62.253.184.2       52.075
    9  193.159.225.237    49.361
    10 217.239.40.173     64.264
    11 217.6.48.246       64.797
    12 194.126.123.58     77.388
    13 194.126.123.53    114.337
    14 194.126.123.46    123.318
    15 195.250.170.38    142.046

  6. #6
    Linux Newbie usblackhawk's Avatar
    Join Date
    Apr 2005
    Location
    London, UK
    Posts
    241
    thanks for your replies! for some reason awk didnt work for me.. but cat did, so am using that

    thanks again!

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Just for completeness, the approach I was suggesting is something like:
    Code:
    #!/usr/bin/perl
    
    use strict;
    
    open my $in, "< datafile" or die "$0: Cannot open datafile for input!\n";
    
    while(<$in>)
    {
        my($num, $ip, $time1, $time2, $time3) = /^(\d+)\s+([\d.]+)\s+([\d.]+) ms\s+([\d.]+) ms\s+([\d.]+) ms$/;
        # do whatever you want with these values
    }
    I am a big fan of the power of regular expressions, and while this one could be more precise, it would be good as long as the data was sane. I like this because rather than just splitting on whitespace, it allows you to specify exactly what fields you want and what they look like.

    But hey, whatever works.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...