Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replacing values in a coloum thru the file.

Status
Not open for further replies.

technoman007

IS-IT--Management
Jul 17, 2012
9
0
0
US
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 .


 
Hi

I suppose that "april" is a typo and actually all month names are abbreviated to 3 characters :
Code:
perl -pae '[b]substr[/b][navy]$_[/navy][teal],[/teal][purple]0[/purple][teal],[/teal][purple]3[/purple][teal],([/teal][b]index[/b][teal]([/teal][green][i]"janfebmaraprmayjunjulaugsepoctnovdec"[/i][/green][teal],[/teal][navy]$F[/navy][teal][[/teal][purple]0[/purple][teal]])/[/teal][purple]3[/purple][teal]+[/teal][purple]1[/purple][teal])*-[/teal]1if$[teal].>[/teal][purple]1[/purple]' /input/file
This modifies the file in-place. Back up your data before trying it.


Feherke.
[link feherke.github.com/][/url]
 
feherke could u please explain it a lil please ... thanks
 
Hi

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

Feherke.
[link feherke.github.com/][/url]
 
thanks a lot feherke.

so this will write to the same input file rite ?
 
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 [tt]-i[/tt] option :
Code:
perl -pa[highlight]i -[/highlight]e '[b]substr[/b][navy]$_[/navy][teal],[/teal][purple]0[/purple][teal],[/teal][purple]3[/purple][teal],([/teal][b]index[/b][teal]([/teal][green][i]"janfebmaraprmayjunjulaugsepoctnovdec"[/i][/green][teal],[/teal][navy]$F[/navy][teal][[/teal][purple]0[/purple][teal]])/[/teal][purple]3[/purple][teal]+[/teal][purple]1[/purple][teal])*-[/teal]1if$[teal].>[/teal][purple]1[/purple]' /input/file

Feherke.
[link feherke.github.com/][/url]
 
Thanks feherke .. appreciate ur help .. will try it in eve .. at work now :p ..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top