Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to format following file using awk

Status
Not open for further replies.

zubnus

Programmer
Apr 22, 2003
8
US
Hi Gurus,

I have a file with the FOLLOWING CONTENTS
here is a little snapshot of it. The actual file may contains more than 1000 lines. I have also put line numbers against each line. so lets say the following file (line 1-12) name is test.csv.


1.Report for states
2.Alabama
3.Michigan
4.Connecticut
5.MONTHLY REPORT - ALABAMA DIVISION
6.01,987,765,980, 345.00
7.44,56,675,380,450.00
8.MONTHLY REPORT - ALABAMA HQ
9.32,767,876,560,450.00
10.44,876,908,787,430.05
11.
12.END OF REPORT

I want to delete lines which doesn't begins with a number or 'MONTHLY REPORT' next thing is as you see line 5 has MONTHLY REPORT - ALABAMA DIVISION. I want to put on line 6 and 7 AD(ALABAMA DIVISION) at the start of line so line 6 should look like
AD,01,987,765,980, 345.00 and line 7 should look like
AD,44,56,675,380,450.00

line 8 has ALABAMA HQ so line 9 and 10 should look like
AH,32,767,876,560,450.00
AH,44,876,908,787,430.05

line 11 is blank so I need to delete that line
line 12 also should get deleted
in the end the file should look like following

AD,01,987,765,980, 345.00
AD,44,56,675,380,450.00
AH,32,767,876,560,450.00
AH,44,876,908,787,430.05
 
something like that:

nawk -f ad.awk myTextFile.txt

#------------------ ad.awk
BEGIN {
FS=OFS=","
}

/MONTHLY REPORT/ {
type=""
monthlyType=substr($0, index($0,"-")+1);
n=split(monthlyType, monthlyARR, " ");
for(i=1; i <= n; i++)
type=type substr(monthlyARR,1,1)
next;
}

/^[0-9].*/ {
$1= type OFS $1
print
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
vgersh99,

Your program seems to work fine, but my MONTHLY REPORT line is like following

MONTHLY REPORT - FEBRUARY 2003 - ORDERS OF DOUBLE LAYER

I want to pick OD( for Order of Double Layer ) and nothing else. Right now it is picking F2-OODL.

also following line was not deleted from my file
1. Shipments:,# Wires ( note 1. is not line number ), is it becuase it started with a number it didn't get deleted.
 
try something like that:
making 'ORDERS OF DOUBLE LAYER' into 'OD' is left as exercise.

#--------------------ad.awk
BEGIN {
FS=OFS=&quot;,&quot;
}

/MONTHLY REPORT/ {
type=&quot;&quot;
tmpNum=split($0, tmpA, &quot;-&quot;);
monthlyType=tmpA[tmpNum];
n=split(monthlyType, monthlyARR, &quot; &quot;);
for(i=1; i <= n; i++)
type=type substr(monthlyARR,1,1)
next;
}

/END OF REPORT/ { type=&quot;&quot;; next}

/^[0-9].*/ && type != &quot;&quot; {
$1= type OFS $1
print
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks vgersh99 for your help, this is working fine now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top