changing fields in ksh - UNIX

January 5, 2012 at 07:08:22
Specs: Windows XP

I have a fixed file and need to change the value in column 900-902 only if the value in those 3 fields are 999, I need to change to spaces. How do I do this in ksh?

Thank you

See More: changing fields in ksh - UNIX

Report •

January 5, 2012 at 09:56:35

In the past, to solve changing column problems like this, I would cut the line into chunks, make the changes, and glue the line back together. Here is an example:

But I think a perl solution is easier:


use warnings;
use strict;

my $src = "./infile.txt";
my $dest = "./outfile.txt";

open(IN, '<', $src) or die "Cannot open inputfile $src: $!\n";
open(OUT, '>', $dest) or die "Cannot open outputfile $dest: $!\n";
while (<IN>) {
# perl strings start counting from 0 not 1
if (substr($_, 899, 3) eq "999")
substr($_, 899, 3) = " ";

print OUT $_;


close IN;
close OUT;

Report •

January 5, 2012 at 10:52:34
Thanks Nail,

I tried :
awk '{ if (substr($0, 900, 3) == "999")
gsub( substr($0, 900, 3), " ");

but gsub changed every 999 in any column to spaces where I only needed column 900-902.

I'll try perl and let you know

Report •

January 5, 2012 at 11:10:56
Interesting. I am surprised that use of substr in awk didn't fail totally. The problem is where perl allows the use of substr on the left hand side of the equation, awk does not. I even checked the GNU version of awk on Linux, and it acts the same way.

Report •

Related Solutions

January 25, 2012 at 06:18:03
Hi Nails,

The perl script is working fine, but how do you change directory if you run the script on a Unix server. what I have is: cd /disk1/post/aab but perl think that the "/" is a division.

Many thanks

Report •

January 25, 2012 at 06:39:04

I answered my question, chdir "/disk1/aab"; worked.


Report •

Ask Question