Replacing string by incrementing number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing string by incrementing number
# 1  
Old 07-24-2013
Replacing string by incrementing number

Dear all
Say I have a file as


Code:
 
! TICKET NBR : 234 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 !
! TICKET NBR : 1809 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 !
! TICKET NBR : 587 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 45 ! 3100.2.134.2 6 !

It would be great, if i could issue a script (Vi/Bash/awk, ...) in order to obtain

Code:
! TICKET NBR : 1 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 !
! TICKET NBR : 2 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 !
! TICKET NBR : 3 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 45 ! 3100.2.134.2 6 !

So, the "replace string" should be replaced by another string containing the counter of replacement.
Could you help me with this?

Thanks a lot.


Last edited by OTNA; 07-24-2013 at 08:38 PM..
# 2  
Old 07-24-2013
How about this:

Code:
awk 'NF > 4 {$5 = ++N} 1' infile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 07-24-2013
Someone already posted a very elegant awk solution, so this is just because I like trying to solve things in pure shell.

Code:
i=1;while read -a line;do line[4]=$i;echo "${line[@]}";let i=i+1;done < infile

This User Gave Thanks to DeCoTwc For This Post:
# 4  
Old 07-24-2013
awesome ,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX replacing and incrementing number

Hi I am unix newbie looking for a unix bash script that can make it easier to do my code work. we have a code number for each code block that we want to incrementally assign. We have 10000 of these and it is very laborious to do this one by one. so what we want is start from the top of the... (4 Replies)
Discussion started by: chamajid
4 Replies

2. UNIX for Beginners Questions & Answers

Incrementing the New File Version Number

Hi, This is my first post here. I am using cygwin on Windows 7. I am starting with a data file with filename "name_1.ext", like "20180831_snapgenotypes_1.csv". The "_1" before ".ext" is a version number. Integers (0-99) are sufficient. They don't have to be like "1.0.0". The filename may... (2 Replies)
Discussion started by: minimalist
2 Replies

3. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

4. UNIX for Dummies Questions & Answers

Incrementing the New File Version Number

Hello All, In the below script i am trying to check and list the file names, get the last file with highest version number and then increment the version number when i create another file. Example: file1 is COBANK_v1.xml and file2 i want to put it as COBANK_v2.xml, to achieve this i am using awk... (15 Replies)
Discussion started by: Ariean
15 Replies

5. Shell Programming and Scripting

Incrementing number in bash

I have the following code and getting the error ./raytrac.bash: line 231: ((: 0++: syntax error: operand expected (error token is "+") iarg = 0 iarg=0 narg=$# # Number of arguments passed. echo "narg = $narg" argsArr=("$@") # Set... (1 Reply)
Discussion started by: kristinu
1 Replies

6. UNIX for Dummies Questions & Answers

replacing a string with another string in a txt file

Dear all, I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot! 8 70003200 21.6206 9 70005700 17.5064 10 70002200 . 11 70005100 19.1001 17 70008000 16.1970 32 70012400 26.3465 33... (9 Replies)
Discussion started by: forevertl
9 Replies

7. Shell Programming and Scripting

Unix c-shell - replacing/incrementing values in columns?

2 21 1 12 3 123 4 1234 6 49 0 49 33 212 I need to replace/increment all the values in the 2nd column that correspond to 0 in the first column. so for 0 49 i would get 0 50 this can be done through: paste num4.txt... (14 Replies)
Discussion started by: audrey_flox
14 Replies

8. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

9. Shell Programming and Scripting

Replacing word and Incrementing

Hi I'm having difficulty in writing a script with searching a specified word using sed and replaces that word with numbers that is incremented I tried this: #!/bin/sh awk '{ for (i=2010; i<=NF; i++) sed 's/TBA/$i; }' filename.txt > outputfile.txt but it doesn't work. here is my desired... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

10. Shell Programming and Scripting

perl + array and incrementing number

morning guys and gals, I am haveing a problem, a friend helped me out with this script but i dont know how to add incrementing number for each movie in movie.list. this is what i have so far. any assistance would be great. I have removed the GT and LT symbols so you can see what is going on... (5 Replies)
Discussion started by: Optimus_P
5 Replies
Login or Register to Ask a Question