找到下一个分隔符ksh

时间:2013-05-31 14:39:52

标签: unix ksh separator

我有一个由假脱机创建的文件。此文件的列由分隔符分隔:#@; 有没有办法在分隔符之间找到数据?我的意思是例如:

#@;Hello #@;World #@;!!!

我必须找到你好世界然后!!!我试过这种方式但不起作用:

tp=${ENDFL##*@#}
          HELLOSTRING=`printf '"%s"\n' "${tp%%#@;*}"`

有什么想法吗?感谢

2 个答案:

答案 0 :(得分:1)

使用翻译命令:

  echo "#@;Hello #@;World #@;!!!"  | tr '#@;' '^' | tr -s '^' 

输出:

 Kaizen ~
 $ echo "#@;Hello #@;World #@;!!!"  | tr '#@;' '^' | tr -s '^'
   ^Hello ^World ^!!!

说明:

第一个tr取代'#@;'用^分隔符,但它会这样做三次。

“#,@,;”是三个单独的文字,所以它分别为所有三个

第二个tr抑制多个^到一个的出现。

因此,你得到^ ^分隔的输出为“^ Hello ^ World ^ !!!”

对于你的文件只是cat filename然后将它传递给translate命令,之后你可以根据需要使用AWK,cut或其他任何格式或提取。

答案 1 :(得分:0)

如果你必须处理一堆行

sed -e 's/#@;//g' input.dat

查找分隔符之间的数据(假设您在tp=建议的变量中有值)

echo "${tp/#@;/}"

如果您想使用这些部件

origIFS=$IFS        # always kep the original $IFS save
IFS='§'            # define a InputFieldSeparator which will not be used in your input
set ${tp//#@;/$IFS} # reduce separator to one char and set $1 ... based on that separator
IFS=$origIFS        # and restore $IFS when you are done
echo "'$1' '$2' '$3' '$4'"  # note that $1 is empty, it is before the first separator)