sed command on AIX, replace specific characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command on AIX, replace specific characters
# 1  
Old 03-31-2017
sed command on AIX, replace specific characters

Hi,

Im using sed on an AIX machine. I am trying to change the 137-139 characters if they are a ' 36'/'000' to a '036'. The positions that need to be changed are fixed.

the source data that I have is
Code:
$cat v.txt
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY OF AUSTRALIA       CASTLE HILL  AU 5599   0000000097950007077Y10001 6022100000     0 0
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY OF AUSTRALIA       CASTLE HILL  AU 5599   000000009795 367077Y10001 6022100000     0 0
4000422985400050462239065593606500000007422985707771046154054910075641MC0318AMWAY OF AUSTRALIA       CASTLE HILL  AU 5599   0000000097954367077Y10001 6022100000     0 0


I am using the below command which I got from this forum
Code:
sed -r -i.Original '/^4000/s/(.{136}) 36|000/\1036/' file

but it is not working on AIX
# 2  
Old 03-31-2017
You are using GNU sed syntax. -i means 'in place' the -i.Original is a suffix to add to a tmp file so you can get your old file back if there was an error. -r is also not standard.

See: sed

You should read the man page for sed on your AIX FIRST before doing copy of code from here.
Code:
sed 'pattern and instructions go here'  inputfile >tmpfile
# if no mistakes in tmpfile be sure to check it; then
mv tmpfile inputfile

# 3  
Old 03-31-2017
Quote:
Originally Posted by jim mcnamara
You are using GNU sed syntax. -i means 'in place' the -i.Original is a suffix to add to a tmp file so you can get your old file back if there was an error. -r is also not standard.

See: sed

You should read the man page for sed on your AIX FIRST before doing copy of code from here.
Code:
sed 'pattern and instructions go here'  inputfile >tmpfile
# if no mistakes in tmpfile be sure to check it; then
mv tmpfile inputfile

sorry about that, I actually modified the code above to suit the AIX sed and am using the below code now
Code:
$cat v.txt
4000422985400050170315170328000000000006333522000000000173000160000000MS000000652363009301030000800000013111000460021000060000000726710035001200000000000000000000000000
$sed '/^4000/s/\(.\{136\}\)000/\1036/' v.txt
4000422985400050170315170328000000000006333522000000000173000160000000MS000000652363009301030000800000013111000460021000060000000726710035001203600000000000000000000000

what confuses me is why are the characters are positions 143,144 and 145 changing when i think the above AIX sed code only replaces characters at positions 137-139
# 4  
Old 03-31-2017
Hi,

What we forgot to mention in the other thread is that when you use 000 instead of ... then you need to use an anchor for the beginning of the line in the form of a caret ( ^ ), otherwise it will try to match 000 with 136 arbitrary characters before it ..

--
Further, alternation is not part of regular sed (which uses BRE, basic regular expressions), instead try specifying the substitution twice:
Code:
sed '/^4000/{s/^\(.\{136\}\)000/\1036/; s/^\(.\{136\}\) 36/\1036/;}' file


---
Alternation would have worked with GNU sed with the -r option (or BSD sed with the -E option) that allows the use of extended regular expressions (ERE), which does have alternation, but GNU sed is usually not present as standard on AIX.
Try:
Code:
sed -r '/^4000/s/^(.{136})( 36|000)/\1036/' file


Last edited by Scrutinizer; 04-01-2017 at 03:39 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-31-2017
You can easily port it to perl
Code:
perl -i.Original -pe '/^4000/ and s/(.{136}) 36|000/\1036/' file

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 03-31-2017
Quote:
Originally Posted by MadeInGermany
You can easily port it to perl
Code:
perl -i.Original -pe '/^4000/ and s/(.{136}) 36|000/\1036/' file

thanks will try it out and let you knowSmilie
# 7  
Old 03-31-2017
I missed something: \1 is certainly a backreference to the ( ) and should become $1 in perl, and needs to be separated from the following text
Code:
perl -i.Original -pe '/^4000/ and s/(.{136}) 36|000/${1}036/' file

This User Gave Thanks to MadeInGermany 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

Replace Language Specific Characters in File

Hi Team, I have requirement to replace Language Specific Characters in File. We have set of characters, it should be replaced to a different character in the file. I have around 38 characters which should replaced to different destination character. Please help. Thanks Bharat (3 Replies)
Discussion started by: bharath561989
3 Replies

2. UNIX for Dummies Questions & Answers

How to replace and remove few junk characters from a specific field?

I would like to remove all characters starting with "%" and ending with ")" in the 4th field - please help!! 1412007819.864 /device/services/heartbeatxx 204 0.547%!i(int=0) 0.434 0.112 1412007819.866 /device/services/heartbeatxx 204 0.547%!i(int=1) 0.423 0.123... (10 Replies)
Discussion started by: snemuk14
10 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

Replace specific characters until pattern

Hi experts, My file looks something like this. abcXX4,7,234 abc,defg,45XX23,74,123 The number of commas left of the XX can vary. The question is how can I replace all the commas left of the 'XX' with an underscore? abcXX4,7,234 abc_defg_45XX23,74,123 Thanks! (5 Replies)
Discussion started by: abercrom
5 Replies

5. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

6. Shell Programming and Scripting

Awk command to replace specific position characters.

Hi, I have a fixed width file. The way this file works is say for example there are 30 columns in it each with different sizes say 10,5,2, etc... If data in a field is less than the field size the rest of it is loaded with spaces. I would like an awk command to that would replace I have... (8 Replies)
Discussion started by: pinnacle
8 Replies

7. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

8. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

9. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

10. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies
Login or Register to Ask a Question