2012-06-05 251 views
0

我解析文件以获取选定的字符串并构建到一行,但是,我不知道如何在shell脚本中执行该操作(如// add ...所示)如何追加字符串

while read line 
do 
    tt=`echo $line | cut -d'|' -f2 | cut -d'"' -f1` 
    //add a $total = add all tt parts into a big string seperate by ", " 
done < tmp_file 

echo $total >> outfile 

谢谢

+0

只是附和什么在'tt'(加逗号)输出? – nhahtdh

+0

我需要为每个文件构建一行($ total),并且该行由许多$ tt作成 –

+0

可以不用' echo'。只是我的想法会招致相当多的磁盘访问。 – nhahtdh

回答

1

您使用赋值和变量扩展附加在shell:

total="${total}, ${tt}" 

花括号({})没有必要在这种情况下,但我觉得他们帮助distinguis h变量时,他们是这样的彼此相邻。

这会给你一个领先的“,”。你可以解决它是这样的:

total="${total:+${total}, }${tt}" 

${variable:+value}结构只有variable设置扩展到value

+0

非常感谢! –

0

的代码必须是这样的,我认为^ _ ^”

while read line 

do 

    $tt=`echo $line | cut -d'|' -f2 | cut -d'"' -f1` 

    $total .= $tt.", "; 

done < tmp_file 

echo $total >> outfile 

我想这是所有^^:D只是错过了一个点的串联:P大声笑:d