LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-19-2010, 05:32 AM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
splitting a fil.e word by word. PERL


[U]I have been using a one liner for a while and just discovered something bad.

doing this results in :
cat ~/tmp/casper/stockfile | perl -nle 'print /(\w+)\sUS/'
ACUZ
ACUZ

however the ~/tmp/casper/stockfile looks like this:
ACUS US stk - (otcbb)
Warning ACUS US symbol has changed to ACUSD US

The one liner seems to stop at the line once it finds what it needs - I need to see the ACUSD.

I wrote a script that splits the file word by word instaed of reading it linne by line, but it is not working

#!/usr/bin/perl -w
use strict;
my $word;
while (<>)
{ for $word (split)
{ if ($word =~ /(\w+)\sUS/){
print $word "\n";
}
}
}

however when i run the script

cat ~/tmp/casper/stockfile | /home/casper/myscript.pl
nothing

cat ~/tmp/casper/stockfile < /home/casper/myscript.pl

again nothing.
 
Old 11-20-2010, 07:36 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,117
Blog Entries: 2

Rep: Reputation: 113Reputation: 113
you could try adding a g in your one-liner

perl -nle 'print /(\w+)\sUS/g'

but the output you want is probably more like

cat /tmp/stock | perl -nle '$, = $/; print @s if (@s = /(\w+)\sUS/g)'

or

cat /tmp/stock | perl -nle '$, = " "; print @s if (@s = /(\w+)\sUS/g)'
 
Old 11-22-2010, 05:17 AM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
The G works on the one liner - but I when i add it to the saved script, it still does not work - so there must be something wrong with the code, not the regex. Splitting the file word by word works - but for some reason i cannot make the seach work. I kinda want to get away from just using one-liners at the end of files, they are too limiting.

Last edited by casperdaghost; 11-22-2010 at 05:18 AM.
 
Old 11-22-2010, 06:11 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Maybe you need to look at your logic?
Code:
for $word (split)
This splits each line based on a space as a delimeter. Therefore the next statement:
Code:
if ($word =~ /(\w+)\sUS/)
Says look in $word to see if it is a word followed by a space followed by US, but as $word is only ever a single word then it will never have a space
and hence the if is working as requested.
 
Old 11-22-2010, 06:23 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Sorry, forgot to add, maybe you are looking for something more simple like:
Code:
#!/usr/bin/perl -w
use strict;

open FH,"$ARGV[0]" or die "unable to open file";

while (<FH>)
{
    if ($_ =~ /(\w+)\sUS/){
        print $_; 
    } 
}
 
Old 11-23-2010, 04:09 AM   #6
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
grail your script works. Thank you for deconstructing the logic - that is right, i split the file by spaces, which means that ACUD US actually equals :
ACUD
US
hence the regex can't work.

I modeled the original script from here http://www.devdaily.com/blog/post/pe...text-file-perl

1 #!/usr/bin/perl
2 while (<>)
3 {
4 # split each input line; words are separated by whitespace
5 for $word (split)
6 {
7
8 print $word . "\n";
9 }
10 }


In this example above, i have no idea what the '.' does in line 8
 
Old 11-23-2010, 05:45 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
dot is used for concatenation
 
  


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
[SOLVED] printf white space issue word splitting dukedog Linux - Newbie 2 06-15-2010 01:46 PM
How can i read two files word by word at a time using any loop by shell script? vaibhavs17 Programming 16 03-19-2010 03:48 AM
[SOLVED] Bash: why is word splitting not effective in an assignment catkin Programming 5 12-12-2009 12:02 PM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
Problems Copying & Pasting In Word When Word Closes - Ubuntu davidx Linux - Software 3 10-22-2008 08:21 PM


All times are GMT -5. The time now is 08:25 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration