awk如何打印第1-4列,在第4列和行尾之间使用前导制表符,保留制表符
我有一个这样的文件(1000行): 在第4列和第5列之间,我想保留一些选项卡。 我期望的结果是:awk如何打印第1-4列,在第4列和行尾之间使用前导制表符,保留制表符,awk,tabs,Awk,Tabs,我有一个这样的文件(1000行): 在第4列和第5列之间,我想保留一些选项卡。 我期望的结果是: blabla.xml,36,14601,lllpxl01,\t\t\tselect 1 from dual 如果\t\t\t是6个文字字符,实际上并不意味着是3个字符 echo "blabla.xml 36 14601 lllpxl01\t\t\tselect 1 from dual" \ | awk '{ # change a consecutive run of space ch
blabla.xml,36,14601,lllpxl01,\t\t\tselect 1 from dual
如果
\t\t\t
是6个文字字符,实际上并不意味着是3个
字符
echo "blabla.xml 36 14601 lllpxl01\t\t\tselect 1 from dual" \
| awk '{
# change a consecutive run of space chars to a single "," 3X
sub(/ */, ",");sub(/ */, ",");sub(/ */, ",")
# use string literal match targets ("\t")
sub("\t", ",\\t")
# match any string literal "\t" and change to "\\t"
# (which will display as "\t" in the output)
gsub("\t", "\\t")
print}'
echo "blabla.xml 36 14601 lllpxl01\t\t\tselect 1 from dual" \
| awk '{
# change a consecutive run of space chars to a single "," 3X
sub(/ */, ",");sub(/ */, ",");sub(/ */, ",");
# change the first reg-exp match for `/\t/` to display as ",\t"
sub(/\t/, ",\\t")
# match any <TAB> reg-exp (/\t/) and and change to "\\t"
# (which will display as "\t" in the output)
gsub(/\t/, "\\t"); print}'
输出
blabla.xml,36,14601,lllpxl01,\t\t\tselect 1 from dual
如果
\t\t\t
是指3个
字符
echo "blabla.xml 36 14601 lllpxl01\t\t\tselect 1 from dual" \
| awk '{
# change a consecutive run of space chars to a single "," 3X
sub(/ */, ",");sub(/ */, ",");sub(/ */, ",")
# use string literal match targets ("\t")
sub("\t", ",\\t")
# match any string literal "\t" and change to "\\t"
# (which will display as "\t" in the output)
gsub("\t", "\\t")
print}'
echo "blabla.xml 36 14601 lllpxl01\t\t\tselect 1 from dual" \
| awk '{
# change a consecutive run of space chars to a single "," 3X
sub(/ */, ",");sub(/ */, ",");sub(/ */, ",");
# change the first reg-exp match for `/\t/` to display as ",\t"
sub(/\t/, ",\\t")
# match any <TAB> reg-exp (/\t/) and and change to "\\t"
# (which will display as "\t" in the output)
gsub(/\t/, "\\t"); print}'
在这两个示例中,我都使用“\\t”
输出一个文本'\t',它将被shell和大多数其他程序解释为
字符。有了这些细微的区别,你可以选择一个你需要的输出
当然,这是一个非常脆弱的解决方案,因为您可能会意识到“哦,我还需要两个补丁”,这将改变您需要使用的步骤
相反,我强烈建议返回输入数据的源代码,让它准确地输出您需要的内容,或者至少使输出处于一种状态,即单个gsub(/*/,“,”)
(或类似)将为您提供可用的结果
IHTH.有些人喜欢这样:
awk '{sub(/ /,",");sub(/ /,",");sub(/ +/,",");sub(/\\t/,",&")}1'
blabla.xml,36,14601,lllpxl01,\t\t\tselect 1 from dual
如果帖子中的\t
是选项卡,则:
awk '{sub(/ /,",");sub(/ /,",");sub(/ +/,",");sub(/\t/,",&")}1' file
blabla.xml,36,14601,lllpxl01, select 1 from dual
真棒的答案。我离我想要的更近了。我可以按原样使用它,如果标签保持为实际标签就好了。我喜欢一句台词:)