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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK Script to convert input file(s) to output file
# 1  
Old 08-25-2010
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:-
Code:
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:-
Code:
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:-
Code:
#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!

Last edited by Franklin52; 08-27-2010 at 03:47 AM.. Reason: Placing correct code tags
# 2  
Old 08-25-2010
not awk but bash
if one of 3 first fields change then create a new header (if i'ts your need)
Code:
#!/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

# 3  
Old 08-25-2010
This will work. You might need to adjust spacing if your line number in column 4 will be more than a single digit.

Code:
 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

This User Gave Thanks to agama For This Post:
# 4  
Old 08-25-2010
Code:
awk 'NR==1{print "HEADER", $1,$2,$3} {print "DETAIL",$4,$5,$6,$7}' $ifile > $ofile

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 08-27-2010
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:-
Code:
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:-
Code:
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.


Moderator's Comments:
Mod Comment Please use code tags, thank you!

Last edited by Franklin52; 08-27-2010 at 03:49 AM.. Reason: adding code tags
# 6  
Old 08-27-2010
Quote:
Originally Posted by frans
not awk but bash
if one of 3 first fields change then create a new header (if i'ts your need)
Code:
#!/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 shell script does the job Smilie
# 7  
Old 08-27-2010
Code:
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

This User Gave Thanks to rdcwayx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Convert shell script output txt file to html table

My concnern related to the post -Convert shell script output txt file to html table, in this how to print the heading as color. awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' <filename> (8 Replies)
Discussion started by: sarajobmai
8 Replies

3. UNIX for Dummies Questions & Answers

Redirect output to the same input file in awk

Hi, I want to compare a value from test file and redirect the o/p value to the same file input file 250 32000 32 128 Below is my code awk '{ if ($1 < "300") print $1 > /tmp/test}' test want to compare 250 < 300 then print 300 to the same place below is the... (24 Replies)
Discussion started by: stew
24 Replies

4. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

5. Homework & Coursework Questions

Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit... (5 Replies)
Discussion started by: AliTheSnake
5 Replies

6. Shell Programming and Scripting

Shell Script - File Input/Output in C

This is part of my code: for in_file in $1/*.in # list of all .in files in working directory. do $c_file < $in_file > "$tempFile.out" if diff "$tempFile.out" $out_file >/dev/null 2>&1 ; then ... (6 Replies)
Discussion started by: spider-man
6 Replies

7. Shell Programming and Scripting

Convert shell script output txt file to html table

Hi, I have script which generates the output as below: Jobname Date Time Status abc 12/9/11 17:00 Completed xyz 13/9/11 21:00 Running I have the output as a text file. I need to convert it into a HTML Table and sent it thru email ... (6 Replies)
Discussion started by: a12ka4
6 Replies

8. Shell Programming and Scripting

Dynamic output file generation using a input text file with predefined output format

Hi, I have two files , one file with data file with attributes that need to be sent to another file to generate a predefined format. Example: File.txt AP|{SSHA}VEEg42CNCghUnGhCVg== APVG3|{SSHA}XK|"password" AP3|{SSHA}XK|"This is test" .... etc --------- test.sh has... (1 Reply)
Discussion started by: hudson03051nh
1 Replies

9. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

10. Shell Programming and Scripting

awk should output if one input file doesnt have matching key

nawk -F, 'FNR==NR{a= $3 ;next} $2 in a{print $1, 'Person',$2, a}' OFS=, filea fileb Input filea Input fileb output i am getting : (2 Replies)
Discussion started by: pinnacle
2 Replies
Login or Register to Ask a Question