Looking for a script to convert my input file to delimited text file. Not familier with AWK or shell programing. Below is sample record in my input file and the expected output format. My OS is HPUX 11.23.
It looks like each of the first three records will have slightly different rules.
(1) has 1230-1630 as field
(2) does not have this field
(3) creates this field by splitting off the previous field
There are three possible record layouts. So, read the record.
if five fields separated by " " space characters, then type A
if four fields and field 3 is 17 characters, then type B
if four fields and field 3 is 23 characters, then type C (similar to type A)
Can you use perl? If so, you can build a mask for each of the three fixed format records, then unpack() them and join() them using your delimiter (looks like a space, supposed to be comma in CSV??)....
What follows is (a) input file, (b) working model for script, (c) execution of script. So, does it properly classify the records into one of the three type? If so, then all that is left is to break the lines into pieces to write out in a format.
> cat file1
4002000W1ABCDABCD7821 123456789071001080600W1VUF 34216002902291LSN 1230-1630 +000000+000400+00000000+00009164+000400+0000916450125+00000000
4002000W1JKLMABCD6022 123456789071001080600W1VUF 34360001302894UOM +000000+000000+00000000+00014514+000000+0001451413125+00000000
4002000W1JKLMABCD6022 123456789071001080600W1VUF 34360001302791RFN123456 +000000+008000+00000000+00223280+008000+0022328050125+00000000
4002000W1JKLMABCD6022 123456789071001080600W1VUFC34360001303067UJN +000000+000000+00000000+00015213+000000+0001521313125+00000000
> cat do_file
#! /usr/bin/bash
while read zf
do
# echo "$zf"
fldcnt=$(echo "$zf" | cut -c50- | wc -w)
fld3cnt=$(echo "$zf" | cut -c50- | cut -d" " -f1 | wc -c)
flag=$(echo "$fldcnt""$fld3cnt")
echo "$flag"
# now do the file formats
if [ "$flag" = "318" ]
then
echo "a"
fi
if [ "$flag" = "218" ]
then
echo "b"
fi
if [ "$flag" = "224" ]
then
echo "c"
fi
done <file1
> do_file
318
a
218
b
224
c
218
b
If you copy/paste that do_file script to unix, do the chmod +x to make executable. It can be run.
The only other change might be in the filename, as near the very end I have a "<file1" -- substitute your filename for file1.