LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2005, 07:04 PM   #1
0aniel
LQ Newbie
 
Registered: Nov 2005
Posts: 19

Rep: Reputation: 0
Text lines?


Hi there,

Just have a question regarding splitting text lines.
How do I get the following fixed strings;


The strings are fixed. I need it to
1.) Look for the first 8 digits and make split.(Global)

2.)Then look for the first 4 Letters and make split
What commands would I need to execute this in my Shell Script.

Thx a lot

Last edited by 0aniel; 11-25-2005 at 08:55 AM.
 
Old 11-21-2005, 07:44 PM   #2
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 31
I would use perl.

Assuming that will always be the format, e.g.: 9600012301FXTF02FTXT03FFTF04TFXX05TXFF06TTTT
Also assuming these are being read from a file:

Code:
#!/usr/bin/perl -w

my $file = $ARGV[0];

open(FILE, "$file") || die "Could not open file: $!";

for(<FILE>){

    my $cwl = $_;
    $cwl =~ m/(\d{8})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})//g;

# Once this match is made, you can do something with $1, $2.....$6...which are all of your matches.
# Print them to a file, stdout, whatever....

}

Last edited by PenguinPwrdBox; 11-21-2005 at 07:48 PM.
 
Old 11-21-2005, 08:00 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
If you know the number & length of the fields (ie fixed format), Perl is fine, but I'd use unpack() instead of a regex. It's easier on the eye.
 
Old 11-21-2005, 08:11 PM   #4
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 31
Was unfamiliar with unpack....
That rocks
 
Old 11-21-2005, 10:02 PM   #5
shukla_chanchal
Member
 
Registered: Oct 2005
Location: banglore(india)
Posts: 62

Rep: Reputation: 15
why not cut

why cut will not work here ?
i think it is easier to do it with cut and script
although perl is best because its a strong programming langualge
 
Old 11-21-2005, 11:31 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
Well, for a start you'd need a separate cmd line for each field with cut (I think).
Perl + unpack() will do it in 1 line.
Really though, use whichever you feel comfortable with.
The more complex your processing, the more I'd recommend using Perl.
 
Old 11-22-2005, 05:38 AM   #7
0aniel
LQ Newbie
 
Registered: Nov 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Hi there,

Its a bash script so
i need to use #unix commands.
 
Old 11-22-2005, 12:00 PM   #8
PenguinPwrdBox
Member
 
Registered: Oct 2003
Posts: 568

Rep: Reputation: 31
rewrite it in perl
 
Old 11-22-2005, 03:54 PM   #9
0aniel
LQ Newbie
 
Registered: Nov 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Cheers

Last edited by 0aniel; 11-25-2005 at 08:53 AM.
 
Old 11-30-2005, 04:08 AM   #10
evoGage
LQ Newbie
 
Registered: Nov 2005
Posts: 15

Rep: Reputation: 0
Quote:
Originally posted by PenguinPwrdBox
I would use perl.

Assuming that will always be the format, e.g.: 9600012301FXTF02FTXT03FFTF04TFXX05TXFF06TTTT
Also assuming these are being read from a file:

Code:
#!/usr/bin/perl -w

my $file = $ARGV[0];

open(FILE, "$file") || die "Could not open file: $!";

for(<FILE>){

    my $cwl = $_;
    $cwl =~ m/(\d{8})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})(\d{2}\w{4})//g;

# Once this match is made, you can do something with $1, $2.....$6...which are all of your matches.
# Print them to a file, stdout, whatever....

}
How would you do that with sed commands instead for that example?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 11:55 AM
awk text that is on several lines homey Programming 2 10-31-2004 10:27 AM
How to filter lines from a text file Rascale Linux - Software 1 10-28-2003 01:46 PM
VERY annoying text borders instead of lines... Glasnost Linux - Newbie 2 10-13-2003 02:52 PM
Loop over lines in text file? amaze Linux - General 2 08-12-2003 08:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration