The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search Replace with regular expressions dirdamalah Shell Programming and Scripting 1 06-21-2008 07:53 AM
Regular Expressions HELP - PERL maxmave Shell Programming and Scripting 3 06-05-2008 04:45 AM
Regular Expressions HELP - PERL maxmave UNIX for Dummies Questions & Answers 1 06-04-2008 01:15 PM
regular expressions ragha81 UNIX for Dummies Questions & Answers 2 03-05-2007 07:24 PM
Perl regular expressions... DrRo183 Shell Programming and Scripting 1 04-26-2006 10:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-30-2008
dynamox dynamox is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
perl regular expressions and field search

Hello guys/gals,

i am sorry as this is probably very simply but i am slowly learning perl and need to convert some old korn shell scripts.

I need to be able to search a file line by line but only match a string at particular location on that line, for example character 20-30. So my file could look something like this:

Code:
apple orange apple
         apple potato apple
         tomato flower orange
i want to be able to search this file for string "orange" but i only want to search for this string at particular position, say from character 7-13. I am used to in bash i can use |awk '{print $2)' to get my second field ..but how would i do it in perl using regular expressions ?

Thanks a bunch
  #2 (permalink)  
Old 08-30-2008
quine quine is offline
Registered User
  
 

Join Date: Mar 2008
Location: Bay Area California
Posts: 68
If you want to SPLIT a line into FIELDS as you say you do in bash, then look up perl's split() built in subroutine... On the other hand, if what you really want is to test the characters from positionX to positionY, then use perl's unpack() function. Once you split out your field by delimiter or position, then you can test it using regular expressions to see if it is the string you want...

I'm not going to do ALL the work for you
  #3 (permalink)  
Old 08-30-2008
okdev's Avatar
okdev okdev is offline
Registered User
  
 

Join Date: Sep 2007
Location: Dallas, Texas
Posts: 5
Here you go!!! njoY!!!

theeven

simply open file >
store each line of file in an array >
split each lines in fields >
print >


$file='./file.txt';
open(INFO,$file);
@lines=<INFO>;
close(INFO);

foreach $line (@lines)
{
@field=split(/ /,$line);
print "$field[1]\n";
}


theeven
  #4 (permalink)  
Old 08-31-2008
dynamox dynamox is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 2
Thanks guys, split looks pretty easy but i also research unpack. I might use it in the future as well, very flexible. Thanks again for your help.
  #5 (permalink)  
Old 09-02-2008
bwreed bwreed is offline
Registered User
  
 

Join Date: Mar 2002
Location: Saint Paul, MN
Posts: 6
Also look at the substr command, and determine which better suits your needs between unpack and substr.

Your first step is being sure what you need - search within certain columns, or searching by field.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0