2014-10-19 60 views
0

需要在bash的副本線幫助5倍..複製一行並粘貼在bash Linux的

**Input: (input.txt)** 

http://localhost.com/123/test.png 
http://localhost.com/456/test.png 

中的newfile這樣既行應粘貼5倍...... ,所以我想下面的輸出像這樣..

**Output: (output.txt)** 

http://localhost.com/123/test.png 
http://localhost.com/123/test.png 
http://localhost.com/123/test.png 
http://localhost.com/123/test.png 
http://localhost.com/123/test.png 
http://localhost.com/456/test.png 
http://localhost.com/456/test.png 
http://localhost.com/456/test.png 
http://localhost.com/456/test.png 
http://localhost.com/456/test.png 

那麼怎麼可能? 能有人給shell腳本來實現這一點..

謝謝

+1

你可以得到從[這裏]更多的答案(http://unix.stackexchange.com/questions/81904/repeat-each-line-multiple-times ) – 2014-10-19 16:14:24

+0

Thanx for link @avinash – TonyStark 2014-10-20 09:43:54

回答

0

您可以使用此:

sed 'p;p;p;p' input.txt > output.txt 
+0

@cyprs thanx解答 – TonyStark 2014-10-19 16:04:40

0

試試這個:

awk '{for (i=0;i<5;i++) print $NF }' input.txt 
+0

thanx for code it works too .. – TonyStark 2014-10-19 16:05:47

+0

@almas如果將'$ NF'更改爲'$ 0',因爲如果一行中包含空格,命令將不起作用。 – 2014-10-19 16:07:43

0

你可以試試下面的Perl命令也。

perl -ne 'print "$_"x5' file 

$_是在Perl的特殊變量,其存儲當前行等$0做在AWK。所以"$_"x5會重複當前行5次。

+0

thanx爲答案,它的作品太 – TonyStark 2014-10-19 16:05:18

0

這裏是經典的方式用循環:

while read line; do 
    for i in {1..5}; do 
    echo "before_text${line}after_text" 
    done 
done < input.txt 
+0

thanx答案,有沒有什麼辦法得到不同的後綴在輸出文件..ex :: localhost.com/123/A localhost.com/123/B – TonyStark 2014-10-19 16:09:40

+0

如果你知道你想要迭代的內容,把它放在一個[數組](http://www.tldp.org/LDP/abs/html/arrays.html)中,以0開始你的範圍,並用它來索引陣列。 – whereswalden 2014-10-19 21:13:52

+0

完成,在{A..E}中更改了我;它的工作原理... – TonyStark 2014-10-20 04:02:51