I have a file with 100 lines. On 50 th line , from position 5 to rest of the data , I need to change the occurrence of A to B and Occurrence of M to N.
Input file :
Line1
Line2
Line3
--
--
12345ABCDEFM
---
--
Line 100
Output
Line1
Line2
Line3
--
--
12345BBCDEFN
---
--
Line 100
A all of the others have produced sed and awk versions...
This is a longhand manual version using builtins only on OSX 10.7.5, default bash terminal.
There is always a way.
In this snippet your string is placed roughly halfway...
Last login: Wed Jan 15 22:06:27 on ttys000
AMIGA:barrywalker~> # Create an array as a tester.
AMIGA:barrywalker~> text=($(printf "12345lkjasldkja\n12345lclhflkjaflkaf\n12345ABCDEFM\n12345sdlklkdlfksd\n12345PWOEPOWER"))
AMIGA:barrywalker~> # Do two sustitutions on the second subscript.
AMIGA:barrywalker~> text[2]="${text[2]/A/B}"
AMIGA:barrywalker~> text[2]="${text[2]/M/N}"
AMIGA:barrywalker~> # Create and build a file.
AMIGA:barrywalker~> > /tmp/text
AMIGA:barrywalker~> echo "${text[0]}" >> /tmp/text
AMIGA:barrywalker~> echo "${text[1]}" >> /tmp/text
AMIGA:barrywalker~> echo "${text[2]}" >> /tmp/text
AMIGA:barrywalker~> echo "${text[3]}" >> /tmp/text
AMIGA:barrywalker~> echo "${text[4]}" >> /tmp/text
AMIGA:barrywalker~> # Now prove the file is correct.
AMIGA:barrywalker~> cat < /tmp/text
12345lkjasldkja
12345lclhflkjaflkaf
12345BBCDEFN
12345sdlklkdlfksd
12345PWOEPOWER
AMIGA:barrywalker~> _
The original request was to only make changes to line 50 in the input file.
The original request was to change all occurrences of A to B and M to N in that line except that no changes are to be made to the 1st five characters on that line.
My expectation is that after 1* , from L to 11111 everything should be changed as per AWK above but but only few letters got translated. any help on this
Maybe you didn't realize that if you change all occurrences of A to Z and then change all occurrences of Z to A, everything that was an A or a Z in the original input will be an A in the output. The sequences of gsub() calls that you have translate Z through N to A through M, z through n to a through m, and 9 through 5 to 0 through 4, respectively. The letters A through M, a through m, and the digits 0 through 4 are effectively unchanged (by changing each of them twice).
not quite sure what 'you want or any other suggestion' - Don's explanation seems to be clear to me explaining what you're getting.
If for example, you want to change A to Z, but if there was a 'change' you don't what to change the 'changed' Z back to A, then
if (!gsub ( "A", "Z", j))
gsub("Z","A",j)
You probably could optimize the whole thing with multiple gsub-s, with the lookup arrays and a single gsub iterating over the arrays......
Many versions of awk limit the length of a line to LINE_MAX bytes. The value of LINE_MAX on your system can be found using getconf LINE_MAX ). But it looks like there is an off by one error that is causing your problem here. Try changing: