2011-02-17 90 views
1

我有这个文件elif的条件语句:没有工作

The number is %d0The number is %d1The number is %d2The number is %d3The number is %d4The number is %d5The number is %d6The... 
The number is %d67The number is %d68The number is %d69The number is %d70The number is %d71The number is %d72The.... 
The number is %d117The number is %d118The number is %d119The number is %d120The number is %d121The number is %d122 

我想填充它喜欢:

The number is %d0 The number is %d1 The number is %d2 The number is %d3 The number is %d4 The number is %d5 The number is %d6 
The number is %d63 The number is %d64 The number is %d65 The number is %d66 The number is %d67 The number is %d68 The number is %d69 
d118The number is %d119The number is %d120The number is %d121The number is %d122The number is %d123The number is %d124The 

请告诉我如何通过shell脚本 我正在做Linux的

+0

顺便说一句,你只有在第一个选择中有一个倒退。 – 2011-02-17 09:41:51

+1

你在用什么外壳?庆典? – marcog 2011-02-17 09:41:54

回答

1

编辑:

氏S单命令管道应该做你想要什么:

sed 's/\(d[0-9]\+\)/\1 /g;s/\(d[0-9 ]\{3\}\) */\1/g' test2.txt >test3.txt 
#     ^three spaces here 

说明:

对于继“d”的数字每个序列,其后添加三个空格。 (我会用“X”来表示空格。)

d1 becomes d1XXX 
d10 becomes d10XXX 
d100 becomes d100XXX 

现在(分号之后的部分),捕捉每一个“d”和接下来的三个字符必须是数字或空格,并将其输出但不任何空间之外。

d1XXX becomes d1XX 
d10XXX becomes d10X 
d100XXX becomes d100 

如果你想为你似乎在您的样本数据显示包线,然后做这个:

sed 's/\(d[0-9]\+\)/\1 /g;s/\(d[0-9 ]\{3\}\) */\1/g' test2.txt | fold -w 133 >test3.txt 

您可能需要调整fold命令的参数,使之出来吧。

有没有必要ifgrep,循环等

原来的答复:

首先,你真的需要说哪个壳您正在使用,但因为你有eliffi,我假设它是伯恩派生的。

基于这个假设,你的脚本没有意义。

  • ifelif的括号是不必要的。在这种情况下,他们创建了一个无用的子shell。
  • ifelifsed命令说:“如果该模式被发现,复制保留空间(它是空的,顺便说一句),以模式空间和输出,并输出所有其他线路。
  • 第一sed命令将始终为为真,因此elif将永远不会执行。sed始终返回true,除非出现错误。

这可能是你的原意:

if grep -Eqs 'd[0-9]([^0-9]|$)' test2.txt; then 
    sed 's/\(d[0-9]\)\([^0-9]\|$\)/\1 \2/g' test2.txt >test3.txt 
elif grep -Eqs 'd[0-9][0-9]([^0-9]|$)' test2.txt; then 
    sed 's/\(d[0-9][0-9]\)\([^0-9]\|$\)/\1 \2/g' test2.txt >test3.txt 
else 
    cat test2.txt >test3.txt 
fi 

但我不知道,如果一切可以通过类似这样一行代码代替:

sed 's/\(d[0-9][0-9]?\)\([^0-9]\|$\)/\1 \2/g' test2.txt >test3.txt 

因为我不知道什么test2.txt看起来像,这只是猜测的一部分。