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

From: Naylor, Jim (Jnaylor@Schnucks.com)
Date: Wed Oct 06 2004 - 15:25:35 EDT


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)


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:43 EDT