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