I have a file that contains some headers at the first of the file starting with "H" that I want to write to the first of all of the following files that are created based on the non H records field 2.
Everything works like I want it to except for the multiple H records printed to each file.
Any help is appreciated.
Here's the data file:
H1
H 2
H 3
H
H 44
GROUP1 AA 1
GROUP1 AA 2
GROUP1 AA 10
GROUP1 BB 1
GROUP1 CC 1
GROUP2 AA 3
GROUP2 AA 4
GROUP3 AA 5
GROUP10 AA 5
and here's my script so far:
BEGIN {
# if (substr($0,1,1) == "H"){print $0 > $2".DAT"}
old=$2
}
{
new=$2
#if (substr($0,1,1) != "H")
{
if (new != old) { print $0 >> $2".DAT"}
if (new == old) { print $0 >> $2".DAT"}
old=new
}
}
Everything works like I want it to except for the multiple H records printed to each file.
Any help is appreciated.
Here's the data file:
H1
H 2
H 3
H
H 44
GROUP1 AA 1
GROUP1 AA 2
GROUP1 AA 10
GROUP1 BB 1
GROUP1 CC 1
GROUP2 AA 3
GROUP2 AA 4
GROUP3 AA 5
GROUP10 AA 5
and here's my script so far:
BEGIN {
# if (substr($0,1,1) == "H"){print $0 > $2".DAT"}
old=$2
}
{
new=$2
#if (substr($0,1,1) != "H")
{
if (new != old) { print $0 >> $2".DAT"}
if (new == old) { print $0 >> $2".DAT"}
old=new
}
}