AWK 分别打印两个 for 循环,标题在开头
AWK Print two for loops separately with titles at the beginning
我有这个脚本,我想在执行条件 if
之前打印一个标题
我的代码
#!/usr/bin/awk -f
{for(i=13; i<=NF; i++)
{if($i~"terror" || $i~ "Terror") print [=10=],i}
}
{for(i=13; i<=NF; i++)
{if ($i ~ "bird" || $i~ "pigeon") print [=10=],i}
}
我的 csv
这是我的 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
我有输入
./c_awk.awk DataAirpleneCrashes.csv
输出我有
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
我想要的输出
***** 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
由于没有输入示例,我用你的“我有的输出”作为输入。
我还检查了整行是否包含单词 terror 或 bird,但如果您需要它所在的列,您可以更改它。
cat testInput | awk '{ if(toupper([=10=]) ~ /TERROR/) { varTerror=varTerror"\n"[=10=]; next; } if(toupper([=10=]) ~ /BIRD/) {varBird=varBird"\n"[=10=]; next; } } END { if(varTerror != "") { print " ***** Terrorism accidents ******* "; print varTerror } if(varBird != "") { print " ******** Bird accidents ******** "; print varBird } }'
我有这个脚本,我想在执行条件 if
之前打印一个标题我的代码
#!/usr/bin/awk -f
{for(i=13; i<=NF; i++)
{if($i~"terror" || $i~ "Terror") print [=10=],i}
}
{for(i=13; i<=NF; i++)
{if ($i ~ "bird" || $i~ "pigeon") print [=10=],i}
}
我的 csv
这是我的 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
我有输入
./c_awk.awk DataAirpleneCrashes.csv
输出我有
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
我想要的输出
***** 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
由于没有输入示例,我用你的“我有的输出”作为输入。
我还检查了整行是否包含单词 terror 或 bird,但如果您需要它所在的列,您可以更改它。
cat testInput | awk '{ if(toupper([=10=]) ~ /TERROR/) { varTerror=varTerror"\n"[=10=]; next; } if(toupper([=10=]) ~ /BIRD/) {varBird=varBird"\n"[=10=]; next; } } END { if(varTerror != "") { print " ***** Terrorism accidents ******* "; print varTerror } if(varBird != "") { print " ******** Bird accidents ******** "; print varBird } }'