$ /usr/xpg4/bin/awk '/CP START ACCOUNT/{close(x);x="F"++i;}{print > x;}' inputfile
/usr/xpg4/bin/awk: line 0 (NR=1): output file "": No such file or directory
How can I get the above to succeed
Also - can I get the split to only split the file after say 20 matches of "CP START ACCOUNT" ?
$ /usr/xpg4/bin/awk '/CP START ACCOUNT/{close(x);x="F"++i;}{print > x;}' inputfile
/usr/xpg4/bin/awk: line 0 (NR=1): output file "": No such file or directory
How can I get the above to succeed
Also - can I get the split to only split the file after say 20 matches of "CP START ACCOUNT" ?
Another approach is using a BASH script, but this is gonna run slower than awk:
#!/bin/bash
c=0
while read line
do
if [[ "$line" =~ "^CP START ACCOUNT" ]]
then
c=$(( c + 1 ))
echo "$line" >> F${c}.txt
else
echo "$line" >> F${c}.txt
fi
done < inputfile
make sure that the FIRST line of the file starts with CP START ACCOUNT - no blank lines, no nothing else. Otherwise your fn will be an empty string resulting in what you've quoted/observed above.
Or to accommodate and skip any lines BEFORE the first CP START ACCOUNT :