给定一个包含如下数据的文件(即stores.dat文件):
id storeNo type
2ttfgdhdfgh 1gfdkl-28 kgdl
9dhfdhfdfh 2t-33gdm dgjkfndkgf
期望输出:
id |storeNo |type
2ttfgdhdfgh |1gfdkl-28 |kgdl
9dhfdhfdfh |2t-33gdm |dgjkfndkgf
希望在这三个切割范围之间添加“|”分隔符:
cut -c1-18,19-30,31-40 stores.dat
如何在每个切割值之间插入分隔符的语法是什么?
如果您能提供修剪值的选项,将获得奖励分数(例如):
id|storeNo|type
2ttfgdhdfgh|1gfdkl-28|kgdl
9dhfdhfdfh|2t-33gdm|dgjkfndkgf\
更新(感谢Mat的答案)我最终成功实现了这个解决方案——(它有点混乱,但我的bash版本似乎不支持更优雅的算术运算)
#!/bin/bash
unpack=""
filename="$1"
while [ $# -gt 0 ] ; do
arg="$1"
if [ "$arg" != "$filename" ]
then
firstcharpos=`echo $arg | awk -F"-" '{print $1}'`
secondcharpos=`echo $arg | awk -F"-" '{print $2}'`
compute=`(expr $firstcharpos - $secondcharpos)`
compute=`(expr $compute \* -1 + 1)`
unpack=$unpack"A"$compute
fi
shift
done
perl -ne 'print join("|",unpack("'$unpack'", $_)), "\n";' $filename
Usage: sh test.sh input_file 1-17 18-29 30-39
tr
命令。 - jaypal singh