replace numbers in records


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace numbers in records
# 15  
Old 10-12-2011
We'd need to see the actual code, actual input data, and actual expected result to tell if your code actually does what you think it does.
# 16  
Old 10-15-2011
sorry for being late .. i just logged in the machine:

When the case was to check a specific record
Code:
ASX201123000000010000000202011282004130001001111114320000000200000000000000000010000002300000000100000000101001143211079191

(ONLY when 0000020 and 001 or 002, we change the 0000020 to 0000000)

The actual code was :
Code:
#!/usr/bin/sh
orgv=ASX................0000020.\{77\}00[12]
orgc=0
toproc="/REV/TO_PROCESS"
lgfile="/REV/scripts/rev.log"
for revfile3 in $toproc/REV*
do
      orgc=`grep -E "$orgv" $revfile3 |wc -l`
      if [ $orgc -ge 1 ]
      then
         sed  '/^ASX................0000020.\{77\}00[12]/s/./0/25' $revfile3 > $toproc/temp
         mv $toproc/temp $revfile3
        echo File $revfile3 has some values that have been changed >> $lgfile
    fi
    orgc=0
done

The above code is tested and works fine


NOW the case to check the records has changed..(when the 7 red digits has number in it and there is 001 or 002, change the 7 red digits to zeros)

radoulov and ygemici, kindly, gave me the sed command for the new case:
Code:
sed 's/^\(ASX.\{16\}\).\{7\}\(.\{77\}00[12].*\)/\10000000\2/' infile

which works fine,

also radoulov gave me code to test the records before using the sed command

Quote:
Originally Posted by radoulov
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

which works greatSmilie

Now and out of curiosity, i want the pattern to assign it to orgv variable as i did before. what i mean is something like this:
Code:
orgv=ASX................(any number in the 7 digits).\{77\}00[12]

CAN I echo the lines numbers affected by the sed... if the sed changed lines 5,6,8 in a file, can i show these lines numbers in the log?
thank you

Last edited by neemoze; 10-15-2011 at 06:36 AM..
# 17  
Old 10-19-2011
Quote:
Originally Posted by neemoze
[...]
Can I echo the lines numbers affected by the sed... if the sed changed lines 5,6,8 in a file, can i show these lines numbers in the log?
You can. This grep/cut pipeline will give you the affected line numbers:

Code:
egrep -vn '^ASX.{16}0{7}' infile | cut -d: -f1

Note that egrep is not standard (grep -E is).
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