×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

replacing values in a coloum thru the file.

replacing values in a coloum thru the file.

replacing values in a coloum thru the file.

(OP)
hi

i have a file which has data like this

month car dealer year
jan lexus xyz 2012
mar merc ber 2011
april toyota wer 2010


with perl i want to replace month with a corresponding number like
jan -1
feb -2
mar -3
..
..
dec -12

the number of lines in the file is not fixed so it has to go thru all the lines that exist in the file.

Looking for some advice as to how i can do this in perl .


RE: replacing values in a coloum thru the file.

Hi

I suppose that "april" is a typo and actually all month names are abbreviated to 3 characters :

CODE --> command-line

perl -pae 'substr$_,0,3,(index("janfebmaraprmayjunjulaugsepoctnovdec",$F[0])/3+1)*-1if$.>1' /input/file 
This modifies the file in-place. Back up your data before trying it.

Feherke.
http://feherke.github.com/

RE: replacing values in a coloum thru the file.

Hi

Oops. My mistake. Than not modifies the file in-place.

Feherke.
http://feherke.github.com/

RE: replacing values in a coloum thru the file.

(OP)
feherke could u please explain it a lil please ... thanks

RE: replacing values in a coloum thru the file.

Hi

CODE

perl \      # invoke the Perl interpreter
-p \        # implicit looping over the input with implicit printing
-a \        # autosplit the input lines
-e '        # execute code specified in command line
substr      # replace part of the string :
  $_,       #   the current input line
  0,        #   at the beginning
  3,        #   3 characters long
  (         #   with the result of this expression
    index(  #     find position of string :
      "janfebmaraprmayjunjulaugsepoctnovdec", # in this one
      $F[0] #       the first field resulted from autosplitting
    )/3     #     divide found position by one abbreviation's length
    +1      #     add 1 because string position start at 0
  )*-1      #   change sign to make it negative
if          # execute the previous statement only if the condition is true
  $.>1      #   current input line number is greater than 1
' /input/file # read input from this file 

Feherke.
http://feherke.github.com/

RE: replacing values in a coloum thru the file.

(OP)
thanks a lot feherke.

so this will write to the same input file rite ?

RE: replacing values in a coloum thru the file.

Hi

No, it will output to STDOUT, leaving the input file unchanged. Was only my intention to make it modify in-place, but misclicked when copying the code.

For in-place modification you only have to add the -i option :

CODE

perl -pai -e 'substr$_,0,3,(index("janfebmaraprmayjunjulaugsepoctnovdec",$F[0])/3+1)*-1if$.>1' /input/file 

Feherke.
http://feherke.github.com/

RE: replacing values in a coloum thru the file.

(OP)
Thanks feherke .. appreciate ur help .. will try it in eve .. at work now :P ..

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close