Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

awk - 在脚本中awk设置命令行选项(awk set command line options in script)

I'm curious about how to set command-line options in awk script, like -F for field separator.

(我很好奇如何在awk脚本中设置命令行选项,例如-F作为字段分隔符。)

I try to write the shebang line like

(我试着像这样写shebang行)

#!/usr/bin/awk -F ":" -f

and get the following error:

(并得到以下错误:)

awk: 1: unexpected character '.'

For this example, I can do with

(对于这个例子,我可以)

BEGIN {FS=":"}

but I still want to know a way to set all those options.

(但我仍然想知道一种设置所有这些选项的方法。)

Thanks in advance.

(提前致谢。)

EDIT:

(编辑:)

let's use another example that should be easy to test.

(让我们使用另一个易于测试的示例。)

inputfile:

(输入文件:)

1
2
3
4

test.awk:

(test.awk:)

#!/usr/bin/awk -d -f
{num += $1}
END { print num}

run

(跑)

/usr/bin/awk -d -f test.awk inputfile

will get 10 and generate a file called awkvars.out with some awk global variables in it.

(将得到10并生成一个名为awkvars.out的文件,其中包含一些awk全局变量。)

but

(但)

./test.awk inputfile

will get

(会得到)

awk: cmd. line:1: ./test.awk
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: ./test.awk
awk: cmd. line:1:   ^ unterminated regexp

if I remove '-d' from shebang line,

(如果我从shebang行中删除“ -d”,)

./test.awk inputfile

will normally output 10.

(通常会输出10。)

My question is that whether there is a way to write "-d" in test.awk file to generate awkvars.out file?

(我的问题是,是否可以在test.awk文件中写入“ -d”以生成awkvars.out文件?)

  ask by Zhu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let's say following is our Input_file, which we are going to use for all mentioned solutions here.

(假设以下是我们的Input_file,我们将在这里将其用于所有提及的解决方案。)

cat Input_file
a,b,c,d
ab,c


1st way of setting Field separator: 1st simple way will be setting FS value in BEGIN section of awk program file.

(第一种设置字段分隔符:第一种简单的方法是在awk程序文件的BEGIN部分中设置FS值。)

Following is our .awk file.

(以下是我们的.awk文件。)

cat file1.awk
BEGIN{
  FS=","
}
{
  print $1"..."$2
}

Now when we run the code following output will come:

(现在,当我们运行代码时,将输出以下内容:)

/usr/local/bin/awk -f file1.awk Input_file
a...b
ab...c


2nd way of setting field separator: 2nd way will be pass FS value before reading Input_file like as follows.

(设置字段分隔符的第二种方法第二种方法是在读取Input_file之前传递FS值,如下所示。)

/usr/local/bin/awk -f file.awk FS="," Input_file

Example: Now following is the file.awk file which has awk code.

(示例:现在是具有awk代码的file.awk文件。)

cat file.awk
{
 print $1".."$2
}

Now when we run awk file with awk -f .. command as follows will be result.

(现在,当我们使用awk -f ..命令运行awk文件时,将得到以下结果。)

/usr/local/bin/awk -f file.awk FS=","  Input_file
a..b
ab..c

Which means it is picking up the field separator as , in this above program.

(这意味着它是拿起场分离器,在这上面的程序。)



3rd way of setting field separator: We can set field separator in awk -f programs like how we do for usual awk programs using -F',' option as follows.

(设置字段分隔符的第三种方法:我们可以在awk -f程序中设置字段分隔符,就像我们如何使用-F','选项对常规awk程序进行设置-F','如下所示。)

/usr/local/bin/awk -F',' -f  file.awk Input_file
a..b
ab..c


4th way of setting field separator: We could mention field separator as a variable by using -v option on command line while running file.awk script as follows.

(设置字段分隔符的 file.awk在运行file.awk脚本时,可以通过在命令行上使用-v选项将字段分隔符作为变量提及,如下所示。)

/usr/local/bin/awk -v FS=',' -f  file.awk Input_file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...