awk 合并 2 列并向 txt 文件添加一个额外的列
awk merging 2 columns and adding an extra column to txt file
我以前这样做没问题,但这次我做不到,我不明白为什么.....
我的原始文件是
1002 10214
1002 10220
1002 10222
1002 10248
1002 10256
我需要创建一个合并上面两列的新文件,并添加值为 1 的第二列
所需的输出应如下所示
100210214 1
100210220 1
100210222 1
100210248 1
100210256 1
我尝试了以下 awk 命令,首先将 2 列打印为 1 到 tmp 文件中,然后添加带有“1”的额外列
cat input.txt | awk '{ print ()}' > tmp1.txt
cat tmp1.txt | awk ' {print [=12=], (1) }' > output.txt
虽然第一个命令似乎工作正常,但第二个却不行
tmp1.txt(确定)
100210214
100210220
100210222
100210248
100210256
output.txt(不行)
10210214
10210220
10210222
10210248
10210256
使用了“1"comes in the front of the first column, not sure why, even replacing the first 2 characters. Is it because the original input file is different (may be "space”而不是制表符)?
当输入文件有 Windows 行结尾(即 \r\n
)时会发生这种情况。您可以使用此命令修复它:
dos2unix file
然后用这个得到想要的输出:
awk '{=;=1}1' file
能否请您尝试以下。
awk 'BEGIN{OFS="\t"} {sub(/\r$/,"");print ,"1"}' Input_file
我以前这样做没问题,但这次我做不到,我不明白为什么.....
我的原始文件是
1002 10214
1002 10220
1002 10222
1002 10248
1002 10256
我需要创建一个合并上面两列的新文件,并添加值为 1 的第二列
所需的输出应如下所示
100210214 1
100210220 1
100210222 1
100210248 1
100210256 1
我尝试了以下 awk 命令,首先将 2 列打印为 1 到 tmp 文件中,然后添加带有“1”的额外列
cat input.txt | awk '{ print ()}' > tmp1.txt
cat tmp1.txt | awk ' {print [=12=], (1) }' > output.txt
虽然第一个命令似乎工作正常,但第二个却不行
tmp1.txt(确定)
100210214
100210220
100210222
100210248
100210256
output.txt(不行)
10210214
10210220
10210222
10210248
10210256
使用了“1"comes in the front of the first column, not sure why, even replacing the first 2 characters. Is it because the original input file is different (may be "space”而不是制表符)?
当输入文件有 Windows 行结尾(即 \r\n
)时会发生这种情况。您可以使用此命令修复它:
dos2unix file
然后用这个得到想要的输出:
awk '{=;=1}1' file
能否请您尝试以下。
awk 'BEGIN{OFS="\t"} {sub(/\r$/,"");print ,"1"}' Input_file