2009-12-19 26 views
1

我想使用标准的linux shell脚本工具(sed,awk等)编写一个小脚本来为每行输入创建一个输出文件。例如,给定以下输入文件“输入”:将stdin转换为每行一个文件

line1 
line2 
line3 

我想运行该命令。

$ cat input | my_prog 

其中 'my_prog' 是创建了三个输出文件,out.1,out.2脚本,out.3

$ cat out.1 
line1 

$ cat out.2 
line2 

$ cat out.3 
line3 
+0

这是部分一系列使用sed和awk的命令。我想知道是否有可能让sed将每个输入行映射到单独的输出管道。我想我会使用ennuikiller的解决方案,用我完整的sed命令管道替换“echo $ line> out。$ count”。 谢谢! – user74838 2009-12-19 21:26:17

回答

2
count=0 
for line in $(cat input) 
do 
echo $line > out.$count 
let count=count+1 
done 
+3

每个单词分割每行。您应该在读取-r行...完成时输入 – 2009-12-19 23:48:36

1
perl -ne 'open my $fh, ">out.$." && print $fh $_' input.txt 
1

用awk

awk '{print $0 > "out_"++d".txt"}' file 
+0

我无法使其工作。 – user74838 2009-12-20 00:17:31

+0

你怎么执行它? – ghostdog74 2009-12-20 00:33:05

+0

从命令行。 – user74838 2009-12-20 01:00:13

0
#!/bin/bash 

while IFS= read -r line; do 
    echo "$line" > out.$((++i)) 
done < /path/to/input