【问题标题】:AWK (illegal statement error with no more description)AWK(没有更多描述的非法语句错误)
【发布时间】:2020-09-19 00:33:38
【问题描述】:

我收到“非法字符错误”,在 AWK 中没有详细描述...

awk:源代码行 1 处的非法语句

感谢 AWK 的帮助,但我只有一行代码.... 请问哪里出错了?

awk '/^# /{$h}{print >"out" $h ".txt" }' File.txt

系统:

Mac OS 10.15

这是我的输入文件

# Heading 1

Rortox symbol....

qwtiRqto porsofql driRo....
DoifId thoso thifIds.....
fot qwwomplishif....

# Epilogue

You qro oxp......
qwwomplishi......

fot doifId so mqy....

# Chapter B

You qro oxpowtod to brifId you......

我想要的输出文件

标题 1.txt

# Heading 1

Rortox symbol....

qwtiRqto porsofql driRo....
DoifId thoso thifIds.....
fot qwwomplishif....

Epilogue.txt

# Epilogue

You qro oxp......
qwwomplishi......

fot doifId so mqy....

B 章

# Chapter B
  
You qro oxpowtod to brifId you......

【问题讨论】:

  • 你希望$h 做什么?!我会冒险猜测并说那是你的错误。提示:如果错误消息看起来不清楚,请将程序格式化为多行并将其放在单独的文件中。这将帮助您缩小问题范围。
  • 喜欢这里justpaste.it/8r7z8 ...将其保存为xyz.awk,并使用awk -f xyz.awk File.txt调用它
  • $h 只是用于存储# Heading 的变量。
  • {h=substr($0,3); next} 会更有意义...然后在下一条规则中去掉'$'。例如。 awk '/^#/{h=substr($0,3);next} {print > "out" h ".txt"}' file
  • 我在 xzy.awk 中尝试了你的命令,现在我得到:awk: bailing out at source line 1。他在抱怨什么?我有 ' 那里我有 \ 在开始和结束...

标签: linux shell awk


【解决方案1】:

如果您想创建一个awk 脚本,该脚本将文件名作为参数,然后执行生成如上所述分离文件的规则,您需要确保第一行使用正确的解释器。

注释已编辑以删除next,因此标题包含在文件中)

例如,创建xyz.awk 我会这样做:

#!/usr/bin/awk -f

/^#/{ h=substr($0,3) }
    { print > "out" h ".txt" }

在 Mac 上检查正确的路径。使用chmod +x xyz.awk 使文件可执行,然后在输入文件中使用您的输入运行脚本会产生:

'outChapter B.txt'
outEpilogue.txt
'outHeading 1.txt'

包含:

$ cat outHeading\ 1.txt
# Heading 1

Rortox symbol....

qwtiRqto porsofql driRo....
DoifId thoso thifIds.....
fot qwwomplishif....

$ cat outEpilogue.txt
# Epilogue

You qro oxp......
qwwomplishi......

fot doifId so mqy....

$ cat outChapter\ B.txt
# Chapter B

You qro oxpowtod to brifId you......

从命令行运行

如果您不想创建awk 脚本,而只是想直接从命令行运行命令:

awk '/^#/{ h=substr($0,3) } { print > "out" h ".txt" }' file

将产生完全相同的文件。

我认为您可能会因为引用而感到困惑。您不需要在 awk 脚本中将规则用单引号括起来 - 但直接从命令行运行时需要这样做。

字符串连接 awk 的一些版本

对于某些版本的 awk(我不知道是哪个版本),您需要通过将串联括在括号中来赋予串联优先级,例如

awk '/^#/{ h=substr($0,3) } { print > ("out" h ".txt") }' file

请参阅GNU Awk User's Guide - String Concatenation 显然所有列出的 awk:BWK awk、gawk 和 mawk,都可以做到,而不必包含 (...),但它也提供了注释——“你不应该依赖这个”—— - 意味着一些 awks 没有正确处理。

针对链接文件运行

针对8263444c 文件运行,创建了 85 个文件并且没有报告错误:

'outThis is not possible in the northern or southern hemisphere.txt'
'outVertex Conjunct Ascendant.txt'
'outVertex Conjunct Jupiter.txt'
'outVertex Conjunct Mars.txt'
'outVertex Conjunct Mercury.txt'
'outVertex Conjunct Midheaven.txt'
'outVertex Conjunct Moon.txt'
'outVertex Conjunct Neptune.txt'
'outVertex Conjunct Pluto.txt'
'outVertex Conjunct Saturn.txt'
'outVertex Conjunct Sun.txt'
'outVertex Conjunct Uranus.txt'
'outVertex Conjunct Venus.txt'
'outVertex Opposition Ascendant.txt'
'outVertex Opposition Jupiter.txt'
'outVertex Opposition Mars.txt'
'outVertex Opposition Mercury.txt'
'outVertex Opposition Midheaven.txt'
'outVertex Opposition Moon.txt'
'outVertex Opposition Neptune.txt'
'outVertex Opposition Pluto.txt'
'outVertex Opposition Saturn.txt'
'outVertex Opposition Sun.txt'
'outVertex Opposition Uranus.txt'
'outVertex Opposition Venus.txt'
'outVertex Quincunx Ascendant.txt'
'outVertex Quincunx Jupiter.txt'
'outVertex Quincunx Mars.txt'
'outVertex Quincunx Mercury.txt'
'outVertex Quincunx Midheaven.txt'
'outVertex Quincunx Moon.txt'
'outVertex Quincunx Neptune.txt'
'outVertex Quincunx Pluto.txt'
'outVertex Quincunx Saturn.txt'
'outVertex Quincunx Sun.txt'
'outVertex Quincunx Uranus.txt'
'outVertex Quincunx Venus.txt'
'outVertex Sextile Ascendant.txt'
'outVertex Sextile Jupiter.txt'
'outVertex Sextile Mars.txt'
'outVertex Sextile Mercury.txt'
'outVertex Sextile Midheaven.txt'
'outVertex Sextile Moon.txt'
'outVertex Sextile Neptune.txt'
'outVertex Sextile Pluto.txt'
'outVertex Sextile Saturn.txt'
'outVertex Sextile Sun.txt'
'outVertex Sextile Uranus.txt'
'outVertex Sextile Venus.txt'
'outVertex Square Ascendant.txt'
'outVertex Square Jupiter.txt'
'outVertex Square Mars.txt'
'outVertex Square Mercury.txt'
'outVertex Square Midheaven.txt'
'outVertex Square Moon.txt'
'outVertex Square Neptune.txt'
'outVertex Square Pluto.txt'
'outVertex Square Saturn.txt'
'outVertex Square Sun.txt'
'outVertex Square Uranus.txt'
'outVertex Square Venus.txt'
'outVertex Trine Ascendant.txt'
'outVertex Trine Jupiter.txt'
'outVertex Trine Mars.txt'
'outVertex Trine Mercury.txt'
'outVertex Trine Midheaven.txt'
'outVertex Trine Moon.txt'
'outVertex Trine Neptune.txt'
'outVertex Trine Pluto.txt'
'outVertex Trine Saturn.txt'
'outVertex Trine Sun.txt'
'outVertex Trine Uranus.txt'
'outVertex Trine Venus.txt'
'outVertex in Aquarius.txt'
'outVertex in Aries.txt'
'outVertex in Cancer.txt'
'outVertex in Capricorn.txt'
'outVertex in Gemini.txt'
'outVertex in Leo.txt'
'outVertex in Libra.txt'
'outVertex in Pisces.txt'
'outVertex in Sagittarius.txt'
'outVertex in Scorpio.txt'
'outVertex in Taurus.txt'
'outVertex in Virgo.txt'

试一试,如果您有问题,请告诉我。请注意,我没有要测试的 Mac。

【讨论】:

    【解决方案2】:

    我发现了问题!从这一切中吸取的教训是,在 Mac OS 上从不使用 AWK,而是使用 GAWK。

    1. Dataset (here): 作为File.txt

    2. brew install gawk

    3. AWK 脚本:Command.awk 内:

    #!/usr/bin/awk -f
    
    /^#/{ h=substr($0,3); }
        { print > ( h ".txt") }
    
    1. gawk -f Command.awk File.txt
    2. 完成!

    所以我们学到的教训是,在 Mac OS 上永远不要使用 AWK,而是使用 GAWK

    感谢@David C. 的大力帮助和时间。没有你,我将无法通过这个。感谢 @James 在 Mac OS 上进行测试。

    【讨论】:

    • 对,同样,使用pcregrep,而不是grep
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    相关资源
    最近更新 更多