如何仅截断给定的列长度?

如何仅截断给定的列长度?

输入:

a@notebook:~$ cat in.csv
'XYZ843141'^'ASDFSAFXYVFSHGDSDg sdGDS  dsGDSgfa assfd faSDFAS saDFSAFD adFSA343fa sdfSADF'^'BAAAR'^'YYY'^'..... and so on, further columns
'YYZ814384'^'ASfdEtRiuognfnseaFREQTzKb   aSFfdsaADSFSA  adsFdsa34 34 ASFfsas  saftrzj etrzrasdfasffasf safs'^'foooobaaar'^'ZZZ'^'..... and so on, further columns

输出:

a@notebook:~$ cat in.csv | SOMEMAGIC
'XYZ843141'^'ASDFSAFXYVFSHGDSDg s'^'BAAAR'^'YYY'^'..... and so on, further columns
'YYZ814384'^'ASfdEtRiuognfnseaFRE'^'foooobaaar'^'ZZZ'^'..... and so on, further columns

我的问题: 如果:

'^'

是分隔符,那么 SOMEMAGIC (awk/sed?)如何将第二列截断为给定长度?示例:最多 20 个字符,来自:

ASDFSAFXYVFSHGDSDg sdGDS  dsGDSgfa assfd faSDFAS saDFSAFD adFSA343fa sdfSADF

对此:

ASDFSAFXYVFSHGDSDg s

并保留所有其他内容:\

答案1

> awk -v OFS="'^'" -F"'\\\\^'" '{if(length($2)>20) $2=substr($2,1,20); print;}' file
'XYZ843141'^'ASDFSAFXYVFSHGDSDg s'^'BAAAR'^'YYY'^'..... and so on, further columns
'YYZ814384'^'ASfdEtRiuognfnseaFRE'^'foooobaaar'^'ZZZ'^'..... and so on, further columns

答案2

这是一个简单的 sed 示例,用于从字符串中仅获取 20 个字符:

$ str = "ASDFSAFXYVFSHGDSDg sdGDS  dsGDSgfa assfd faSDFAS saDFSAFD adFSA343fa sdfSADF"

$ echo $str | sed -e 's/^\(.\{20\}\).*/\1/'
ASDFSAFXYVFSHGDSDg s

或者命令:

$ echo $str | cut -c 1-20

编辑:这个怎么样:

$ echo $str | awk -F "^" '{print $1,$2}'
'XYZ843141' 'ASDFSAFXYVFSHGDSDg s'

答案3

你可以试试这个:

paste -d^ <(cut -f1 -d^ in.csv) \
          <(cut -f2 -d^ in.csv | sed -e 's/^\(.\{21\}\).*/\1'\''/')\
          <(cut -f3- -d^ in.csv)

^然而,如果某些字段存在的话,这将失败。

相关内容