#!/bin/bashunpack=""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 shiftdoneperl -ne 'print join("|",unpack("'$unpack'", $_)), "\n";' $filename用法:sh test.sh input_file 1-17 18-29 30-39Usage: sh test.sh input_file 1-17 18-29 30-39推荐答案如果您不害怕使用perl,这里有一个单行代码:If you're not afraid of using perl, here's a one-liner:$ perl -ne 'print join("|",unpack("A17A12A10", $_)), "\n";' input unpack 调用将从输入行中提取一个17个字符的字符串,然后一个12个字符的字符串,然后一个10个字符的字符串,然后将它们返回到数组中(带空格). join 添加 | .The unpack call will extract one 17 char string, then a 12 char one, then a 10 char one from the input line, and return them in an array (stripping spaces). join adds the |s.如果您希望输入列采用 x-y 格式,而无需编写真实"脚本,则可以像这样修改它(但很丑陋):If you want the input columns to be in x-y format, without writing a "real" script, you could hack it like this (but it's ugly):#!/bin/bashunpack=""while [ $# -gt 1 ] ; do arg=$(($1)) shift unpack=$unpack"A"$((-1*$arg+1))doneperl -ne 'print join("|",unpack("'$unpack'", $_)), "\n";' $1用法: t.sh 1-17 18-29 30-39 input_file . 这篇关于unix-剪切命令(添加自己的定界符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-11 17:48