replace numbers in records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace numbers in records
# 8  
Old 10-11-2011
Well,
you won't need the variable because this sed script doesn't touch the correct records:

Code:
sed '/^ASX.\{16\}0\{7\}/!s/^\(.\{19\}\)\(.\{7\}\)\(.\{77\}00[12].*\)/\10000000\3/' infile

Let me know if it works (hope I got the numbers right Smilie).
# 9  
Old 10-11-2011
Code:
# sed 's/^\(ASX.\{16\}\)[0-9]\{7\}\(.\{77\}00[1-2].*\)/\10000000\2/' infile
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000010000000002011282004130001001111114320000000200000000000000000010000002300000000100000000201001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000000000000002011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191
ASX201123000000001000000002011282004130001001111114320000000200000000000000000010000002300000000100000008101001143211079191
ASX201123000000010000004302011282004130001001111114320000000200000000000000000010000002300000000100000008101001143211079191

# 10  
Old 10-11-2011
Good point @ygemici,
the second grouping is unnecessary:

Code:
sed '/^ASX.\{16\}0\{7\}/!s/^\(.\{19\}\).\{7\}\(.\{77\}00[12].*\)/\10000000\2/' infile

Your code will do some redundant substitutions (0000000 to 0000000),
but I suppose that's not a problem.

And given the fixed format, I think this should suffice:

Code:
sed 's/^\(ASX.\{16\}\).\{7\}\(.\{77\}00[12].*\)/\10000000\2/' infile

This User Gave Thanks to radoulov For This Post:
# 11  
Old 10-11-2011
Quote:
Originally Posted by radoulov
Good point @ygemici,
the second grouping is unnecessary:

Code:
sed '/^ASX.\{16\}0\{7\}/!s/^\(.\{19\}\).\{7\}\(.\{77\}00[12].*\)/\10000000\2/' infile

Your code will do some redundant substitutions (0000000 to 0000000),
but I suppose that's not a problem.

And given the fixed format, I think this should suffice:

Code:
sed 's/^\(ASX.\{16\}\).\{7\}\(.\{77\}00[12].*\)/\10000000\2/' infile

@radulov
yes, it can be converted to your command for this input Smilie
regards
# 12  
Old 10-11-2011
thanks radoulov, ygemici
I'll try the command as soon as i get on the machine and let you know (i'm sure it will work like the first one Smilie)

I need the variable (or any logical way):
1-To run the sed ONLY when there is at least one record need to be changed,
2-To know if the sed has changed some records in the file or not? After running the sed command inside the script, the entry in the log file associated to the script is (Some records in $Filename has been changed) or (No records in $Filename has been changed)

Last edited by neemoze; 10-11-2011 at 04:34 PM..
# 13  
Old 10-12-2011
You could try something like this (untested):

Code:
for f in *; do
  # count how many lines should be changed
  _count=$(
    egrep '^.{103}00[12]' "$f" | 
      egrep -v '^ASX.{16}0{7}' | 
        wc -l
        )
[ "$_count" != 0 ] && {
      sed '/^ASX.\{16\}0\{7\}/!s/^\(.\{19\}\).\{7\}\(.\{77\}00[12].*\)/\10000000\2/' "$f"
    printf '%s: records changed: %d\n' "$f" "$_count" >> logfile
    } ||
      printf '%s: no change\n' "$f" >> logfile
done

This User Gave Thanks to radoulov For This Post:
# 14  
Old 10-12-2011
Thank you radoulov, the code is great but i have to change in many places.. i'll tell you about my idea which i did before and hope it works now.
long time ago when the condition was to check for a specific record (only when 0000020 and there is 001 or 002), i did like the following:

(i'm not at the machine now, so the syntax is not 100%), i'll explain the idea

Code:
orgv=ASX20112300000001000000020.\{77\}00[12]
for (each file in the Dir)
do
orgc=0
orgc=grep -E $orgv (file name)
 
if [ orgc -ge 1 ] 
then
sed .....
echo "some records in $filename has been changed" >> logfile
else
echo "No records in $filename records has been changed" >> logfile
fi
done

it works fine ..
what you think? and if it is ok,
- how to put the pattern for the orgv variable according to the new condition (any number in the 7 digits and there is 001 0r 002)


thank you

Last edited by neemoze; 10-12-2011 at 06:05 PM..
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