2017-03-04 57 views
0

输入文件包含以下行:在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 
+0

用四个空格前缀代码/数据。请看[编辑帮助](http://stackoverflow.com/editing-help)。 – Cyrus

+1

简单地追加文件n次,比如'cat file1 >> file2'在循环中怎么样? –

回答

2

使用seqxargs

seq 2 | xargs -Inone cat file 
+2

无论在所有系统上都不可用“seq”,该解决方案都能正常工作并解决问题。 downvoter可以添加一个解释吗?从我+1。 – jm666

1

另一种解决方案可以是

#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 
0

printfbrace 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 
0

一个小溶液用于打印file 3次:

cat $(yes file | head -n 3) 

的命令替换$()扩展为cat file file file。 这只适用于没有空格的文件名。如果您的文件名包含空格或制表符,请设置IFS=$'\n'

相关问题