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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-21-2005, 07:04 PM
|
#1
|
LQ Newbie
Registered: Nov 2005
Posts: 19
Rep:
|
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.
|
|
|
11-21-2005, 07:44 PM
|
#2
|
Member
Registered: Oct 2003
Posts: 568
Rep:
|
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.
|
|
|
11-21-2005, 08:00 PM
|
#3
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
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.
|
|
|
11-21-2005, 08:11 PM
|
#4
|
Member
Registered: Oct 2003
Posts: 568
Rep:
|
Was unfamiliar with unpack....
That rocks
|
|
|
11-21-2005, 10:02 PM
|
#5
|
Member
Registered: Oct 2005
Location: banglore(india)
Posts: 62
Rep:
|
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
|
|
|
11-21-2005, 11:31 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415
|
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.
|
|
|
11-22-2005, 05:38 AM
|
#7
|
LQ Newbie
Registered: Nov 2005
Posts: 19
Original Poster
Rep:
|
Hi there,
Its a bash script so
i need to use #unix commands.
|
|
|
11-22-2005, 12:00 PM
|
#8
|
Member
Registered: Oct 2003
Posts: 568
Rep:
|
rewrite it in perl
|
|
|
11-22-2005, 03:54 PM
|
#9
|
LQ Newbie
Registered: Nov 2005
Posts: 19
Original Poster
Rep:
|
Cheers
Last edited by 0aniel; 11-25-2005 at 08:53 AM.
|
|
|
11-30-2005, 04:08 AM
|
#10
|
LQ Newbie
Registered: Nov 2005
Posts: 15
Rep:
|
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?
|
|
|
All times are GMT -5. The time now is 05:12 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|