cut not counting whitespaces - Unix

This is a discussion on cut not counting whitespaces - Unix ; Hi, I have this file wherein i am trying to cut based on characters. Since the file is a fixed length one, there are spaces present to mark field start-end. my cut instruction however, does not seem to count the ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: cut not counting whitespaces

  1. cut not counting whitespaces

    Hi,

    I have this file wherein i am trying to cut based on characters.

    Since the file is a fixed length one, there are spaces present to mark
    field start-end. my cut instruction however, does not seem to count the
    spaces as characters (i use -c). I also tried -b but to no avail.

    I have not yet tried perl or any other tool... just wondering on
    behvior of cut.

    Any ideas ?

    cheers
    raghav..


  2. Re: cut not counting whitespaces

    try awk, it got more built-in functions, wish it helps.
    I am not quite good at it, but it will do what u want i reckon.

    Steven

    Raghav wrote:
    > Hi,
    >
    > I have this file wherein i am trying to cut based on characters.
    >
    > Since the file is a fixed length one, there are spaces present to mark
    > field start-end. my cut instruction however, does not seem to count the
    > spaces as characters (i use -c). I also tried -b but to no avail.
    >
    > I have not yet tried perl or any other tool... just wondering on
    > behvior of cut.
    >
    > Any ideas ?
    >
    > cheers
    > raghav..



  3. Re: cut not counting whitespaces

    "Raghav" writes:
    >I have this file wherein i am trying to cut based on characters.


    >Since the file is a fixed length one, there are spaces present to mark
    >field start-end. my cut instruction however, does not seem to count the
    >spaces as characters (i use -c). I also tried -b but to no avail.


    >I have not yet tried perl or any other tool... just wondering on
    >behvior of cut.


    >Any ideas ?


    Are they spaces? or are they tabs expanding out to spaces?

    When counting characters, one tab character can expand out to look
    like several spaces.

    If your file has mixed tabs and spaces, its probably better to define
    the delimeter to be tab and go by fields instead of characters.

    Or use awk/perl which would have better field seperation guessing than
    cut does.


  4. Re: cut not counting whitespaces


    Raghav wrote:
    > Hi,
    >
    > I have this file wherein i am trying to cut based on characters.
    >
    > Since the file is a fixed length one, there are spaces present to mark
    > field start-end. my cut instruction however, does not seem to count the
    > spaces as characters (i use -c). I also tried -b but to no avail.
    >
    > I have not yet tried perl or any other tool... just wondering on
    > behvior of cut.
    >
    > Any ideas ?
    >
    > cheers
    > raghav..


    Have you tried cut with -f and -d' ' ?


  5. Re: cut not counting whitespaces

    Raghav wrote:

    > Hi,
    >
    > I have this file wherein i am trying to cut based on characters.
    >
    > Since the file is a fixed length one, there are spaces present to mark
    > field start-end. my cut instruction however, does not seem to count the
    > spaces as characters (i use -c). I also tried -b but to no avail.
    >
    > I have not yet tried perl or any other tool... just wondering on
    > behvior of cut.
    >
    > Any ideas ?
    >
    > cheers
    > raghav..
    >


    I don't use cut much but this sounds like a job for awk/nawk/gawk.

  6. Re: cut not counting whitespaces

    Raghav wrote:
    > Hi,
    >
    > I have this file wherein i am trying to cut based on characters.
    >
    > Since the file is a fixed length one, there are spaces present to mark
    > field start-end. my cut instruction however, does not seem to count the
    > spaces as characters (i use -c). I also tried -b but to no avail.
    >
    > I have not yet tried perl or any other tool... just wondering on
    > behvior of cut.
    >
    > Any ideas ?
    >
    > cheers
    > raghav..
    >



    If the file has tabs, use something like

    newform -i4 $FILE | cut -c 1-3,8-11

    That will expand the tabs to spaces, and send that to cut. If you may
    want to use -i8, etc.

    Or if you want say the third column, try

    cat $FILE | awk '{print $3;}'

    There are probably 100 other easy ways to do this, it depends on exactly
    what you want to do.

    --

    Glenn Robitaille
    glennr@monitorscripts.com
    http://www.monitorscripts.com

  7. Re: cut not counting whitespaces


    Glenn Robitaille wrote:
    > Raghav wrote:
    > > Hi,
    > >
    > > I have this file wherein i am trying to cut based on characters.
    > >
    > > Since the file is a fixed length one, there are spaces present to mark
    > > field start-end. my cut instruction however, does not seem to count the
    > > spaces as characters (i use -c). I also tried -b but to no avail.
    > >
    > > I have not yet tried perl or any other tool... just wondering on
    > > behvior of cut.
    > >
    > > Any ideas ?
    > >
    > > cheers
    > > raghav..
    > >

    >
    >
    > If the file has tabs, use something like
    >
    > newform -i4 $FILE | cut -c 1-3,8-11
    >
    > That will expand the tabs to spaces, and send that to cut. If you may
    > want to use -i8, etc.
    >
    > Or if you want say the third column, try
    >
    > cat $FILE | awk '{print $3;}'


    UUOC.
    awk '{print $3;}' $FILE
    OR
    awk '{print $3;}' < $FILE
    >
    > There are probably 100 other easy ways to do this, it depends on exactly
    > what you want to do.
    >
    > --
    >
    > Glenn Robitaille
    > glennr@monitorscripts.com
    > http://www.monitorscripts.com



+ Reply to Thread