Finding a String and Replacing it with an incremental value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding a String and Replacing it with an incremental value
# 1  
Old 11-09-2010
Finding a String and Replacing it with an incremental value

Hi Friends,

I have a text file which has about 200,000 records in it. we have a string which repeats in each and every record. So we have to write a script in ksh which finds that string on each line and replaces it with a new string(incremental value) for a set every four records.

To be more clear please look at the sample data below

say file a.txt has some 8 records in it
Code:
dallastexasabc12345tx
dentonabc12345tx
houstoabc12345tx
sanfraabc12345tx
oklahamaabc12345oh
irvingfrabc12345tx
mcaurtabc12345tx
kingkotabc12345tx

abc12345 is the repeating string in each and every record, we have to replace it with an incremental value say 12345678 in first four records and then with 12345679 in the next four records and so on.

Any help is greatly appreciated.
Thank you.
Moderator's Comments:
Mod Comment
Please use code tags

Last edited by vgersh99; 11-09-2010 at 01:00 PM.. Reason: Code tags, please!
# 2  
Old 11-09-2010
This works for me in ksh88

Code:
typeset -i cnt=1
typeset -i seq=678
cat /dev/null > newfile.txt
while read string
do
echo $string | sed -e "s/\(.*\)\(12345\)\(.*\)/\1\2$seq\3/" >> newfile.txt
((cnt%4 == 0)) && seq=$((seq+1))
cnt=$((cnt+1))
done < mod.txt

Your input in "mod.txt"

Code:
dallastexasabc12345tx
dentonabc12345tx
houstoabc12345tx
sanfraabc12345tx
oklahamaabc12345oh
irvingfrabc12345tx
mcaurtabc12345tx
kingkotabc12345tx

Was changed to

Code:
dallastexasabc12345678tx
dentonabc12345678tx
houstoabc12345678tx
sanfraabc12345678tx
oklahamaabc12345679oh
irvingfrabc12345679tx
mcaurtabc12345679tx
kingkotabc12345679tx

in "newfile.txt"
# 3  
Old 11-09-2010
Instead of replacing whole string abc12345 with 12345678 and so on it is adding 678 at the end of the string.
Input file mod.txt
Code:
 
dallastexasabc12345tx
dentonabc12345tx
houstoabc12345tx
sanfraabc12345tx
oklahamaabc12345oh
irvingfrabc12345tx
mcaurtabc12345tx
kingkotabc12345tx

It should actually look like:
newfile.txt
Code:
dallastexas12345678tx
denton12345678tx
housto12345678tx
sanfra12345678tx
oklahama12345679oh
irvingfr12345679tx
mcaurt12345679tx
kingkot12345679tx


Last edited by vpv0002; 11-09-2010 at 03:17 PM..
# 4  
Old 11-09-2010
Code:
awk -v var=12345678 '
NR>1 && NR%4==1 {var++}
{
  s1=s2=$0
  sub("[0-9].*","",s1)
  sub(".*[0-9]","",s2)
  print s1 var s2
}'  file

# 5  
Old 11-09-2010
Missed the 'abc' part, sorry. It's a simple fix.

Code:
typeset -i cnt=1
typeset -i seq=678
cat /dev/null > newfile.txt
while read string
do
echo $string | sed -e "s/\(.*\)\(abc12345\)\(.*\)/\112345$seq\3/" >> newfile.txt
((cnt%4 == 0)) && seq=$((seq+1))
cnt=$((cnt+1))
done < mod.txt

# 6  
Old 11-09-2010
bash code:
  1. #!/bin/bash
  2. i=12345678
  3. while :
  4. do
  5.    for i in {1..4}
  6.    do read || break 2
  7.       echo ${REPLY/abc12345/$i}
  8.    done
  9.    ((i++))
  10. done <file-in >file-out

Last edited by frans; 11-09-2010 at 04:25 PM..
# 7  
Old 11-09-2010
Also, got to thinking about the 200,000 lines you have. Might want to avoid extra processes and not use sed inside a loop. May not make a difference, but the following works and does take less system resources/time (fractions of a second) on my system:

Code:
typeset -i cnt=1
typeset -i seq=678
AL_CONST=abc12345
NM_CONST=12345
cat /dev/null > newfile.txt
while read orig_str 
do
  left_str=${orig_str%%${AL_CONST}*}
  rght_str=${orig_str##*${AL_CONST}}
  new_str="${left_str}${NM_CONST}${seq}${rght_str}"
  echo $new_str >> newfile.txt
  ((cnt%4 == 0)) && seq=$((seq+1))
  cnt=$((cnt+1))
done < mod.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding a format in a file and replacing it

Hi, I need help in the following: I have a file in directory with mutiple comma seperated values. One of the value is a date and time format like 2012-04-10 xx:yy:zz I need to find that time format in the file and then replace it with xx:yy+1:zz and then save it as a new file and copy it to a... (3 Replies)
Discussion started by: rabh
3 Replies

2. Shell Programming and Scripting

Finding and replacing whole line using sed

Hi all, assume that i am having the following line in a file called file1. triumph and disaster must be treated same. I want to replace this line with. follow excellence success will chase you. is it possible to do this using sed. if possible kindly post me the... (2 Replies)
Discussion started by: anishkumarv
2 Replies

3. Homework & Coursework Questions

Finding/replacing text and redirection help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: What command would rename "sequentialInsert", in ~cs252/Assignments/commandsAsst/project/arrayops.h, to... (2 Replies)
Discussion started by: lothwen
2 Replies

4. Shell Programming and Scripting

Finding and replacing links

Hi, I would like to do a scripting for finding the links based on the name I have and replace the links with the new name. General find command lists everything for that links ( means all the sub-sirs and all the files), i need only the main link and replace. Can you anyone give me some... (1 Reply)
Discussion started by: rrb2009
1 Replies

5. UNIX for Dummies Questions & Answers

Finding & Replacing specific Fields

All I have a very large file (aproximately 150,000) as shown below separated by pipe "|". I need to replace data in 2, 16, 17, 23 fields that are of time stamp format. My goal is to look in those fields and it ends with "000000|" then replace it with "000|". In other words, make it as 6 digit... (2 Replies)
Discussion started by: ddraj2015
2 Replies

6. Shell Programming and Scripting

Need help in finding and replacing port numbers.

Hi All, I am trying to write a shell script which firstly will search some files and then increase the port numbers mentioned in them by a certain no. let me clear it with an example- suppose there r few files a,b,c,d.... file a's content- <serverEntries xmi:id="ServerEntry_1"... (3 Replies)
Discussion started by: ankushsingh10
3 Replies

7. Shell Programming and Scripting

Writing script for finding a string and replacing

Hi all i need some help in writing a small script that searches a string and then replaces it by a new string for searching the best result i get is from find comand combined with xargs e.g. find . -name "*.*" |xargs grep -l "search string" this command I use in root directory and this... (4 Replies)
Discussion started by: Aditya_IT
4 Replies

8. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies

9. Shell Programming and Scripting

HELP !! Builting string in incremental order

I have files in a folder like .. citi11082006_1.trn citi11082006_2.trn citi11082006_3.trn ... ... ... citi11082006_13.trn .,... citi11082006_nn.trn Each file will have one string inside and I am builting a master string based out the string in the sub files by merging all into one by... (6 Replies)
Discussion started by: u263066
6 Replies

10. UNIX for Dummies Questions & Answers

shellscript for finding and replacing in DG-UNIX

Newby here..... Is there any way of doing a find & replace within a huge file other than using something like : " vi - FileForReplacing < /u1/excel/consultants " contents of consultants file looks like :1,$s/0000031/CON/g :1,$s/0001032/JONES/g :1,$s/0001355/SMITH/g... (3 Replies)
Discussion started by: Gerry405
3 Replies
Login or Register to Ask a Question