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
# 8  
Old 11-10-2010
Thank you everyone for sharing your thoughts

Hi rwuerth, when i used your code, for some reason it is removing any existing spaces from the record.

i.e. say if the record to be converted is
Code:
 dentonabc12345tx 76201 usa

by using the script you have shared i am geting output as
Code:
 denton12345678tx76201usa

instead i am expecting the output to be as
Code:
 denton12345678tx 76201 usa

# 9  
Old 11-10-2010
perl is very easy to do so

perl -ne '{s/(12345)(?{$cnt++;$tmp = int(($cnt-1)\/4);})/$1+$tmp/e;print;}' yourfile
# 10  
Old 11-11-2010
I do not have perl on my machine and i have to write it using ksh
# 11  
Old 11-11-2010
Quote:
Originally Posted by vpv0002
i have to write it using ksh
Is this homework?
# 12  
Old 11-11-2010
Quote:
Originally Posted by Corona688
Is this homework?
This is not an homework
# 13  
Old 11-11-2010
Posix:
Code:
i=$((12345678 * 4))
while read line
do
  echo "${line%abc12345*}$((i++/4))${line#*abc12345}"
done < infile > outfile

bash/ksh93:
Code:
i=$((12345678 * 4))
while read line
do
   echo "${line/abc12345/$((i++/4))}"
done < infile > outfile

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