gpt4 book ai didi

arrays - 使用 awk 解析文本文件的各个部分

转载 作者:行者123 更新时间:2023-12-02 08:28:17 24 4
gpt4 key购买 nike

我的脚本有 2 个问题:

  1. 将正确的变量传递给 awk
  2. Awk 不喜欢用于指定要在指定模式之间打印的开始值和结束值的特定命令。

这是 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'

等等

我想做的是开发一个脚本,在解析时分别提取每个状态的信息。

所以脚本看起来像这样:

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

所以这是破坏的地方:

第一个是传递给 awk 的变量:

awk -v state1=$state1 -v state2=$state2 '/state1/{f=1} f; /state2/{f=0}';

awk -v state1=${state1} state2=${state2} '/state1/{f=1} f; /state2/{f=0}';

我得到一个错误

第二个是当我将变量调整为它们的 -v 格式时 awk 不喜欢它(它只是 cat 整个文件,多次)。

 awk -v state1=${state1} -v state2=${state2} 'state1{f=1} f; state2{f=0}'

我只是反复获取整个文件的完整内容。

预期的输出应该是这样的:

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'

最佳答案

任何时候你在 shell 中编写一个循环只是为了操作文本,你的方法都是错误的。

在这种情况下,看起来您真正需要的是:

awk 'NF==1{out=$1".txt"} {print > out}' states.txt

如果不是,请澄清。哦,对于非 gawk,您可能需要在 out=... 之前添加 close(out)

关于arrays - 使用 awk 解析文本文件的各个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29927315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com