tags:

views:

61

answers:

4

I would like to write a small script using the standard linux shell scripting tools (sed, awk, etc.) to create one output file per line of input. For example, given the following input file 'input':

line1
line2
line3

I would like to run the command.

$ cat input | my_prog

Where 'my_prog' is a script that creates three output files, out.1, out.2, out.3

$ cat out.1
line1 

$ cat out.2
line2

$ cat out.3
line3
+2  A: 
count=0
for line in $(cat input)
do
echo $line > out.$count
let count=count+1
done
ennuikiller
That splits each line per word. You should do `while read -r line ... done < input`
Dennis Williamson
+1  A: 
perl -ne 'open my $fh, ">out.$." && print $fh $_' input.txt
Greg Bacon
+1  A: 

using awk

awk '{print $0 > "out_"++d".txt"}' file
I couldn't get this to work.
how are you executing it??
from the command line.
then you are doing it wrong. show your error messages, and what didn't work. provide more information on the failure next time!
A: 
#!/bin/bash

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