Results 1 to 5 of 5
  1. #1
    Join Date
    May 2007
    Location
    HONKKONG
    Posts
    2

    Unanswered: how can pick a field stored in a file

    i have a sequence of words seperated by delimiter i want to pick a field . How can i pick that?

  2. #2
    Join Date
    Mar 2007
    Posts
    29
    Hi Jagan,
    Try using cut command. I hope this will be useful in your case.
    Eg: cut -d: -f1,3 filename

    After the -d option you can specify the delimiter(here i have mentioned as colon) and then in -f option you can mention the number of fields(here i have taken the first three fields).

    Regards,
    Shiya

  3. #3
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    Hi,

    Another option is to use awk. With the -F flag you can define a different delimiter from the default space and tab. Like this:
    Code:
    awk -F ":" '{print $0}' file, or: 
    echo sequenceofwords | awk -F ":" '{print $1, $2, $3}'
    Like Shiya I specified the colon as delimiter and in the first case every complete line ($0) from the inputfile is written to output. In the second case the output from a command is piped into awk and only the first three fields are written to output.

    Regards

  4. #4
    Join Date
    May 2007
    Location
    HONKKONG
    Posts
    2

    Thanx alot...

    Thanx for your reply, but if the delimiter is only single Space!!!

  5. #5
    Join Date
    Aug 2006
    Location
    The Netherlands
    Posts
    248
    With awk it's easy to use a space/several spaces as delimiter cause that's the default delimiter. So with the previous example it would be:
    Code:
    echo sequenceofwords| awk '{print $1, $2, $3}'
    (Note that if the sequenceofwords is a string of three fields separated by a space the output of this awk command will be exactly like the input. This is because of the comma's in the print statement, they separate the outputfields by spaces. Without the comma's the three outputfields will be concatenated.)

    Regards

Posting Permissions

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