#!/bin/sh
while read line
do
if echo $line | grep source; then
line_no=`echo $line | sed 's/source\([0-9]*\)/\1/g'`
elif echo $line | grep END; then
: #ignore
else
echo $line >> out.$line_no.txt
fi
done < input_split.txt
Your post #1 didn't say anything about ignoring lines containing the string END as the code above does, said you wanted output files abc.txt and bcd.txt instead of the files produced by the above code ( out. table abc.txt and out. table bcd.txt ), will convert any sequence of one or more spaces and/or tabs to a single space character (which is not what I expected from the sample you provided), and is processing a file named input_split.txt instead of the file Input.txt that you said you wanted to process.
To get something closer to what you said you wanted, you might try something more like:
You didn't answer my question about what operating system you're using. If you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .
Unlike the open() system call (which opens the lowest available file descriptor), the shell file redirection operation >> "$x" closes standard output (file descriptor 1) if it is currently open and opens the file named by the expansion of $x as as file descriptor 1 with the append flag set.
If for some reason you are opening files with different file descriptors (instead of reusing a single file descriptor), you can close standard input with <&- , close standard output with >&- , close an input file using file descriptor n with n<&- , and close an output file using file descriptor n with n>&- .