英文:
How can I update an Awk command to work with a newer version of MacOS?
问题
抱歉,我无法理解你的请求。如果你有其他需要,请随时告诉我。
英文:
I am using the following Awk command which is based on this Stack Exchange post:
tail -n +2 *.csv | sort -t',' -k2 | awk -F',' '$2~/^[[:space:]]*$/{next} {sub(/\r$/,"")} $2!=prev{close(out); out=$2".txt"; prev=$2} {print $1 > out}'
The command works perfectly under MacOS 10.14. However, I recently upgraded to MacOS 12.6 and it no longer works. (MacOS 12.6 uses awk version 20200816).
It produces the following error:
awk: newline in regular expression ... at source line 1
context is
$2~/^[[:space:]]*$/{next} {sub(/ >>>
<<<
awk: syntax error at source line 1
awk: illegal statement at source line 1
How can I get it working again and ideally (if possible) make it more future proof, without having to install any extra software. I looked at the changes made to awk, but can't find anything that would cause it to stop working.
<hr>
Background
The command takes all CSV files in a directory. It splits the file into text files according to the values of the second column of the CSV file while only keeping the values stored in the first column.
Example CSV file:
COLUMN 1,COLUMN 2
innovation "is essential",3-Entrepreneurship
countless,
innocent,2-Police
toilet handle,2-Bathroom
née dresses,3-Companies
odorless,2-Sense of Smell
old ideas "new takes",3-Entrepreneurship
new income streams,3-Entrepreneurship
Zoë’s food store,3-Companies
many,
crime "doesn't sleep",2-Police
bath room,2-Bathroom
ring,
móvíl résumés,3-Companies
musty smell's come here,2-Sense of Smell
good publicity guru,3-Entrepreneurship
Señor,3-Companies
E.g. after split
In file 3-Entrepreneurship.txt
innovation "is essential"
old ideas "new takes"
new income streams
good publicity guru
In file 2-Bathroom.txt
toilet handle
bath room
In file 2-Police.txt
innocent
crime "doesn't sleep"
In file 2-Sense of Smell.txt
odorless
musty smell's come here
In file 3-Companies.txt
née dresses
Zoë’s food store
móvíl résumés
Señor
答案1
得分: 1
以下是翻译好的内容:
我大约在3年前发布的解决方案仍然有效:
生成的文件在运行之前不得存在
awk -F, 'FNR>1 && $2 {print $1 >> ($2 ".txt"); close($2 ".txt")}' file.csv
生成:
$ head *.txt
==> 2-Bathroom.txt <==
卫生间把手
浴室
==> 2-Police.txt <==
无辜
犯罪“不休息”
==> 2-Sense of Smell.txt <==
无味
发霉的气味来这里
==> 3-Companies.txt <==
出生名字的衣服
佐伊的食品店
移动简历
先生
==> 3-Entrepreneurship.txt <==
创新“是必不可少的”
旧思想“新的方法”
新的收入来源
良好的宣传大师
或者,这里有一个Ruby版本:
ruby -r csv -e '
CSV.parse($<.read, **{:headers=>true, :liberal_parsing=>true}).
select{|r| r["COLUMN 2"]}.
group_by{|r| r["COLUMN 2"]}.
each{|k,v| File.write("#{k}.txt", v.map(&:first).map(&:last).join("\n"))
}
' file.csv
相同的输出
英文:
The solution I posted nearly 3 years ago still works:
# the files produced must not exist prior to the run
awk -F, 'FNR>1 && $2 {print $1 >> ($2 ".txt"); close($2 ".txt")}' file.csv
Produces:
$ head *.txt
==> 2-Bathroom.txt <==
toilet handle
bath room
==> 2-Police.txt <==
innocent
crime "doesnt sleep"
==> 2-Sense of Smell.txt <==
odorless
musty smells come here
==> 3-Companies.txt <==
née dresses
Zoë’s food store
móvíl résumés
Señor
==> 3-Entrepreneurship.txt <==
innovation "is essential"
old ideas "new takes"
new income streams
good publicity guru
Or, here is a Ruby:
ruby -r csv -e '
CSV.parse($<.read, **{:headers=>true, :liberal_parsing=>true}).
select{|r| r["COLUMN 2"]}.
group_by{|r| r["COLUMN 2"]}.
each{|k,v| File.write("#{k}.txt", v.map(&:first).map(&:last).join("\n"))
}
' file.csv
# same output
答案2
得分: 0
"Looks like it's treating the \r
as a literal linefeed (possible issue with using smart quotes?).
You might try, say, replacing \r
with \x0d
to see if that makes a difference."
英文:
Looks like it's treating the \r
as a literal linefeed (possible issue with using smart quotes?).
You might try, say, replacing \r
with \x0d
to see if that make a difference.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论