[HPADM] RE: -SUMMARY- Using sed or awk

From: Naylor, Jim (Jnaylor_at_Schnucks.com)
Date: 10/06/04

  • Next message: Kelly Choo: "[HPADM] CDE doesn't start"
    To: "Hpux-Admin@Dutchworks. Nl (E-mail)" <hpux-admin@dutchworks.nl>
    Date: Wed, 6 Oct 2004 14:25:35 -0500 
    
    

    Thanks to Bill Thompson, Marc Ahrendt, Thomas Leber, Richard Wright, Gary
    Paveza, Timothy Reed, Tim Pickel, Ram V., James Sohr, and Mike Blanchette
    who all suggested using cut

    cat filename | cut -c1-54 > newfile
    or
    cut -c1-54 filename > newfilename

    I did try using cut but I was doing the following:

    cat filename | while read line
    do
    echo "$line" | cut -c1-54 > jjj
    done

    doing this truncates the spaces.

    Also thanks to Magnus Andersen and Tom Swigg for the awk approach:

    cat filename | awk '{ print substr($0,1,54) }'

    And from Eef Hartman:

    There are probably hundreds of solutions, the one I thought up is this:
    sed 's/\(......\).*/\1/' your_file > your_truncated_file
    in which ^^^^^^
    should be exactly the NUMBER of chars you want to keep (54 in this case).

    So there should be 54 dots in between the (escaped) parenthesis (sp?).

    Of course the quotes and \'s should be typed in exactly as shown and you
    will have to replace the input and output filenames (they should be
    DIFFERENT,
    otherwise you overwrite the input file before you process it!).

    What is does is: it divides the line into 2 parts, the _first_ 54 chars and
    _all the others chars (.*) and it then replaces the whole line by "that
    first
    part" (\1 = first expression in between parenthesis).
    As I don't need the second part, I don't need to enclose the .* within ()
    too,
    but if I had done so, it would be \2 in the "replacement" part of the
    command.

    This is a nice example of regular expressions, as all the chars in the s///
    command are "special" as the command should work against ANY text line.
    I don't know of any way within RE's to get the 54 dots an "easier" way.
    But maybe someone else has a better solution.

    Thanks again to all.

    Thanks,
    Jim Naylor
    Unix Systems Administrator
    Schnuck Markets, Inc.
    * Direct: (314) 994-4784
    *)) Cell: (314) 691-0186
    * Fax : (314) 994-4684
    * jnaylor@schnucks.com

    -----Original Message-----
    From: Naylor, Jim
    Sent: Wednesday, October 06, 2004 1:30 PM
    To: Hpux-Admin@Dutchworks. Nl (E-mail)
    Subject: [HPADM] Using sed or awk

    Hello all,
    I am not well versed in sed and awk so I would like to post this issue. I
    have a file that has records in it that are 133 characters in length. The
    actual data needed is only 54 characters the rest of the record is spaces.
    What I need to do is get the first 54 characters from each record. I need
    all 54 characters whether the are space or not. Any suggestions would be
    appreciated.

    Thanks,
    Jim Naylor
    Unix Systems Administrator
    Schnuck Markets, Inc.
    * Direct: (314) 994-4784
    *)) Cell: (314) 691-0186
    * Fax : (314) 994-4684
    * jnaylor@schnucks.com

    --
                 ---> Please post QUESTIONS and SUMMARIES only!! <---
            To subscribe/unsubscribe to this list, contact
    majordomo@dutchworks.nl
           Name: hpux-admin@dutchworks.nl     Owner:
    owner-hpux-admin@dutchworks.nl
     
     Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse
    only)
                http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse &
    search)
    --
                 ---> Please post QUESTIONS and SUMMARIES only!! <---
            To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
           Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
     
     Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
                http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)
    

  • Next message: Kelly Choo: "[HPADM] CDE doesn't start"

    Relevant Pages

    • Re: String problem I guess?
      ... specified number of characters. ... Command: ... ** the end of the string. ... need a 12 chars string to proper hold what you read from the stdin. ...
      (comp.lang.c.moderated)
    • Re: Splitting a string into characters portably
      ... characters. ... Here's a portable 2 command equivalent of GAWK's ability to split with "": ... Doesn't this work for the K&R awk (except maybe for the default ...
      (comp.lang.awk)
    • Re: escaping within multi-substitution
      ... Command line processing of eval $AWK and eval "$AWK" ... characters that are not needed... ...
      (comp.unix.shell)
    • Re: ascii nulls, awk, and tr
      ... > them in awk already, so adding something to an awk script would be a ... don't know how well it compares or whether it can handle nulls. ... can handle, nulls just like other characters, etc. ... reading the terminal after you typed that command). ...
      (comp.unix.solaris)
    • Re: Set App Priority
      ... string and then terminates" ... gives then parameters to use to modify the START command ... below) /F:OFF Disable file and directory name completion characters ... no special characters between the two quote characters, ...
      (microsoft.public.windowsxp.customize)