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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global replace nth character of nth line of all files 2

Status
Not open for further replies.

tbarthen

Technical User
Jul 26, 2003
33
US
I have a directory of files - some of them with the naming convention *.input

What I would like to do is run the following command-line global replace on all of these *.input files:

[blue]For files *.input, replace the 15th to the last character of lines 2 and 3 with 'mystring'[/blue]

If its any easier, this would be acceptable to:

[blue]For files *.input, replace the 15th to the last character of line x with 'mystring'[/blue]

With that second one, I could just run it twice - once for line 2, and once for line 3.

I'm thinking that the perl substr function could be used, but I can't figure out how.

[red]FYI: I'm working in a UNIX KORN SHELL[/red]
 

First, make backup copies of all files that could be affected!

Code:
# If 1st line of 1st file has just been read,
# remember filename.
1==NR { file = FILENAME }

# If this is 2nd or 3rd line of a file...
2==FNR || 3==FNR {
  $0 = substr($0,1,14) "mystring"
}

# Have we just started reading another file?
1==FNR && NR!=FNR {
  write(file, array)
  delete array
  file = FILENAME; i=0
}

# Add line just read to array.
{ array[++i] = $0 }

# All files have been read; write last one.
END { write(file, array) }


function write( fname, a     ,i )
{ for (i=1; i in a; i++)
    print a[i] >fname
  close( fname )
}
Save as "allFileReplace.awk" and run with
[tt]
awk -f allFileReplace.awk *.input
[/tt]
 
Looks like there may be some minor syntax errors:

[red]awk: syntax error near line 12
awk: illegal statement near line 12
awk: syntax error near line 21
awk: illegal statement near line 21
awk: bailing out near line 24[/red]

I'd fix it myself, but I'm just a beginner with PERL.

It looks like the error has something to do with this:
write(file, array)

I googled for examples of that syntax and found 2:
[blue]write(file, array, 4*count);
xfercount = write(file, array, rows * cols);[/blue]

Maybe the errors are obvious to you once you take a closer look. I don't know PERL well enough myself to fix it.

Thanks for what you've provided so far. After doing a little research, I can see that "awk" is the way to go. Looks like it just needs a little tweaking.
 
That's just classic :)

Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
tbarthen, I assure you there are no syntax errors when used with a modern awk (tested with Kernighan's awk95, mawk, and gawk).

Some versions of Unix (Sun's, I think) have a very old and primitive awk that doesn't understand function definitions such as "function write()". If you have nawk (new awk), always use it instead.
 
I see. Thanks for the tip.

I'll try to get a different version of awk.

Thanks again for the script.

[thumbsup2]
 
F*&in' mighty ... to bed
--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
cntr,

welcome to the group, this is perl, and it's got strengths and weaknesses, and you're obviously from an awk, gawk, nawk background.

Anyway, welcome
--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Thanks, PaulTEG. Awk is a midget compared to Perl, but its nimbleness can sometimes let it score. And there's a lot more activity here than in the awk group, so I can't resist looking for opportunities.
 
Here's a one-liner that will do what you want. It will edit the files "in place."
Code:
perl -pi.bak -e 'if($.==2||$.==3){substr($_,14)="mystring\n"}' *.input
In this example, your files would be backed up as filename.bak. You can change the .bak to any extension you want. If you omit the extension, no backups will be made. (Dangerous.)

Since you're using the Korn shell, the shell should handle globbing *.input for you.



 
mikevh, That is exactly what I was looking for!

Thanks! [cheers] [rockband]
 
You're welcome. Normally I might have gone with something a bit less awkward ... :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top