The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
shift not working Nagapandi UNIX for Dummies Questions & Answers 4 06-11-2008 04:28 AM
shift and push question in perl hankooknara Shell Programming and Scripting 5 06-29-2007 06:37 AM
Regarding the shift command??? shrao Shell Programming and Scripting 2 03-31-2007 03:39 AM
shift command Nisha Shell Programming and Scripting 6 07-19-2002 05:54 AM
shift command AkumaTay UNIX for Dummies Questions & Answers 1 05-20-2002 08:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-06-2009
ifeatu ifeatu is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
Shift Question (Perl)

I am attempting to write a script that reads each line of a file into a separate array and does some work on it then puts it all back together and I think I need to use the 'shift()' command to read each line into its own array, but I need help nesting it into a while loop (while not eof)

So here is some of the raw data in the file:

Code:
Vallejo-1991-Jan-20-The_Bride-BLH
Vallejo-1991-Jan-20-The_Bride-BLH
Berkeley-1992-Jan-26-I_Corinth_14-BLH
berkeley-1992-Jan-26-I_Corinth_14-BLH
Union City-1991-July-14-Promises_covenent_of_circumcision-BLH
UC-1991-July-14-Promises_covenent_of_circumcision-BLH
I want the output to replace the Location (Vallejo, etc.) with a consistent syntax (Capital first letter and city spelled out; e.g. Union City, Vallejo, Berkeley, there are only these three locations) AND, more importantly I want to change the Month element into a number (e.g July - 07, Jan -01, unfortunately the abbreviations aren't always consistent, BUT the first three letters usually are (Jul, Jan, Aug, etc.)

So the output of the data above should be:

Vallejo-1991-01-20-The_Bride-BLH
Vallejo-1991-01-20-The_Bride-BLH
Berkeley-1992-01-26-I_Corinth_14-BLH
Berkeley-1992-01-26-I_Corinth_14-BLH
Union City-1991-07-14-Promises_covenent_of_circumcision-BLH
Union City-1991-07-14-Promises_covenent_of_circumcision-BLH

here is what I have so far:
Code:
# Open File containing raw data
open(FILE, "test2.txt") or die("unable to open file");

# read file into an array
@RawData = <FILE>;
close(FILE);
while (<>) {

#read in Folder name
@FileNames = shift(@RawData);
I know how to write the code to split the data into arrays BUT I dont know how to analyze and replace the data...
  #2 (permalink)  
Old 01-07-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
You are somehow trying to read the same file twice. This line reads the ENTIRE file into the @RawData array:
Code:
@RawData = <FILE>;
Then you try to start reading from files listed on the command line (or standard input) with
Code:
while (<>) {
1. Maybe you mean:
Code:
open(FILE, "test2.txt") or die("...");
while (<FILE>) { 
   # process one line in $_ at a time...
}
I think that's what you want.

2. To process: use split()
Code:
# inside while loop
($city,$year,$month,$day,$title,$speaker)=split('-'); 

# Convert month name to a number, or print existing value if not found in mapping.
$month=exists $month2int{$month} ? $month2int{$month} : $month;

# Canonicalize city name: use the value found in the map; if not in the map, just capitalize first letter. 
$city=exists $citymap{$city} ? $citymap{$city} : ucfirst($city);

# reconstruct and print out line.
print join("-",$city,$year,$month,$day,$title,$speaker);
3. Define your "mappings" for month names and cities. Do this before the while loop. Fill in the ellipses with the rest of the information you'll need....
Code:
%citymap = ( UC => "Union City", VJ => "Vallejo", ... );
%month2int  = ( Jan => 1, Feb => 2, ...., Jul => 7, July => 7, ... Okt => 10, Oct => 10, October => 10, ... );
  #3 (permalink)  
Old 01-07-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
Or maybe you mean:
Code:
@RawData = <FILE>
while ($_ = shift @RawData) {
...
}
## @RawData is empty
But in that case, it's simpler, better, and faster to use:
Code:
@RawData = <FILE>
foreach (@RawData) {
 ...
}
## @RawData contains processed data.
  #4 (permalink)  
Old 01-07-2009
ifeatu ifeatu is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
help

Okay I'm getting a syntax error here is my code

Code:
open(FILE, "test2.txt") or die ("The file is not found");

$citymap = ( UC => "Union City", VJ => "Vallejo", Vallejo => "Vallejo", Union Ci
ty => "Union City", berk => "Berkeley", Berk => "Berkeley" );
$month2int  = ( Jan => 1, Feb => 2, Mar => 3, mar => 3, March => 3, Apr => 4, ap
r => 4, April => 4, may => 5, May => 5, Jun => 6, Jul => 7, July => 7, jul => 7,
 aug => 8, Aug => 8, August => 8, august => 8, Sept => 9, September => 9, sept =
> 9,Okt => 10, Oct => 10, October => 10, oct => 10, nov => 11, Nov => 11, Novemb
er => 11, november => 11, Dec => 12, December => 12, december => 12);

while (<FILE>) {
($city,$year,$month,$day,$title,$speaker)=split('-');
$month=exists $month2int{$month} ? $month2int{$month} : $month;
$city=exists $citymap{$city} ? $citymap{$city} : ucfirst($city);
print join("-",$city,$year,$month,$day,$title,$speaker);
}
I run the command:
Code:
perl ConvertingFileNames
and my error is:

Code:
syntax error at ConvertingFileNames line 3, near "Union City"
Execution of ConvertingFileNames aborted due to compilation errors.
I noticed in the code you gave me you had a prefix of "%" instead of "$" for the variables defining the month and Location...I tried running with both and it gives me the same error

So I like to talk my code out in words correct me if I'm wrong here but this is what I have so far:

1. Open the file with the file names
2. Define variables to compare the file to
3. While not eof take each line of the file and split it by "-" and put each split value into a unique variable
4. Check two of the variables (month and location) against the two variables defined before the "while" statement
5. make the appropriate changes or do nothing if nothing matches
6. print each line of the file back in the same order it was found with the appropriate changes
7. Close file

Can you help me with syntax?
  #5 (permalink)  
Old 01-08-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,884
The problem is where you have:
Code:
Union City => "Union City",
The space is throwing off perl. But you DONT NEED to map every city name... if it's not in the hash array, the script will simply capitalize the first letter and take the rest. So:
Code:
berkeley => 'Berkeley', Vallejo => 'Vallejo'
are also not needed. However this IS needed (just in case):
Code:
"union city" => "Union City"
Otherwise you will get "Union city".
  #6 (permalink)  
Old 01-08-2009
ifeatu ifeatu is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
Okay I got the script working...Thank you VERY much for your help...one more thing though
it minor dont worry...

I want the output to be as follows

YEAR-MN-DY-YR- Subject -BLH
so there should be a space before and after the subject
also I need the Month to be two digits
I tried altering the code by putting Feb => 02 instead of Feb => 2 but it errors out
I also tried changing the print line to ...,0$month,...or ...,'0' month,... that errors out as well

can you give me some guidance on syntax

#!/usr/bin/perl

open(FILE, "test3.txt") or die ("The file is not found");

%citymap = ( UC => "Union City", VJ => "Vallejo", Vallejo => "Vallejo",berk => "
Berkeley", Berk => "Berkeley" );
%month2int = ( Jan => 1, Feb => 2, Mar => 3, mar => 3, March => 3, Apr => 4, ap
r => 4, April => 4, may => 5, May => 5, Jun => 6, Jul => 7, July => 7, jul => 7,
aug => 8, Aug => 8, August => 8, august => 8, Sept => 9, September => 9, sept =
> 9,Okt => 10, Oct => 10, October => 10, oct => 10, nov => 11, Nov => 11, Novemb
er => 11, november => 11, Dec => 12, December => 12, december => 12);

while (<FILE>) {
($city,$year,$month,$day,$title,$speaker)=split('-');
$month=exists $month2int{$month} ? $month2int{$month} : $month;
$city=exists $citymap{$city} ? $citymap{$city} : ucfirst($city);
print join("- ",$city,$year,$month,$day,$title,$speaker);
  #7 (permalink)  
Old 01-08-2009
ifeatu ifeatu is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 5
nvrmind...I just discovered "printf" :-) :-)
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl shift, scripting, shift, shift perl

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 08:50 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