Home > database >  AWK Print two for loops separately with titles at the beginning
AWK Print two for loops separately with titles at the beginning

Time:12-28

I have this script and I would like to print a single title before executing the conditional if

My code

#!/usr/bin/awk -f 
{for(i=13; i<=NF; i  ) 
    
    {if($i~"terror" || $i~ "Terror") print $0,i}
    
}

{for(i=13; i<=NF; i  ) 
        
    {if ($i ~ "bird" || $i~ "pigeon") print $0,i}
}

My csv

This is the first row of my CSV.

Date,"Time","Location","Operator","Flight","Route","Type","Registration","cn/In",Aboard,Fatalities,Ground,"Summary"
04/11/1955,"17:30","Off Great Natuna Island, Sarawak","Air India","","Hong Kong - Jakarta","Lockheed 749A Constellation","VT-DEP","2666",19,19,0,"An explosion occurred at 18,000 feet while over the China Sea. A resulting fire which started on the starboard wing soon spread and eventually led to total hydrauliand electrical failure. Smoke filled the cockpit and the plane crashed. An act of political terrorism.  An aircraft worker placed an incendiary device in the starboard wheel well." 53
10/04/1960,"17:40","Boston, Massachusett","Eastern Air Lines","375","Boston - Philadelphia","Lockheed 188A Electra","N5533","1062",72,62,0,"A few seconds after becoming airborne, the aircraft struck a flock of starlings. A number of these birds were ingested in engines No. 1, 2, and 4.  Engine No. 1 was shut down and its propeller was feathered. Engines No. 2 and 4 experienced a substantial momentary loss of power. This abrupt and intermittent loss and recovery of power resulted in the aircraft yawing to the left and decelerating to the stall speed. As speed decayed during the continued yaw and skidding left turn, the stall speed was reached; the left wing dropped, the nose pitched up, and the aircraft rolled left into a spin and fell almost vertically into the water. An altitude of less than 150 feet precluded recovery." 25
03/24/1968,"10:59","Off Wexford Harbor, Ireland","Aer Lingus","712","Cork - London","Vickers Viscount 803","EI-AOM","178",61,61,0,"The aircraft was believed to have gone into a spin or spiral dive with recovery at about 12,000 ft. The dive apparently caused structural damage which resulted in the aircraft plunging into St George's Channel, 10 miles east of Carnsore Point. Possible causal factors may have been metal fatigue, corrosion, flutter (vibration) or a bird strike." 63
12/21/1988,"19:03","Lockerbie, Scotland","Pan American World Airways","103","London - New York City","Boeing B-747-121A","N739PA","19646/15",259,259,11,"The airliner disappeared from radar shortly after leveling off at FL 310 while on a flight from Heathrow Airport, London,  to New York. The aircraft broke up with two main sections of wreckage coming down in the town of Lockerbie. Detonation of an explosive device in the forward cargo area planted by terrorists.  Musician Paul Jeffreys killed. The aircraft was named 'Clipper Maid of the Seas.'" 62

Input I have

./c_awk.awk DataAirpleneCrashes.csv 

Output I have

04/11/1955,"17:30","Off Great Natuna Island, Sarawak","Air India","","Hong Kong - Jakarta","Lockheed 749A Constellation","VT-DEP","2666",19,19,0,"An explosion occurred at 18,000 feet while over the China Sea. A resulting fire which started on the starboard wing soon spread and eventually led to total hydrauliand electrical failure. Smoke filled the cockpit and the plane crashed. An act of political terrorism.  An aircraft worker placed an incendiary device in the starboard wheel well." 53
10/04/1960,"17:40","Boston, Massachusett","Eastern Air Lines","375","Boston - Philadelphia","Lockheed 188A Electra","N5533","1062",72,62,0,"A few seconds after becoming airborne, the aircraft struck a flock of starlings. A number of these birds were ingested in engines No. 1, 2, and 4.  Engine No. 1 was shut down and its propeller was feathered. Engines No. 2 and 4 experienced a substantial momentary loss of power. This abrupt and intermittent loss and recovery of power resulted in the aircraft yawing to the left and decelerating to the stall speed. As speed decayed during the continued yaw and skidding left turn, the stall speed was reached; the left wing dropped, the nose pitched up, and the aircraft rolled left into a spin and fell almost vertically into the water. An altitude of less than 150 feet precluded recovery." 25
03/24/1968,"10:59","Off Wexford Harbor, Ireland","Aer Lingus","712","Cork - London","Vickers Viscount 803","EI-AOM","178",61,61,0,"The aircraft was believed to have gone into a spin or spiral dive with recovery at about 12,000 ft. The dive apparently caused structural damage which resulted in the aircraft plunging into St George's Channel, 10 miles east of Carnsore Point. Possible causal factors may have been metal fatigue, corrosion, flutter (vibration) or a bird strike." 63
12/21/1988,"19:03","Lockerbie, Scotland","Pan American World Airways","103","London - New York City","Boeing B-747-121A","N739PA","19646/15",259,259,11,"The airliner disappeared from radar shortly after leveling off at FL 310 while on a flight from Heathrow Airport, London,  to New York. The aircraft broke up with two main sections of wreckage coming down in the town of Lockerbie. Detonation of an explosive device in the forward cargo area planted by terrorists.  Musician Paul Jeffreys killed. The aircraft was named 'Clipper Maid of the Seas.'" 62


Output I want


***** Terrorism accidents *******
04/11/1955,"17:30","Off Great Natuna Island, Sarawak","Air India","","Hong Kong - Jakarta","Lockheed 749A Constellation","VT-DEP","2666",19,19,0,"An explosion occurred at 18,000 feet while over the China Sea. A resulting fire which started on the starboard wing soon spread and eventually led to total hydrauliand electrical failure. Smoke filled the cockpit and the plane crashed. An act of political terrorism.  An aircraft worker placed an incendiary device in the starboard wheel well." 53
12/21/1988,"19:03","Lockerbie, Scotland","Pan American World Airways","103","London - New York City","Boeing B-747-121A","N739PA","19646/15",259,259,11,"The airliner disappeared from radar shortly after leveling off at FL 310 while on a flight from Heathrow Airport, London,  to New York. The aircraft broke up with two main sections of wreckage coming down in the town of Lockerbie. Detonation of an explosive device in the forward cargo area planted by terrorists.  Musician Paul Jeffreys killed. The aircraft was named 'Clipper Maid of the Seas.'" 62


********Bird accidents********
10/04/1960,"17:40","Boston, Massachusett","Eastern Air Lines","375","Boston - Philadelphia","Lockheed 188A Electra","N5533","1062",72,62,0,"A few seconds after becoming airborne, the aircraft struck a flock of starlings. A number of these birds were ingested in engines No. 1, 2, and 4.  Engine No. 1 was shut down and its propeller was feathered. Engines No. 2 and 4 experienced a substantial momentary loss of power. This abrupt and intermittent loss and recovery of power resulted in the aircraft yawing to the left and decelerating to the stall speed. As speed decayed during the continued yaw and skidding left turn, the stall speed was reached; the left wing dropped, the nose pitched up, and the aircraft rolled left into a spin and fell almost vertically into the water. An altitude of less than 150 feet precluded recovery." 25
03/24/1968,"10:59","Off Wexford Harbor, Ireland","Aer Lingus","712","Cork - London","Vickers Viscount 803","EI-AOM","178",61,61,0,"The aircraft was believed to have gone into a spin or spiral dive with recovery at about 12,000 ft. The dive apparently caused structural damage which resulted in the aircraft plunging into St George's Channel, 10 miles east of Carnsore Point. Possible causal factors may have been metal fatigue, corrosion, flutter (vibration) or a bird strike." 63


CodePudding user response:

Since there was no input example, I used your "Output I have" as input.

I also checked if the whole line contains the word terror or bird, but you can change it if you need the column where it is.

cat testInput | awk '{ if(toupper($0) ~ /TERROR/) { varTerror=varTerror"\n"$0; next; } if(toupper($0) ~ /BIRD/) {varBird=varBird"\n"$0; next; } } END { if(varTerror != "") { print " ***** Terrorism accidents ******* "; print varTerror } if(varBird != "") { print " ******** Bird accidents ******** "; print varBird } }'
  • Related