我有一个这样的文件(test1.txt):
100 100
200 200
300 HR
400 IT
500 500
600 600
Run Code Online (Sandbox Code Playgroud)
我想要这样的输出:
100 100 0
200 200 0
300 HR 1
400 IT 1
500 500 0
600 600 0
Run Code Online (Sandbox Code Playgroud)
我已经尝试运行此命令:
#!/bin/bash
awk1=`awk '{print $1}' test1.txt`
awk2=`awk '{print $2}' test1.txt`
if [ "$awk1" = "$awk2" ]; then
echo "0"
else
echo "1"
fi
Run Code Online (Sandbox Code Playgroud)
但结果并不像我预期的那样。
你能不能试试以下。
awk '{$(NF+1)=$1==$2?0:1} 1' Input_file
Run Code Online (Sandbox Code Playgroud)
或者让输出 TAB 分隔尝试:
awk 'BEGIN{OFS="\t"} {$(NF+1)=$1==$2?0:1} 1' Input_file
Run Code Online (Sandbox Code Playgroud)
说明:为上述代码添加详细说明。
awk ' ##Starting awk program from here.
{
$(NF+1)=$1==$2?0:1 ##Creating a new NF+1 field which will be newer last field and its value will be decided as per condition:
} ##If $1 and $2 are equal then set 0 or set 1 its value.
1 ##1 will print the complete line value here(including new last field value).
' Input_file ##Mentioning Input_file name here.
Run Code Online (Sandbox Code Playgroud)