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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
Due to network maintenance being performed by our provider, LQ will be down starting at 05:01 AM UTC. The exact duration of the downtime isn't currently known. We apologize for the inconvenience.
|
 |
11-21-2005, 06: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 07:55 AM.
|
|
|
|
11-21-2005, 06:44 PM
|
#2
|
|
Member
Registered: Oct 2003
Location: /illinois/chicago
Distribution: Slackware/Gentoo/FC/RHEL
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 06:48 PM.
|
|
|
|
11-21-2005, 07:00 PM
|
#3
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,988
|
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, 07:11 PM
|
#4
|
|
Member
Registered: Oct 2003
Location: /illinois/chicago
Distribution: Slackware/Gentoo/FC/RHEL
Posts: 568
Rep:
|
Was unfamiliar with unpack....
That rocks 
|
|
|
|
11-21-2005, 09: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, 10:31 PM
|
#6
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 14,988
|
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, 04: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, 11:00 AM
|
#8
|
|
Member
Registered: Oct 2003
Location: /illinois/chicago
Distribution: Slackware/Gentoo/FC/RHEL
Posts: 568
Rep:
|
rewrite it in perl
|
|
|
|
11-22-2005, 02:54 PM
|
#9
|
|
LQ Newbie
Registered: Nov 2005
Posts: 19
Original Poster
Rep:
|
Cheers
Last edited by 0aniel; 11-25-2005 at 07:53 AM.
|
|
|
|
11-30-2005, 03: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?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:40 PM.
|
|
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
|
|