Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    7

    Question Unanswered: multiple cuts in a line

    Hi,

    a new pal in this great group...Hello everyone!

    so what i need is to be able to cut a line vertically at different columns of course using the 'cut' command.
    say like there is a line in a file a.txt which reads
    "I am a boy."(excluding the " marks)

    so i want to have in my output
    I boy

    what i thot of is
    cat a.txt | cut -c1-1 -c8-10

    but this when tried does not take all the -c's but only the last. so in this case i get boy and the first -c parameter goes unheeded..

    any other way of using this cut or any other utility to have what i want?

    bye

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You must specify only one -c option.
    With this option you can specify a list of ranges separated by commas.

    Code:
    cut -c1-1,8-10 a.txt
    Don't use 'cat' when you can specify the input file for the command.
    Jean-Pierre.

  3. #3
    Join Date
    Mar 2004
    Posts
    7
    Originally posted by aigles
    You must specify only one -c option.
    With this option you can specify a list of ranges separated by commas.

    Code:
    cut -c1-1,8-10 a.txt
    Don't use 'cat' when you can specify the input file for the command.

    hi,

    thanks for the help...but later i found me helpless again..
    so what i want now is to be able to place the columns wherever i want
    so if i wanna concat 38-41 columns with 25 column and make it a single number say i am not able to do it....what it does is go from 1st column to the last and output the columns serially if asked for in the command....
    so in this case i will get the number i want as

    [25][38-41] and not as [38-41][25] which is what i want...

    and again i also cannot delimit the columns in the output..like say
    cut -c4-7," "1-2 a.txt

    to get separate columns in the output
    Last edited by amolbc; 03-24-04 at 11:45.

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    You can do it with 'awk'

    Code:
    awk ' {
       print substr($0,38,4) substr($0,25,1) # print [25][38-41]
       print substr($0,4,4),substr($0,1,2)   # print [4-7] [1-2]
    } ' a.txt

    $0
    The record read from file

    substr(STRING, START [, LENGTH])

    This returns a LENGTH-character-long substring of STRING, starting
    at character number START. The first character of a string is
    character number one. For example, `substr("washington", 5, 3)'
    returns `"ing"'.

    If LENGTH is not present, this function returns the whole suffix of
    STRING that begins at character number START. For example,
    `substr("washington", 5)' returns `"ington"'. The whole suffix is
    also returned if LENGTH is greater than the number of characters
    character number one. For example, `substr("washington", 5, 3)'
    returns `"ing"'.

    If LENGTH is not present, this function returns the whole suffix of
    STRING that begins at character number START. For example,
    `substr("washington", 5)' returns `"ington"'. The whole suffix is
    also returned if LENGTH is greater than the number of characters
    remaining in the string, counting from character number START.

    print statement

    The `print' statement does output with simple, standardized
    formatting. You specify only the strings or numbers to be printed, in a
    list separated by commas. They are output, separated by single spaces,
    followed by a newline. The statement looks like this:

    print ITEM1, ITEM2, ...
    Jean-Pierre.

  5. #5
    Join Date
    Mar 2004
    Posts
    7

    Cool

    Originally posted by aigles
    You can do it with 'awk'

    Code:
    awk ' {
       print substr($0,38,4) substr($0,25,1) # print [25][38-41]
       print substr($0,4,4),substr($0,1,2)   # print [4-7] [1-2]
    } ' a.txt

    $0
    The record read from file

    substr(STRING, START [, LENGTH])

    This returns a LENGTH-character-long substring of STRING, starting
    at character number START. The first character of a string is
    character number one. For example, `substr("washington", 5, 3)'
    returns `"ing"'.

    If LENGTH is not present, this function returns the whole suffix of
    STRING that begins at character number START. For example,
    `substr("washington", 5)' returns `"ington"'. The whole suffix is
    also returned if LENGTH is greater than the number of characters
    character number one. For example, `substr("washington", 5, 3)'
    returns `"ing"'.

    If LENGTH is not present, this function returns the whole suffix of
    STRING that begins at character number START. For example,
    `substr("washington", 5)' returns `"ington"'. The whole suffix is
    also returned if LENGTH is greater than the number of characters
    remaining in the string, counting from character number START.

    print statement

    The `print' statement does output with simple, standardized
    formatting. You specify only the strings or numbers to be printed, in a
    list separated by commas. They are output, separated by single spaces,
    followed by a newline. The statement looks like this:

    print ITEM1, ITEM2, ...
    Thanks a lot..this wudd help me....
    i think i think of only in one direction....i was after cut only when awk is fit and happy to help me out..

    now u go for replying my latest post..

Posting Permissions

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