Computing.Net > Forums > Unix > Awk to parse and output

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Awk to parse and output

Reply to Message Icon

Name: DeeDogg
Date: September 12, 2005 at 09:55:42 Pacific
OS: Sun OS
CPU/Ram: sparc9 2Gigs Rams
Comment:

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.
-Jean

Example:
item#1
aaa
bbb
ccc
;
item#2
aaa
bbb
ccc
;
itme#3
aaa
bbb
ccc
;
....




Sponsored Link
Ads by Google

Response Number 1
Name: Luke Chi
Date: September 12, 2005 at 10:55:07 Pacific
Reply:

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;third

output:

item1.txt:
1
a
first

item2.txt:
2
b
second

item3.txt:
3
c
third


Luke Chi


0

Response Number 2
Name: DeeDogg
Date: September 12, 2005 at 11:36:14 Pacific
Reply:

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


0

Response Number 3
Name: Jim Boothe
Date: September 12, 2005 at 12:15:15 Pacific
Reply:

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


0

Response Number 4
Name: Luke Chi
Date: September 12, 2005 at 12:41:12 Pacific
Reply:

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


0

Response Number 5
Name: Luke Chi
Date: September 12, 2005 at 13:12:21 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Jim Boothe
Date: September 13, 2005 at 07:00:21 Pacific
Reply:

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.


0

Response Number 7
Name: DeeDogg
Date: September 13, 2005 at 07:06:02 Pacific
Reply:

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



0

Response Number 8
Name: Luke Chi
Date: September 13, 2005 at 10:35:47 Pacific
Reply:

Good !

Luke Chi


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Awk to parse and output

Parsing and emailing a text file www.computing.net/answers/unix/parsing-and-emailing-a-text-file-/6623.html

how to parse a path in unix www.computing.net/answers/unix/how-to-parse-a-path-in-unix/3301.html

sed/awk - line feeds? www.computing.net/answers/unix/sedawk-line-feeds/5237.html