Replace text in column in multiple lines unix

September 22, 2011 at 20:05:18
Specs: Windows 7
I want to change a year in all lines in multiple files with validation:
let say the validation located in column 18 and column 50 both with 4 digits (this is a year)
i want to validate if it is 1910 or 1911 and I want to change it in 2010 and 2011
how to do it using script in unix.

Thanks


See More: Replace text in column in multiple lines unix

Report •

#1
September 22, 2011 at 22:02:49
This link is a similar explains a similar situation:

http://www.computing.net/answers/un...

I originally presented a script that cut the string into pieces. The next solution was an eleant perl soution. I might give it a shot over the weekend if you want perl solution.

otherwise, you can modify my original cut example.


Report •

#2
September 22, 2011 at 22:32:37
nails,

yes, please if you have time? thanks

I have so many files with so many lines and every lines in column 17 and column 50 I need to replace the value of 1910 to 2010 and 1911 to 2011 only in column 17 and 50.

I thank you for your kind and great effort.


Report •

#3
September 25, 2011 at 22:21:44
If your perl script is called myperl.ss, call it with 2 command line arguments - the first is the source file (input) and the second is the destination (output) file:

myperl.ss <inputfilename> <outputfilename)

Let me know if you have any questions.

#!/usr/bin/perl

use warnings;
use strict;

my $src = shift @ARGV;
my $dest = shift @ARGV;

open(IN, '<', $src) or die "Cannot open inputfile $src: $!\n";
open(OUT, '>', $dest) or die "Cannot open outputfile $dest: $!\n";
while (<IN>) {
   # check the substring at position 17, perl strings start counting from 0 not
1
   my $s1 = substr($_, 16, 4);
   if ($s1 eq "1910")
      {
      substr($_, 16, 4) = "2010";
      }

   $s1 = substr($_, 49, 4);
   # check the substring at position 50, perl strings start counting from 0 not
1
   if ($s1 eq "1911")
      {
      substr($_, 49, 4) = "2011";
      }

   print OUT $_;

}

close IN;
close OUT;


Report •

Related Solutions

#4
September 25, 2011 at 23:54:40
thanks a lot you really did a great job...it works

Report •

Ask Question