I have 2 problems with a script:
- Passing the correct variable into awk
- Awk doesn't like the specific command used to specify the begninnging value and the ending value to print in between a specified pattern.
Here is the content of states.txt:
Alabama
Area: 52,423 sq.mi (135,775 sq.km.), 30th
Land: 50,750 sq.mi. (131,442 sq.km.), 28th
Water: 1,673 sq.mi. (4,333 sq.km.), 23rd
Coastline: 53 mi. (85 km.), 17th
Shoreline: 607 mi. (977 km.), 19th
Alaska
Area: 656,425 sq.mi (1,700,134 sq.km.), 1st
Land: 570,374 sq.mi. (1,477,263 sq.km.), 1st
Water: 86,051 sq.mi. (222,871 sq.km.), 1st
Coastline: 6,640 mi. (10,686 km.), 1st
Shoreline: 33,904 mi. (54,563 km.), 1st
Arizona
Area: 114,006 sq.mi (295,274 sq.km.), 6th
Land: 113,642 sq.mi. (294,332 sq.km.), 6th
Water: 364 sq.mi. (943 sq.km.), 48th
Arkansas
Area: 53,182 sq.mi (137,741 sq.km.), 29th
Land: 52,075 sq.mi. (134,874 sq.km.), 27th
Water: 1,107 sq.mi. (2,867 sq.km.), 31st
California
Area: 163,707 sq.mi (423,999 sq.km.), 3rd
Land: 155,973 sq.mi. (403,969 sq.km.), 3rd
Water: 7,734 sq.mi. (20,031 sq.km.), 6th
Coastline: 840 mi. (1,352 km.), 3rd
Shoreline: 3,427 mi. (5,515 km.), 5th
Colorado
Area: 104,100 sq.mi (269,618 sq.km.), 8th
Land: 103,730 sq.mi. (268,660 sq.km.), 8th
Water: 371 sq.mi. (961 sq.km.), 46th'
And so-on and so-forth
What I am trying to do is develop a script that pulls the information for each state individually while parsing it.
So the script looks something like this:
for state in $(cat states.txt | egrep -v 'Area|Land|Water' | grep [A-Z]) ; do
echo $state >> ./statelist.txt ;
done ;
for statesnip in $(cat ./statelist.txt | awk 'NR>1{print p "_" $0 ORS} {p=$0}' | grep [A-Z]) ; do
state1=$(echo $statesnip | awk -F _ '{print $1}') ;
state2=$(echo $statesnip | awk -F _ '{print $2}') ;
cat ./states.txt | awk '/$state1/{f=1}; /$state2/{f=0}' >> $state1.tmp.txt ;
done;
rm -f ./statelist.txt
So here is what is breaking:
The first, being the variables passing into awk:
as in
awk -v state1=$state1 -v state2=$state2 '/state1/{f=1} f; /state2/{f=0}';
or
awk -v state1=${state1} state2=${state2} '/state1/{f=1} f; /state2/{f=0}';
I get an error
And the second being that awk doesn't like it when I adjust variables into their -v format (it just cat's the entire file, numerous times).
awk -v state1=${state1} -v state2=${state2} 'state1{f=1} f; state2{f=0}'
I just get a full cat of the entire file repeatedly.
The expected output should look like this:
cat ./statelist.txt
Alabama
Alaska
Arizona
Arkansas
California
Colorado
cat ./statelist.txt | awk 'NR>1{print p "_" $0 ORS} {p=$0}' | grep [A-Z]
Alabama_Alaska
Alaska_Arizona
Arizona_Arkansas
Arkansas_California
California_Colorado
cat ./Alabama.txt:
Alabama
Area: 52,423 sq.mi (135,775 sq.km.), 30th
Land: 50,750 sq.mi. (131,442 sq.km.), 28th
Water: 1,673 sq.mi. (4,333 sq.km.), 23rd
Coastline: 53 mi. (85 km.), 17th
Shoreline: 607 mi. (977 km.), 19th
cat ./Alaska.txt
Alaska
Area: 656,425 sq.mi (1,700,134 sq.km.), 1st
Land: 570,374 sq.mi. (1,477,263 sq.km.), 1st
Water: 86,051 sq.mi. (222,871 sq.km.), 1st
Coastline: 6,640 mi. (10,686 km.), 1st
Shoreline: 33,904 mi. (54,563 km.), 1st
cat ./Arizona.txt
Arizona
Area: 114,006 sq.mi (295,274 sq.km.), 6th
Land: 113,642 sq.mi. (294,332 sq.km.), 6th
Water: 364 sq.mi. (943 sq.km.), 48th
cat ./Arkansas.txt
Arkansas
Area: 53,182 sq.mi (137,741 sq.km.), 29th
Land: 52,075 sq.mi. (134,874 sq.km.), 27th
Water: 1,107 sq.mi. (2,867 sq.km.), 31st
cat ./California.txt
California
Area: 163,707 sq.mi (423,999 sq.km.), 3rd
Land: 155,973 sq.mi. (403,969 sq.km.), 3rd
Water: 7,734 sq.mi. (20,031 sq.km.), 6th
Coastline: 840 mi. (1,352 km.), 3rd
Shoreline: 3,427 mi. (5,515 km.), 5th
cat ./Colorado.txt
Colorado
Area: 104,100 sq.mi (269,618 sq.km.), 8th
Land: 103,730 sq.mi. (268,660 sq.km.), 8th
Water: 371 sq.mi. (961 sq.km.), 46th'
cat foo.txt | awk '{ ... }'is better written asawk '{ ... }' foo.txt.