Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I need an answer using unix awk, sed, or perl commands to replace certain digits within one field through the file. ex. "1" to be replaced with "2", "2" to be replaced with "3", and so on.
The original file looks like this:546720,54,1,,0,2001,1.205
324315,52,0,,0,2005,0.200and it should be like this:
657831,54,1,,0,2001,1.205
435426,52,0,,0,2005,0.200In the example I just add +1 for all digits, or "1" is replaced by "2" untill "9" is replaced by "0".
Sorry this was answered in Programming category but I need in Unix, the answer using perl didn't work.
Thanks in advance

I must respectfully disagree with you. The perl solution that Fishmonger provided in the Programming forum definitely DOES work:
perl -F',' -ane '$,=","; $F[0] =~ tr/0123456789/1234567890/; print @F' file1
That said, here is a Korn shell unix script which reads file1 using a while loop. For each line it parses the first field and the rest of the line using the comma as a field separator. I then change field 1 to your requirement using the Unix translate, tr, command. I then glue the line back together sending the output to file2.
#!/bin/ksh while IFS="," read f1 fx do f1=$(echo "$f1"| tr [0123456789] [1234567890]) echo "$f1,$fx" done < file1 > file2Personally, I like the perl solution better.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |