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
# 29  
Old 01-29-2014
Quote:
Originally Posted by Chubler_XL
This should match on field three, notice how if matching the "*" (star) character two backquotes are needed.

The star on the end is to match with the rest of the string so example is looking for any field 3 that starts with "NM1*IL*1"

Code:
k3='NM1\\*IL\\*1*'
awk -v k3="$k3" '
    BEGIN{
      F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
      T="ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210"
      for(i=1;i<=length(F);i++)N[substr(F,i,1)]=substr(T,i,1);
    }
    $3 ~ k3 {
      out=x
      for(i=1;i<=length($3);i++) {
         c=substr($3,i,1)
         out=out (i>9&&(c in N)?N[c]:c)
      }
      $3=out
    }
    1
' $file

sorry i gave wrong information.. here it is i wanted.. please refer the updated one

i don't have any issue with the above code. is it possible to make the above code work on single field .. say field $3 .. first it has to search a specific field ($3) . in field # 3, it has to replace from 9 th position to end of the field ...

first i need to locate the field i want ,.. it could be field #3 or 4 or 5 or anything
then on the located field .. i need to replace from start position to end postion.. say 9 to 12 or 5 to end of that that specific field
# 30  
Old 01-29-2014
You have changed your requirements so many times that I have absolutely no idea what you want to do. Please give us a complete set of requirements.

Are we processing a single line in a file or every line in a file? If it is a single line, how do we identify which line you want to process? If some lines are not to be processed, are they copied unchanged from the input to the output, or are they removed from the output?

What constitutes a field?

Are you trying to change data from a certain point in a field to the end of that field, to the end of that line, for some number of characters in that field, or for some number of characters in that line?
This User Gave Thanks to Don Cragun For This Post:
# 31  
Old 01-29-2014
Quote:
Originally Posted by Don Cragun
You have changed your requirements so many times that I have absolutely no idea what you want to do. Please give us a complete set of requirements.

Are we processing a single line in a file or every line in a file? If it is a single line, how do we identify which line you want to process? If some lines are not to be processed, are they copied unchanged from the input to the output, or are they removed from the output?

What constitutes a field?

Are you trying to change data from a certain point in a field to the end of that field, to the end of that line, for some number of characters in that field, or for some number of characters in that line?

Sorry for inconvenience !

I will identify few lines in the file based on some predefined strings. On each identified line , I need to do manipulation on only few fields. Again within the fields, I will manipulate only on certain range of position. Yes, unprocessed input will be copied into output.

What was previous requirement

Input
Code:
NM1*IL*1*AYNCH*AILLIAM****AI*R02089540

output
Code:
NM1*IL*1*ZBMXS*ZROORZN****ZR*I97910459

After “1*” , everything is manipulated

What is current requirement

Example 1

input
Code:
NM1*IL*1*AYNCH*AILLIAM****AI*R02089540

output
Code:
NM1*IL*1*AYNCH*ZROOIAM****AI*R02080540

On fifth field from 1(A) to 4 (L) needs to be converted

Example 2

Input
Code:
NM1*IL*1*AYNCH*AILLIAM****AI*R02089540

ouput
Code:
NM1*IL*1*AYNCH*ZROORZN****AI*R02080540

On fifth field I'm converting everything.
# 32  
Old 01-29-2014
Looks like some more flexibility in the code could help you change things to get what you require.

Start with this for example 2 (Change all of field 5):

Code:
awk -F\* '
function rot(string,start,end,i,ret) {
   if(!end) end=length(string);
   if(!start) start=1;
   for(i=start;i<=length(string);i++) {
        c=substr(string,i,1)
        ret=ret (i>=start&&i<=end&&(c in N)?N[c]:c)
   }
   return ret
}
BEGIN{
    OFS=FS
    F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    T="ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210"
    for(i=1;i<=length(F);i++)N[substr(F,i,1)]=substr(T,i,1);
}

{ $5=rot($5) } 1 ' $file

Change to this for example 1 (chars 1-4 of field 5):
Code:
awk -F\* '
function rot(string,start,end,i,ret) {
   if(!end) end=length(string);
   if(!start) start=1;
   for(i=start;i<=length(string);i++) {
        c=substr(string,i,1)
        ret=ret (i>=start&&i<=end&&(c in N)?N[c]:c)
   }
   return ret
}
BEGIN{
    OFS=FS
    F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    T="ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210"
    for(i=1;i<=length(F);i++)N[substr(F,i,1)]=substr(T,i,1);
}

{ $5=rot($5,1,4) } 1 ' $file

This User Gave Thanks to Chubler_XL For This Post:
# 33  
Old 01-29-2014
where should i include the line number ?? i need to

Something like this

Code:
 NR == k3

or

Code:
k3='NM1\\*IL\\*1*'
awk -v k3="$k3" '


and i tried to running the above command with line numbers in it ( im not sure i did correct). Here is the content from terminal

Code:
+ awk '-F*' k3=19 '
function rot(string,start,end,i,ret) NR == k3 {
   if(!end) end=length(string);
   if(!start) start=1;
   for(i=start;i<=length(string);i++) {
        c=substr(string,i,1)
        ret=ret (i>=start&&i<=end&&(c in N)?N[c]:c)
   }
   return ret
}
BEGIN{
    OFS=FS
    F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    T="ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210"
    for(i=1;i<=length(F);i++)N[substr(F,i,1)]=substr(T,i,1);
}

{ $5=rot($5,1,5) } 1 ' Xdata_6513155487964.xml-3C488ABB-22C2-4925-A6FF-33CF6A767268.dat
awk: cmd. line:1: fatal: cannot open file `

Code what i gave

Code:
awk  -F\*  k3="$k3" '
function rot(string,start,end,i,ret) NR == k3 {
   if(!end) end=length(string);
   if(!start) start=1; 
   for(i=start;i<=length(string);i++) {
        c=substr(string,i,1)
        ret=ret (i>=start&&i<=end&&(c in N)?N[c]:c)
   }
   return ret
}
BEGIN{
    OFS=FS
    F="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    T="ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba9876543210"
    for(i=1;i<=length(F);i++)N[substr(F,i,1)]=substr(T,i,1);
}

{ $5=rot($5,1,5) } 1 ' $file


Last edited by Rajesh_us; 01-29-2014 at 11:28 PM..
# 34  
Old 01-29-2014
Like this:

Code:
k3=17
awk -F\* -v k3="$k3" '
...
NR == k3 { $5=rot($5) } 1 ' $file

or this

Code:
k3='NM1\\*IL\\*1*'
awk -F\* -v k3="$k3" '
...
$0 ~ k3 { $5=rot($5,1,4) } 1' $file

This User Gave Thanks to Chubler_XL For This Post:
# 35  
Old 01-30-2014
it works.. just thanks is not enough for u guys... for now , thanks a lot

---------- Post updated at 11:59 PM ---------- Previous update was at 10:40 PM ----------

Quote:
Originally Posted by Chubler_XL
Like this:

Code:
k3=17
awk -F\* -v k3="$k3" '
...
NR == k3 { $5=rot($5) } 1 ' $file

or this

Code:
k3='NM1\\*IL\\*1*'
awk -F\* -v k3="$k3" '
...
$0 ~ k3 { $5=rot($5,1,4) } 1' $file



i'm afraid to ask a question again. i'm not adding up any new requirements but i have few more cases (scenario). This will be my last question

Whatever you provided, its work fine

here i have two more scenario

Example 3

input

Code:
NM1*IL*1*AYNCH*AILLIAM****AI*R02089540

output

Code:
NM1*IL*1*AYNCH*AIOORZN****AI*R02080540


Example 4

input

Code:
NM1*IL*1*AYNCH*AILLIAM****AI*R02089540

output

Code:
NM1*IL*1*AYNCH*AIOOIAM****AI*R02080540

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