如何在Bash脚本中运行AWK?

时间:2022-12-05 23:53:00

The problem is related to my answer here.

这个问题与我的回答有关。

I cannot get the following code to work

我无法使用以下代码

#!/bin/bash 
wget http://www.google.com/coop/cse?cx=017685108214920485934%3Aizx6pzojwka -O CSE-spanien
awk '/^searches [1-9]* sites, including:/ { print $2 }' CSE-spanien

Example of the Error message [edited]

错误消息示例[已编辑]

$./code
--2009-04-14 19:01:32--  http://www.google.com/coop/cse?cx=017685108214920485934%3Aizx6pzojwka
Resolving www.google.com... 74.125.79.103, 74.125.79.99, 74.125.79.147, ...
Connecting to www.google.com|74.125.79.103|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: `cse?cx=017685108214920485934:izx6pzojwka.6'

    [ <=>                                                                                                               ] 8,096       --.-K/s   in 0s      

2009-04-14 19:01:33 (78.0 MB/s) - `cse?cx=017685108214920485934:izx6pzojwka.6' saved [8096]

./code: line 3: -O: command not found
awk(57264) malloc: *** error for object 0x4eee0: Non-aligned pointer being freed (2)
*** set a breakpoint in malloc_error_break to debug
awk: cmd. line:1: fatal: cannot open file `/Users/Masi/Documents/test/CSE-spanien' for reading (No such file or directory)

How can you run AWK in a Bash script?

如何在Bash脚本中运行AWK?

3 个解决方案

#1


You're missing -O CSE-spanien at the end of your wget line. As a result, CSE-spanien doesn't exist - wget is saving it the HTML with the filename cse?cx=017685108214920485934:izx6pzojwka.2.

你错过了你的wget线末尾的CSE-spanien。因此,CSE-spanien不存在 - wget将其保存为文件名为cse的HTML?cx = 017685108214920485934:izx6pzojwka.2。

#2


Some random thoughts

一些随意的想法

  • Check you path---the one used in the script environment. Is awk named "awk" in you environemnt?
  • 检查路径---脚本环境中使用的路径。 awk在你的environemnt中被命名为“awk”吗?

  • Consider using HERE documents for non-trivial scripts
  • 考虑将HERE文档用于非平凡的脚本

  • Do you need to redirect the output? What about the standard error?
  • 你需要重定向输出吗?标准错误怎么样?

  • Should you capture the exit state of the previous process and do some decision making first?
  • 您是否应该捕获先前流程的退出状态并首先做出决策?

  • From you error message it looks like you could use a if [ -f FileToUse ]. If you don't know for certain whee wget will put the output, you might want to force it (-O).
  • 从您的错误消息看起来您可以使用if [-f FileToUse]。如果你不知道某个we会输出输出,你可能想强制它(-O)。

#3


The problem is that wget doesn't create a CSE-spanien file in the current working directory. I suggest you to check where the file is created and add full or relative path of file in your awk command. It would also be wise to test if the expected file exists (test -f CSE-spanien && awk ...)

问题是wget不会在当前工作目录中创建CSE-spanien文件。我建议你检查文件的创建位置,并在awk命令中添加文件的完整或相对路径。测试预期的文件是否存在也是明智的(测试-f CSE-spanien && awk ...)

#1


You're missing -O CSE-spanien at the end of your wget line. As a result, CSE-spanien doesn't exist - wget is saving it the HTML with the filename cse?cx=017685108214920485934:izx6pzojwka.2.

你错过了你的wget线末尾的CSE-spanien。因此,CSE-spanien不存在 - wget将其保存为文件名为cse的HTML?cx = 017685108214920485934:izx6pzojwka.2。

#2


Some random thoughts

一些随意的想法

  • Check you path---the one used in the script environment. Is awk named "awk" in you environemnt?
  • 检查路径---脚本环境中使用的路径。 awk在你的environemnt中被命名为“awk”吗?

  • Consider using HERE documents for non-trivial scripts
  • 考虑将HERE文档用于非平凡的脚本

  • Do you need to redirect the output? What about the standard error?
  • 你需要重定向输出吗?标准错误怎么样?

  • Should you capture the exit state of the previous process and do some decision making first?
  • 您是否应该捕获先前流程的退出状态并首先做出决策?

  • From you error message it looks like you could use a if [ -f FileToUse ]. If you don't know for certain whee wget will put the output, you might want to force it (-O).
  • 从您的错误消息看起来您可以使用if [-f FileToUse]。如果你不知道某个we会输出输出,你可能想强制它(-O)。

#3


The problem is that wget doesn't create a CSE-spanien file in the current working directory. I suggest you to check where the file is created and add full or relative path of file in your awk command. It would also be wise to test if the expected file exists (test -f CSE-spanien && awk ...)

问题是wget不会在当前工作目录中创建CSE-spanien文件。我建议你检查文件的创建位置,并在awk命令中添加文件的完整或相对路径。测试预期的文件是否存在也是明智的(测试-f CSE-spanien && awk ...)