Re: sed - how to? : the power of awk

From: Thierry ITTY (thierry.itty@BESANCON.ORG)
Date: Thu Oct 23 2003 - 03:08:00 EDT


just try :

awk -v FS="\t" -v OFS="\t" '{$2=sprintf("%3.3u", $2); print $0}' <
inputfile > outputfile

this will set the 4th field to the desired format (fixed length 3 digits
integer) and leave the remainder unchanged.
awk will implicitely remove leading blanks on the fourth field before
considering it is a number.
FS and OFS settings will tell awk to consider only tabs as field delimiter
in input (FS) and ouput (OFS), thus ensuring spaces, either leading,
trailing or embedded, won't be removed, but for the 4th field

to change the processed field just set the $number accordingly

hth

A 13:35 22/10/2003 -0500, vous avez écrit :
>My input file looks like this:
>
>
>
>ab cdef ghi 34 jkl
>
>abc de fghi 2 jkl
>
>abc def gh 012 ijkl
>
>abc defg hi 01 jkl
>
>abcd ef ghi 05 jkl
>
>
>
>(tab delimited, fourth field may or may not have leading spaces)
>
>
>
>The desired output is:
>
>
>
>ab cdef ghi 034 jkl
>
>abc de fghi 002 jkl
>
>abc def gh 010 ijkl
>
>abc defg hi 001 jkl
>
>abcd ef ghi 005 jkl
>
>
>
>(fourth field padded with leading zeros to a fixed (3) length)
>
>
>
>I could do it with the following:
>
>
>
>while read FIELD1 FIELD2 FIELD3 FIELD4 FIELD5; do
>
> printf "%s\t%s\t%s\t%s\t%03d\t%s\n" $FIELD1 $FIELD2 $FIELD3 $FIELD4
>$FIELD5
>
>done < input.file
>
>
>
>The problem is that the actual input file(s) have up to a hundred fields
>and the field that I need to act upon varies from file to file.
>
>
>
>I was wondering if there was an easier way to accomplish this with sed.
>
>
>
>TIA
>
>
>
>David
>
>
>
>
>
>
>
>**********************************************************************
>This email and any files transmitted with it are confidential and
>intended solely for the use of the individual or entity to whom they
>are addressed. If you have received this email in error please notify
>the system manager.
>
>This footnote also confirms that this email message has been swept by
>MIMEsweeper for the presence of computer viruses.
>
>www.mimesweeper.com
>**********************************************************************
>
>
>Attachment Converted: "d:\app\net\eudora\attach\sed-howt"
>



This archive was generated by hypermail 2.1.7 : Wed Apr 09 2008 - 22:17:18 EDT