本文介绍了使用awk删除特定的空白并替换为分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linux和命令行的新手.我正在尝试找到一个命令,该命令将允许我将white space(在.csv文本文件中)替换为除第一个以外的所有字段的分号.请参见下面的示例,将不胜感激地收到任何帮助,我花了很长时间寻找解决方案.如果您有答案,请解释一下命令,以便我尝试并了解如何以及为什么.非常感谢.

I am new to linux and the command line. I am trying to find a command to that will allow me to replace white space (in a .csv text file) with a semi-colon for all fields except the first. Please see example below, any help would be gratefully received, I have spent a long time looking for a solution. If you do have an answer could you please explain the command so I can try and learn how and why. Many thanks.

输入文字示例:

0   k__Bacteria  p__Firmicutes   c__Bacilli             
1   k__Bacteria  p__Firmicutes   c__Clostridia      
2   k__Bacteria  p__Bacteroidetes    c__Bacteroidia     
3   k__Bacteria  p__Bacteroidetes    c__Bacteroidia

我需要的输出是

0   k__Bacteria;p__Firmicutes;c__Bacilli        
1   k__Bacteria;p__Firmicutes;c__Clostridia    
2   k__Bacteria;p__Bacteroidetes;c__Bacteroidia   
3   k__Bacteria;p__Bacteroidetes;c__Bacteroidia

推荐答案

$ cat file
0   k__Bacteria  p__Firmicutes   c__Bacilli     foo     bar
1   k__Bacteria  p__Firmicutes   c__Clostridia  the   quick     brown
2   k__Bacteria  p__Bacteroidetes    c__Bacteroidia     fox jumped      over
3   k__Bacteria  p__Bacteroidetes    c__Bacteroidia     the lazy dogs back

$ awk -v skip=1 '{match($0,"([^[:space:]]+[[:space:]]+){"skip"}"); head=substr($0,1,RSTART+RLENGTH); tail=substr($0,RSTART+RLENGTH+1); gsub(/[[:space:]]+/,";",tail); print head tail}' file
0   k__Bacteria;p__Firmicutes;c__Bacilli;foo;bar
1   k__Bacteria;p__Firmicutes;c__Clostridia;the;quick;brown
2   k__Bacteria;p__Bacteroidetes;c__Bacteroidia;fox;jumped;over
3   k__Bacteria;p__Bacteroidetes;c__Bacteroidia;the;lazy;dogs;back

$ awk -v skip=2 '{match($0,"([^[:space:]]+[[:space:]]+){"skip"}"); head=substr($0,1,RSTART+RLENGTH); tail=substr($0,RSTART+RLENGTH+1); gsub(/[[:space:]]+/,";",tail); print head tail}' file
0   k__Bacteria  p__Firmicutes;c__Bacilli;foo;bar
1   k__Bacteria  p__Firmicutes;c__Clostridia;the;quick;brown
2   k__Bacteria  p__Bacteroidetes;c__Bacteroidia;fox;jumped;over
3   k__Bacteria  p__Bacteroidetes;c__Bacteroidia;the;lazy;dogs;back

$ awk -v skip=3 '{match($0,"([^[:space:]]+[[:space:]]+){"skip"}"); head=substr($0,1,RSTART+RLENGTH); tail=substr($0,RSTART+RLENGTH+1); gsub(/[[:space:]]+/,";",tail); print head tail}' file
0   k__Bacteria  p__Firmicutes   c__Bacilli;foo;bar
1   k__Bacteria  p__Firmicutes   c__Clostridia;the;quick;brown
2   k__Bacteria  p__Bacteroidetes    c__Bacteroidia;fox;jumped;over
3   k__Bacteria  p__Bacteroidetes    c__Bacteroidia;the;lazy;dogs;back

这篇关于使用awk删除特定的空白并替换为分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:53