replace numbers in records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace numbers in records
# 1  
Old 10-10-2011
replace numbers in records

hello every one
I have file with following records

Code:
begin
ASX120016719
ASX190006729
ASX153406729
ASX190406759
ASX180006739
end

for each record there is ASX word then 9 digits after it (NO spaces included)
what i want is to :

1- skip ASX
2-skip first 2 digits after ASX word
3- check digits 3-5, if all zeros then ok, go to next record
4- if any digit from 3 to 5 is a number then check digit 8
if digit 8 is 1 or 2 then make digits 3-5 zeros
if digit 8 is NOT 1 or 2 then ok go to next record

According to the above records, the output should be:
Code:
begin
ASX120006719
ASX190006729
ASX150006729
ASX190406759
ASX180006739
end

i tried sed command but i could not figure the right syntax..
please how to do it

thank you
# 2  
Old 10-10-2011
Code:
sed '
  /^ASX..000/!s/^\(.\{5\}\)\(...\)\(..[12].*\)/\1000\3/
  ' infile

These 3 Users Gave Thanks to radoulov For This Post:
# 3  
Old 10-10-2011
Excellent solution Radoulov.

I take my hat off to you.Smilie
# 4  
Old 10-10-2011
Thanks Shell_Life,
hope it's working fine on the OP's machine Smilie
# 5  
Old 10-10-2011
Thanks alot Radoulov. It works on hp-ux.
however, I put small records to clear my point..since i'm confused with the sed script you applied, i'll put the exact number of digits in the record

usually the record is like:
Code:
ASX123456789340123456789123456789123456789123456789212345

ASX(9 digits)(3 digits to be checked if they are zeros or numbers)(36 digits)(1 digit to be checked if it is 1 or 2)(5 digits)

how the sed script will be?
thanks again

Last edited by neemoze; 10-11-2011 at 01:25 AM..
# 6  
Old 10-11-2011
It would be easier if you post sample data and expected output.
This User Gave Thanks to radoulov For This Post:
# 7  
Old 10-11-2011
Thank you radoulov, you are right, i thought the short records would be easier for the reader .. i was wrong Smilie..

we receive many files daily, The longest file records we receive like this:
Code:
 
ASX201123000000010000000302011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000010000000302011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000010000000302011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000010000000302011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000000000500002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000302011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000010000000302011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000302011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000001000000002011282004130001001111114320000000200000000000000000010000002300000000100000008101001143211079191
ASX201123000000010000004302011282004130001001111114320000000200000000000000000010000002300000000100000008101001143211079191

1- skip ASX
2-skip first 16 digits after ASX word
3- check digits 17-23, if all zeros then ok, go to next record
4- if any digit from 17 to 23 is a number then check digits 101 to 103
if digits 101 to 103 is 001 or 002 then make digits 17-23 zeros
if digits 101 to 103 is NOT 001 or 002 then ok go to next record

The output should be:

Code:
 
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000001000000002011282004130001001111114320000000200000000000000000010000002300000000100000008101001143211079191
ASX201123000000010000004302011282004130001001111114320000000200000000000000000010000002300000000100000008101001143211079191

another thing is how to assign a record, from above records that meet the conditions, to a variable
ex
orgv=record has NO zeros in digits from 17 to 23 and has 001 or 002 in digits 101 to 103

the reason for the variable is to grep it from the file..then use if statement to do the action if it is true (some times we receive file has the right records that do not need to be changed..)

because the sender sends these files to many receivers, we can't fix it from the sender

I hope i explained it well Smilie..Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace variable value in first file based on records in second

Hello , I have below files a) File A <?xml version="1.0" encoding="UTF-8" standalone="no"?> <root xmlns="http://aaa/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema" version="2.0"> <project name="source"> <mapping name="m_Source"> <parameter... (3 Replies)
Discussion started by: Pratik4891
3 Replies

2. Shell Programming and Scripting

Replace multiple positions in records which match crireria

I have a test file a.txt 001 123 456 789 002 This is just a 001 test data 003 file. I want to clear columns 5 and 6 if the first 3 characters are 001 using awk. I tried following but does not work. Any suggestions? awk 'BEGIN{OFS=FS=""} {if (substr($0,1,3)=="123") $5=" "; $6="... (20 Replies)
Discussion started by: Soham
20 Replies

3. UNIX for Dummies Questions & Answers

How can I replace the lines that start with a star and replace it with numbers start from 1?

I need to replace the (*) in the fist of a list with numbers using sed for example > this file contain a list * linux * computers * labs * questions to >>>> this file contain a list 1. linux 2. computers 3. labs 4. questions (7 Replies)
Discussion started by: aalbazie
7 Replies

4. Shell Programming and Scripting

[Solved] Replace records according to reference

Dear All, I am struggling with the following task and would appreciate some help. I have a large table (file1.txt). The first column of this table contains an ID. I would like to replace the ID with a label according to a reference file. Here is an example: cat infile.txt 0 AJ2312 310 ... (7 Replies)
Discussion started by: GDC
7 Replies

5. Shell Programming and Scripting

How to replace multiple numbers?

hello everyone i searched the net and i could not find script for this request. i believe sed command will do it but i'm not sure about how. my file contains thousands of records, the following is sample: BEGIN ASX15001 BEGIN ASX15000000500020101230 ASX18001020070002010123... (10 Replies)
Discussion started by: neemoze
10 Replies

6. Shell Programming and Scripting

Replace a particular field in all records in a csv file

hi, i have various csv files, the file format is as follows Entry: "1",4,2010/08/15-10-00-00.01,,"E",,,,,,,,,120,0,"M4_","C","KEW-011-5337140-20100916163456-540097","1234567890","N N 0 ",,,"NUK 800100200",,,"NN",,,,,,,,,,,,"0000000001|0001|20150401... (2 Replies)
Discussion started by: niteesh_!7
2 Replies

7. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

8. Shell Programming and Scripting

how to delete records with the given line numbers

I have a file which has about 10000 records and I need to delete about 50 records from the file. I know line numbers and am using sed '134,1357,......d' filename > new file. It does not seem to be working. Please Advice (5 Replies)
Discussion started by: mad_man12
5 Replies

9. UNIX for Dummies Questions & Answers

seperating records with numbers from a set of numbers

I have two files one (numbers file)contains the numbers(approximately 30000) and the other file(record file) contains the records(approximately 40000)which may or may not contain the numbers from that file. I want to seperate the records which has the field 1=(any of the number from numbers... (15 Replies)
Discussion started by: Shiv@jad
15 Replies

10. Shell Programming and Scripting

Numbers of records in SAS dataset

I'm declaring a variable within a Korn shell to represent the total number of records in a SAS dataset and could use a little help with the syntax. This is what I have thus far: #!/usr/bin/ksh RecCount = `sas -x "select count(*) from /users/abc/123/sas_dataset.sas7bdat"` (2 Replies)
Discussion started by: sasaliasim
2 Replies
Login or Register to Ask a Question