Re: sed - how to?

From: Renison, Rick (rick.renison@EDS.COM)
Date: Wed Oct 22 2003 - 16:42:23 EDT


not sed, but a script that you only need to change two lines to get to work
on any file:

fields=5 # num fields per line
(
  echo "typeset -Z3 f4 # field to convert"
  printf "while read "
i=0
while (( i < fields ))
do
  (( i = i + 1 ))
  printf " f$i"
done
printf "; do\n echo "
i=0
while (( i < fields ))
do
  (( i = i + 1 ))
  printf " \$f$i"
done
echo "\ndone"
) > /tmp/$$
chmod +x /tmp/$$
/tmp/$$ < testfile >testfile.out
rm /tmp/$$

-----Original Message-----
From: Taylor, David [mailto:DTaylor@WBMI.COM]
Sent: October 22, 2003 2:36 PM
To: aix-l@Princeton.EDU
Subject: sed - how to?

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
**********************************************************************



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