輸入文件包含以下行:在Linux中:如何以相同的順序在文件中重複多行?
a
b
c
我想要的輸出爲(n次):
a
b
c
a
b
c
我試過下面的命令,但它並沒有維持秩序
while read line; do for i in {1..4}; do echo "$line"; done; done < file
但輸出
a
a
b
b
c
c
輸入文件包含以下行:在Linux中:如何以相同的順序在文件中重複多行?
a
b
c
我想要的輸出爲(n次):
a
b
c
a
b
c
我試過下面的命令,但它並沒有維持秩序
while read line; do for i in {1..4}; do echo "$line"; done; done < file
但輸出
a
a
b
b
c
c
使用seq
與xargs
:
seq 2 | xargs -Inone cat file
無論在所有系統上都不可用「seq」,該解決方案都能正常工作並解決問題。 downvoter可以添加一個解釋嗎?從我+1。 – jm666
另一種解決方案可以是
#multicat count filename(s)
multicat() {
local count=$1
shift
for((i=0;i < $count; i++)) {
cat "[email protected]"
}
}
multicat 3 abc # outputs the "abc" file 3 times
printf
和brace expansion
可以使用重複的字符串N次,然後可以作爲輸入傳遞給cat
其執行其級聯的作業
$ printf "file %.s" {1..4}
file file file file $
$ cat $(printf "file %.s" {1..4})
a
b
c
a
b
c
a
b
c
a
b
c
隨着perl
,如果文件是足夠小的內存要求可以咕嚕咕嚕整個
$ perl -0777 -ne 'print $_ x 2' file
a
b
c
a
b
c
一個小溶液用於打印file
3次:
cat $(yes file | head -n 3)
的命令替換$()
擴展爲cat file file file
。 這隻適用於沒有空格的文件名。如果您的文件名包含空格或製表符,請設置IFS=$'\n'
。
用四個空格前綴代碼/數據。請看[編輯幫助](http://stackoverflow.com/editing-help)。 – Cyrus
簡單地追加文件n次,比如'cat file1 >> file2'在循環中怎麼樣? –