amparida has asked for the wisdom of the Perl Monks concerning the following question:
Output:[8/29/2013 7:16:45 AM] User abc def [DEFAULT] [8/29/2013 7:16:45 AM] User xyz abc [DEFAULT]
I know how to replace. But need help to do this replace operation for the string present in square bracket. Regards, amiya here my code[8/29/2013<tab>7:16:45<tab>AM] User abc def [DEFAULT] [8/29/2013<tab>7:16:45<tab>AM] User xyz abc [DEFAULT]
#!/usr/bin/perl -w use strict; open(FILE, "<input.txt") || die "File not found"; my @lines = <FILE>; close(FILE); my @newlines; foreach(@lines) { $_ =~ s/\s+/\t/g; push(@newlines,"$_\n"); } my $file = "output.txt"; unlink $file; open(FILE, '>>',$file) || die "FILE not found"; print FILE @newlines; close(FILE); print "Done \n"; input.txt: [8/29/2013 7:16:45 AM] User abc def [DEFAULT] [8/29/2013 7:16:45 AM] User xyz abc [DEFAULT] output.txt: with my code [8/29/2013<tab>7:16:45<tab>AM]<tab>User<tab>abc<tab>def<tab>[DEFAULT] [8/29/2013<tab>7:16:45<tab>AM]<tab>User<tab>xyz<tab>abc<tab>[DEFAULT] Output.txt:(expected) [8/29/2013<tab>7:16:45<tab>AM] User abc def [DEFAULT] [8/29/2013<tab>7:16:45<tab>AM] User xyz abc [DEFAULT]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Replace space with tab between square bracket
by kcott (Archbishop) on Aug 31, 2013 at 12:57 UTC | |
by amparida (Initiate) on Aug 31, 2013 at 13:09 UTC | |
|
Re: Replace space with tab between square bracket
by toolic (Bishop) on Aug 31, 2013 at 12:15 UTC | |
| |
|
Re: Replace space with tab between square bracket
by hdb (Monsignor) on Aug 31, 2013 at 12:52 UTC |