AWK Script to convert input file(s) to output file

Hi All,

I am hoping someone can help me with some scripting I need to complete using AWK.

I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format.

The Input file is:-

aaaa bbbbb ccccc 1 xxxx aaa bbb
aaaa bbbbb ccccc 2 abcd aaa CCC
aaaa bbbbb ccccc 3 fghi bbb deppp

Output file needs to be:-

HEADER  aaaa bbbbb ccccc
DETAIL  1 xxxx aaa bbb
DETAIL  2 abcd aaa CCC
DETAIL  3 fghi bbb deppp

I had planned on using the line number pos(17,1) 1-3 to split out the detail from the repeated header information.

My script so far is as follows:-

#Define  Variables
ifile="input.txt"
ofile="output.txt"
#Begin Code
awk '{ LIN=substr($0,17,1)
        if ( LIN = "1" ) print "HEADER    " $LIN substr($0,1,17) "\n" "DETAIL    "  substr($0,18,14)
       else print "DETAIL    " substr($0,18,14)
     }'  $ifile > $ofile

Any help will be very much appreciated!

not awk but bash
if one of 3 first fields change then create a new header (if i'ts your need)

#!/bin/bash
while read A B C D E F G
do	[ "$A $B $C" != "$H" ] && { H="$A $B $C"; echo "HEADER $H"; }
	echo "DETAIL $D $E $F $G"
done < infile

This will work. You might need to adjust spacing if your line number in column 4 will be more than a single digit.

 awk '
        NR == 1 { printf( "HEADER   %10s %10s %10s\n", $1, $2, $3 ); }
        {
                printf( "DETAIL %d %10s %10s %10s\n", $4, $5, $6, $7 );
        }
' <inputfile >outputfile
1 Like
awk 'NR==1{print "HEADER", $1,$2,$3} {print "DETAIL",$4,$5,$6,$7}' $ifile > $ofile
1 Like

Many thanks for your help and swift replies!! These awk code samples both worked very well - however I need to be able to handle multiple sections as shown below:-
(I apologise I hadn't included that in my original post).

If the Input file is:-

aaaa bbbbb ccccc 1 xxxx aaa bbb
aaaa bbbbb ccccc 2 abcd aaa CCC
aaaa bbbbb ccccc 3 fghi bbb deppp
aaaa bbbbb ddddd 1 fghi bbb deppp
aaaa bbbbb ddddd 2 fghi bbb deppp
aaaa bbbbb eee    1 fghi bbb deppp
aaaa bbbbb eee    2 fghi bbb deppp

The output file needs to be:-

HEADER aaaa bbbbb ccccc
DETAIL 1 xxxx aaa bbb
DETAIL 2 abcd aaa CCC
DETAIL 3 fghi bbb deppp
HEADER aaaa bbbbb ddddd
DETAIL 1 xxxx aaa bbb
DETAIL 2 abcd aaa CCC
HEADER aaaa bbbbb eee
DETAIL 1 xxxx aaa bbb
DETAIL 2 abcd aaa CCC

I hope this is clear enough but please let me know if you need any more information.

This shell script does the job :slight_smile:

awk '!a[$1 FS $2 FS $3]++ {print "HEADER", $1,$2,$3} {print "DETAIL",$4,$5,$6,$7}' infile
HEADER aaaa bbbbb ccccc
DETAIL 1 xxxx aaa bbb
DETAIL 2 abcd aaa CCC
DETAIL 3 fghi bbb deppp
HEADER aaaa bbbbb ddddd
DETAIL 1 fghi bbb deppp
DETAIL 2 fghi bbb deppp
HEADER aaaa bbbbb eee
DETAIL 1 fghi bbb deppp
DETAIL 2 fghi bbb deppp
1 Like

Many thanks Frans for your help

  • I decided to go with awk in the end but thanks anyway!

---------- Post updated at 10:33 AM ---------- Previous update was at 10:32 AM ----------

rdcwayx - this is superb and achieves exactly what required!
Many thanks for your swift reply!

Another one:

awk '$4=="1"{print "HEADER " $1,$2,$3}{print "DETAIL " $4,$5,$6,$7}' file
while(<DATA>){
  chomp;
  my ($a,$b)=$_=~/(\S*\s\S*\s\S*)\s(.*)/;
  push @{$hash{$a}}, $b;
}
for my $key (keys %hash){
 print "Header ",$key,"\nDetail ";
 print join "\nDetail ",@{$hash{$key}};
 print "\n";
}
__DATA__
aaaa bbbbb ccccc 1 xxxx aaa bbb
aaaa bbbbb ccccc 2 abcd aaa CCC
aaaa bbbbb ccccc 3 fghi bbb deppp