03-20-2013
Splitting records in a text file based on delimiter
A text file has 2 fields (Data, Filename) delimited by # as below,
Data,Filename
Row1 -> abc#Test1.xml
Row2 -> xyz#Test2.xml
Row3 -> ghi#Test3.xml
The content in first field has to be written into a file where filename should be considered from second field.
So from the above example
1. Data 'abc' should be written into a file named Test1.xml
2. Data 'xyz' should be written into a file named Test2.xml
3. Data 'ghi' should be written into a file named Test3.xml
Is it possible to acheive this using Shellscript, please help me with the logic.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I just wandering how to split a record which has more than one delimiter,
i have a file which contains pattern as group separtor and ~ as field separtor, Ultimately I need consider even the groups as a field, So i need to make this multi-delimited file into ~ delimited file.
My record... (4 Replies)
Discussion started by: braindrain
4 Replies
2. Shell Programming and Scripting
I need to split a file based on certain context inside the file. Is there a unix command that can do this? I have looked into split and csplit but it does not seem like those would work because I need to split this file based on certain text. The file has multiple records and I need to split this... (1 Reply)
Discussion started by: matrix1067
1 Replies
3. Shell Programming and Scripting
Hi Everybody,
I need some help on formatting the files coming into unix box on the fly.
I get a file some thing like this in a single line.
ISA^M00^M ^M00^M ^M14^M006929681900 ^M01^M095449419 ... (5 Replies)
Discussion started by: ntekupal
5 Replies
4. Shell Programming and Scripting
All,
We receive a file with a large no of records (records can vary) and we have to split it into two files based on another file. e.g.
File1:
UHDR 2008112
"25187","00000022","00",21-APR-1991,"" ,"D",-000000519,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ... (2 Replies)
Discussion started by: er_ashu
2 Replies
5. Shell Programming and Scripting
Hi,
I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies
6. Shell Programming and Scripting
Hello gurus,
I am new to "awk" and trying to break a large file having 4 million records into several output files each having half million but at the same time I want to keep the similar key records in the same output file, not to exist accross the files.
e.g. my data is like:
Row_Num,... (6 Replies)
Discussion started by: kam66
6 Replies
7. Shell Programming and Scripting
Hi,
I have a No Delimiter variable length text file with following schema -
Column Name Data length
Firstname 5
Lastname 5
age 3
phoneno1 10
phoneno2 10
phoneno3 10
sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies
8. UNIX for Dummies Questions & Answers
Hi Folks,
I am a novice and need to build a script in bash. I have 2 text files data.txt file is big file, column 2 is the we need to search and delete in the output. The filter file contains the rows to be deleted.
Data.txt
state city zone
Alabama Huntsville 4
California SanDiego 3... (3 Replies)
Discussion started by: tech_frk
3 Replies
9. UNIX for Dummies Questions & Answers
i have a snippet from server log delimited by forward slash.
/a/b/c/d/filename
i need to cut until last delimiter. So desired output should look like:
/a/b/c/d
can you please help?
Thanks in advance. (7 Replies)
Discussion started by: alpha_1
7 Replies
10. Shell Programming and Scripting
Hello I have a file of following format
HDR 1234 abc qwerty
abc def ghi jkl
HDR 4567 xyz qwerty
abc def ghi jkl
HDR 890 mno qwerty
abc def ghi jkl
HDR 1234 abc qwerty
abc def ghi jkl
HDR 1234 abc qwerty
abc def ghi jkl
-Need to split this into multiple files based on tag... (8 Replies)
Discussion started by: wincrazy
8 Replies
LEARN ABOUT DEBIAN
data::stag::basegenerator
Data::Stag::BaseGenerator(3pm) User Contributed Perl Documentation Data::Stag::BaseGenerator(3pm)
NAME
Data::Stag::BaseGenerator - base class for parsers and other event generators
SYNOPSIS
# writing the parser
package MyParser;
use base qw(Data::Stag::BaseGenerator);
sub parse_fh {
my ($self, $fh) = shift;
my $lnum = 0;
$self->start_event('data');
while (<$fh>) {
++$lnum;
$self->line_no($lnum);
# do stuff
$self->start_event('foo');
# ...
$self->event(blah=>5);
#
if (/incorrect_line/) {
$self->parse_err('line not in correct format');
}
# ...
$self->end_event('foo');
}
$self->pop_stack_to_depth(0);
}
1;
# using the parser
my $p = MyParser->new;
my $h = MyHandler->new; # see Data::Stag::BaseHandler
my $eh = Data::Stag->makehandler;
$p->handler($h);
$p->errhandler($eh);
$p->parse($file);
# result tree
print $h->stag->xml;
# write parse errs on standard err
printf *STDERR $p->errhandler->stag->xml;
# using the parser from the command line
unix> stag-parse.pl -p MyParser -w xml -e err.xml > out.xml
# using the parser from the command line via intermediate handler
unix> stag-handle.pl -p MyParser -m MyHandler -w xml -e err.xml > out.xml
DESCRIPTION
This is the base class for all parsers and event generators
parsers/generators take some input (usually a filehandle, but a generator could be a socket listener, for example) and fire stag events
stag events are
start_event NODENAME
evbody DATA
end_event NODENAME {optional}
event NODENAME DATA
These events can be nested/hierarchical
If uncaught, these events are stacked into a stag tree, which can be written as xml or one of the other stag formats
specialised handlers can be written to catch the events your parser throws
For example, you may wish to write a pod parser that generates nested events like this:
<pod>
<section>
<type>head1</type>
<name>NAME</name>
<text>Data::Stag - Structured Tags datastructures</text>
</section>
...
</pod>
(see the source for Data::Stag::PodParser for details)
You can write handlers that take the pod-xml and generate something - for example HTML
parsers may encounter unexpected things along the way - they may throw an exception, and fall over - or they may choose to fire an error
event. by default, error event streams are diverted to STDERR. You can create your own error handlers
PUBLIC METHODS
new
Title: new
Args:
Return: L<Data::Stag::BaseGenerator>
Example:
CONSTRUCTOR
handler
Title: handler
Function: GET/SET ACCESSOR METHOD
Args: handler L<Data::Stag::BaseHandler> optional
Return: L<Data::Stag::BaseHandler>
Example: $p->handler(MyHandler->new);
each parser has a handler - all events generated are passed onto the handler; the default handler simply sits there collecting events
errhandler
Title: errhandler
Function: GET/SET ACCESSOR METHOD
Args: handler L<Data::Stag::BaseHandler> optional
Return: L<Data::Stag::BaseHandler>
Example: $p->errhandler(Data::Stag->makehandler);
each parser has an error handler - if the parser encounters things it does not expect, it can pass errors to the errorhandler
if no errorhandler is set, an XML event handler that writes to STDERR is used
cache_errors
Title: cache_errors
Args:
Return:
Example: $p->cache_errors
If this is called, all errors will be cached rather than written to STDERR
The error list can be accessed like this
$p->parse($fn);
@errs = $p->errhandler->stag->get_error;
parse
Example - $parser->parse($file1, $file2);
Returns -
Args - filenames str-LIST
parses a file
parse
Example - $parser->parse_fh($fh)
Returns -
Args - fh FILEHANDLE
parses an open filehandle
PROTECTED METHODS
These methods are only of interest if you are making your own parser/generator class
start_event NODENAME
evbody DATA
end_event NODENAME {optional}
event NODENAME DATA
SEE ALSO
Data::Stag Data::Stag::BaseHandler
perl v5.10.0 2008-06-03 Data::Stag::BaseGenerator(3pm)