Editing a output file
Posted in 1999
Topics: General Discussion
I have a SQL command that runs each night against my database to generate an output file, but the output is not quite right. The output format is as follows: TKID | TKINIT | TKFIRST | TKLAST 1 | RAB | Robert A. | Banks | 2 | DEC | David R.| Cunningham | 3 | MRD | Mathew R. | Donaldson | Is the a way to write a script, use VI or a third party program to edit this file to remove the pipe symbol (and extra space) from between the TKFIRST and TKLAST so that the final format looks like this: 1 | RAB | Robert A. Banks | 2 | DEC | David R. Cunningham | 3 | MRD | Mathew R. Donaldson | Stacey Windsor x6071
The following is an awk script which will concatenate the 2 fields or just use the last name if the first name field is blank. This is what I use to get the timekeeper information from our Elite database to Carpe Diem and our cost collection system. Just put the following in a file and make it executable, then run it giving the output file name as the first parameter. The output file will be replace by the edited version. awk 'BEGIN {FS=OFS="|"} { if ($3 != ""){ print $1, $2, $3 " " $4 } else{ print $1, $2, $4 } }' $1 > $1.tmp mv $1.tmp $1 ------------------------------------------------------------------------------- Larry Foote LFoote@pipeline.com "Stacey Windsor" <swindsor@verner.com> wrote in message news:8294s7$5b3$1@news.xmission.com... > > > I have a SQL command that runs each night against my database to generate an output > file, but the output is not quite right. The output format is as follows: > > TKID | TKINIT | TKFIRST | TKLAST > > 1 | RAB | Robert A. | Banks | > 2 | DEC | David R.| Cunningham | > 3 | MRD | Mathew R. | Donaldson | > > Is the a way to write a script, use VI or a third party program to edit this file to remove the pipe symbol (and extra space) from between the TKFIRST and TKLAST so that the final format looks like this: > > > 1 | RAB | Robert A. Banks | > 2 | DEC | David R. Cunningham | > 3 | MRD | Mathew R. Donaldson | > > > > > Stacey Windsor > x6071 >
Well, one option is probably to avoid putting the pipe in the wrong place when doing the
SELECT:-
SELECT TKID, TKINIT, TRIM(TKFIRST) || ' ' || TRIM(TKLAST)
FROM WHEREVER;
Failing that, try using either awk or 'perl in emulate awk mode', set the field separator to '|',
then concatenate the third and fourth fields before printing the results.
#--untested!
awk -F'|' '{print $1, $2, $3 " " $4;}' $*
Stacey Windsor wrote:
> I have a SQL command that runs each night against my database to generate an output
> file, but the output is not quite right. The output format is as follows:
>
> TKID | TKINIT | TKFIRST | TKLAST
>
> 1 | RAB | Robert A. | Banks |
> 2 | DEC | David R.| Cunningham |
> 3 | MRD | Mathew R. | Donaldson |
>
> Is the a way to write a script, use VI or a third party program to edit this file to remove the pipe symbol (and extra space) from between the TKFIRST and TKLAST so that the final format looks like this:
>
> 1 | RAB | Robert A. Banks |
> 2 | DEC | David R. Cunningham |
> 3 | MRD | Mathew R. Donaldson |
>
> Stacey Windsor
> x6071
--
Jonathan Leffler (jleffler@informix.com, jleffler@earthlink.net)
Guardian of DBD::Informix v0.62 -- see http://www.perl.com/CPAN
#include <disclaimer.h>