Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have a list of items separated by the semicolon. Is it possible to output each item to a separate file using awk(or nawk).
Thanks in advance.
-JeanExample:
item#1
aaa
bbb
ccc
;
item#2
aaa
bbb
ccc
;
itme#3
aaa
bbb
ccc
;
....

program:
awk -F";" ' { print $1 > "item1.txt"; print $2 > "item2.txt"; print $3 > "item3.txt"; } ' input.txt
input.txt:
1;2;3
a;b;c
first;second;thirdoutput:
item1.txt:
1
a
firstitem2.txt:
2
b
seconditem3.txt:
3
c
third
Luke Chi

Hi Luke,
Thanks for the reply.
I was looking for:
item1.txt:
aaa
bbb
ccc
;then item2.txt:
aaa
bbb
ccc
;
etc...
I don't want to separate the words on the lines, but rather the paragraphs, which are separated by the semicolon.Thanks,
Jean

The csplit command can come in very handy at times. The following command will split infile into separate files named outfile00, outfile01, etc, with lines consisting of semicolon being the separation line. The {*} following the pattern says to use that pattern repeatedly.
The problem with this solution is that the matched separator line becomes the first line of each new output file. I think you want to throw that line away, and I do not find a csplit option to do so.
csplit -f outfile infile '/^;$/' '{*}'
So here is an awk solution, and I coded it two different ways, but the output will be identical.
awk '\
BEGIN {fileout="outfile.001"
seq=1}
{if ($0==";")
{if (seq!=0)
close fileout
seq++
fileout=sprintf "outfile.%3.3d",seq
next}
print > fileout
}' infile
awk '\
BEGIN {seq=01
fileout="fileout.001"}
/;/ {close fileout
seq++
fileout=sprintf "fileout.%3.3d",seq
open fileout
next}
{print > fileout}' infile

On Redhat linux:
$ csplit -f item input.txt /\;/+1 {*}
input.txt:
1
2
3
;
a
b
c
;
first
second
third
;output:
item00:
1
2
3
;item01:
a
b
c
;item02:
first
second
third
;Note: My Solaris has problem to deal with {*}.
Luke Chi

The following is for Solaris:
CT=`grep ^\;$ input.txt | wc -l`
csplit -f item input.txt /\;/+1 {`expr $CT - 2`}My HP is down and I can't test it on HP machine at this moment.
Luke Chi

But still, the csplit +1 operand does nothing to get rid of the delimiter line.
The +1 says instead of using the line containing the pattern as the delimiting line, to use the line following. So instead of each output file beginning with the delimiter line, each output file will end with a delimiter line.

Hi,
Thanks for the help I went with the csplit command:
csplit -k -n{3} -fpin input.file '/^-/' '/;/+1' '{99}'I told it where the file started '-' and where the file ended ';'. The limitation is the problem, which is 99, and I have about 578 list of items. Before I would get an out of range error, so I added the -n option.
Thanks for the help Luke, Jim,
Jean

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |