Need help on find and replacement on specific line and position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on find and replacement on specific line and position
# 8  
Old 01-15-2014
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...
Code:
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~> _

This User Gave Thanks to wisecracker For This Post:
# 9  
Old 01-15-2014
Quote:
Originally Posted by wisecracker
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...
Code:
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~> _

This works fine except for two points:
  1. The original request was to only make changes to line 50 in the input file.
  2. 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.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 10  
Old 01-16-2014
Quote:
Originally Posted by Yoda
Code:
awk '
        NR == 50 {
                i = sprintf ( "%s", substr ( $0, 1, 5 ) )
                j = sprintf ( "%s", substr ( $0, 6 ) )
                gsub ( "A", "B", j )
                gsub ( "M", "N", j )
                $0 = i j
        }
        1
' file

it is working .. thanks a lot

---------- Post updated 01-16-14 at 03:11 PM ---------- Previous update was 01-15-14 at 06:24 PM ----------

Quote:
Originally Posted by Yoda
Code:
awk '
        NR == 50 {
                i = sprintf ( "%s", substr ( $0, 1, 5 ) )
                j = sprintf ( "%s", substr ( $0, 6 ) )
                gsub ( "A", "B", j )
                gsub ( "M", "N", j )
                $0 = i j
        }
        1
' file

Hello.

If my 18 th line is

NM1*IL*1*LYNCH*WILLIAM****MI*11111

Code:
awk '
        NR == 18 {
                i = sprintf ( "%s", substr ( $0, 1, 10 ) )
                j = sprintf ( "%s", substr ( $0, 11 ) )
		  gsub ( "A", "Z", j )
                gsub ( "B", "Y", j )
                gsub ( "C", "X", j )
                gsub ( "D", "W", j )
                gsub ( "E", "V", j )
                gsub ( "F", "U", j )
                gsub ( "G", "T", j )
gsub ( "H", "S", j )
gsub ( "I", "R", j )
gsub ( "J", "Q", j )
gsub ( "K", "P", j )
gsub ( "L", "O", j )
gsub ( "M", "N", j )
gsub ( "N", "M", j )
gsub ( "O", "L", j )
gsub ( "P", "K", j )
gsub ( "Q", "J", j )
gsub ( "R", "I", j )
gsub ( "S", "H", j )
gsub ( "T", "G", j )
gsub ( "U", "F", j )
gsub ( "V", "E", j )
gsub ( "W", "D", j )
gsub ( "X", "C", j )
gsub ( "Y", "B", j )
gsub ( "Z", "A", j )
gsub ( "a", "z", j )
gsub ( "b", "y", j )
gsub ( "c", "x", j )
gsub ( "d", "w", j )
gsub ( "e", "v", j )
gsub ( "f", "u", j )
gsub ( "g", "t", j )
gsub ( "h", "s", j )
gsub ( "i", "r", j )
gsub ( "j", "q", j )
gsub ( "k", "p", j )
gsub ( "l", "o", j )
gsub ( "m", "n", j )
gsub ( "n", "m", j )
gsub ( "o", "l", j )
gsub ( "p", "k", j )
gsub ( "q", "j", j )
gsub ( "r", "i", j )
gsub ( "s", "h", j )
gsub ( "t", "g", j )
gsub ( "u", "f", j )
gsub ( "v", "e", j )
gsub ( "w", "d", j )
gsub ( "x", "c", j )
gsub ( "y", "b", j )
gsub ( "z", "a", j )
gsub ( 0, 9, j )
gsub ( 1, 8, j )
gsub ( 2, 7, j )
gsub ( 3, 6, j )
gsub ( 4, 5, j )
gsub ( 5, 4, j )
gsub ( 6, 3, j )
gsub ( 7, 2, j )
gsub ( 8, 1, j )
gsub ( 9, 0, j )

                $0 = i j
        }
        1
 ' test_tdm.dat

the ouput is


input ) NM1*IL*1*AYNCH*WILLISM****US*11111
output ) NM1*IL*1*ABMCH*DILLISM****US*11111

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
# 11  
Old 01-16-2014
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).
This User Gave Thanks to Don Cragun For This Post:
# 12  
Old 01-16-2014
Quote:
Originally Posted by Don Cragun
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).
without sequences , can we put everything together , so that i will achieve what i want or any other suggestion

Last edited by Rajesh_us; 01-16-2014 at 04:36 PM..
# 13  
Old 01-16-2014
How about this:

Code:
awk '
    BEGIN{
      F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
      T="ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210"
      for(i=1;i<length(F);i++)N[substr(F,i,1)]=substr(T,i,1);
    }
    NR == 18 {
      out=x
      for(i=1;i<length;i++) {
         c=substr($0,i,1)
         out=out (i>10&&(c in N)?N[c]:c)
      }
      $0=out
    }
    1
' infile


Last edited by Chubler_XL; 01-16-2014 at 05:13 PM.. Reason: Missed 5 in F and T
This User Gave Thanks to Chubler_XL For This Post:
# 14  
Old 01-16-2014
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
Code:
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......
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Remove line break at specific position

Hi, I need to remove line breaks from a file, but only the ones at specific position. Input file: this is ok this line is divided at posit ion 30. The same as this one, also position 30 the rest of lines are ok with different lengths The longest ones are always s plitted at same... (15 Replies)
Discussion started by: qranumo
15 Replies

3. Shell Programming and Scripting

Find and replace with 0 for characters in a specific position

Need command for position based replace: I need a command to replace with 0 for characters in the positions 11 to 20 to all the lines starts with 6 in a file. For example the file ABC.txt has: abcdefghijklmnopqrstuvwxyz 6abcdefghijklmnopqrstuvwxyz abcdefghijklmnopqrstuvwxyz... (4 Replies)
Discussion started by: thangabalu
4 Replies

4. Shell Programming and Scripting

How to concatene files and put each line of files on a specific position ?

Hi, I have some files that i want to concatene and put each of lines of this files on a specific position : File1 AAAAAAA BBBBBBB CCCCCCC File2 DDDDDDD EEEEEEE FFFFFFF File3 GGGGGG HHHHHH IIIIII New file (6 Replies)
Discussion started by: apippo70
6 Replies

5. Shell Programming and Scripting

Printing characters at specific position in line

Hi, I am trying to get an output like : +----------------------------------+ ----------- + + some variable substitution + some text + Is there a way I can specify in printf (in ksh) the particular position I want to print a character, and also repeat a character from... (1 Reply)
Discussion started by: neil.k
1 Replies

6. Shell Programming and Scripting

Find the position of a pattern on a line from a csv file

hello I'm doing a unix program and i'm using many file csv.in each csv file the colums are separated by ";" I would like to know the position of a pattern. For example for a line yyyy, bbbb, cccc; ddddd;eeee. I will like for example by finding the position of the pattern "cccc" and the response is... (6 Replies)
Discussion started by: papis
6 Replies

7. Shell Programming and Scripting

Search in specific position and print the whole line

I have two files abc.dat and sant.dat (Big file 60k rows) for every line's 1,4 of abc.dat need to seach if this is present on 28,4 of sant.dat every line. if its present the output needs to go to bde.dat Example: contents abc.dat aaaa bbbb cccc dddd contents sant.dat this is... (4 Replies)
Discussion started by: ssantoshss
4 Replies

8. Shell Programming and Scripting

search a line and insert string into specific at position

Hi, guys. I have one question: How can I search for a line with certain string in it and then insert a string into this line? For example: There is a file called shadow, the contents of it are below: ************************** ... yuanz:VIRADxMsadfDF/Q:0:0:50:7:::... (9 Replies)
Discussion started by: daikeyang
9 Replies

9. Shell Programming and Scripting

Deleting Characters at specific position in a line if the line is certain length

I've got a file that would have lines similar to: 12345678 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 23456781 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 34567812 x.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 45678123 x.00 xx.00 xx.00 xx.00 xx.00 x.00 xxx.00 xx.00 xx.00 xx.00 xx.00... (10 Replies)
Discussion started by: Cailet
10 Replies

10. Shell Programming and Scripting

check position of end of line for some specific lines

-------------------------------------------------------------------------------- Have to check in a file that the lines starting with 620 and 705 are ending at same posiotin. 82012345 62023232323 70523949558 62023255454 9999 In the above lines, i have to check the lines starting... (1 Reply)
Discussion started by: senthil_is
1 Replies
Login or Register to Ask a Question