altering numbers in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting altering numbers in files
# 1  
Old 03-26-2008
altering numbers in files

I want to change a number in a file into number -1..
for instance
file_input is

fdisdlf_s35
fdjsk_s27
fsdf_s42
jkljllljkkl_s57
... etc

now i want the output to be
fdisdlf_s34
fdjsk_s26
fdsf_s41
jkljllljkkl_s56
... etc

I was think of using "sed -e 's/2/1/g' -e 's/3/2/g' -e 's/4/3/g' ...etc file_input>new_file"

but it changes each character and give me an output of

fdisdlf_s22
fdjsk_s14
fdsf_s29
jkljllljkkl_s44

how do i fix this
# 2  
Old 03-26-2008
sed is not the right tool. Try awk or perl.

Code:
perl -ple 's/(\d+)$/$1 - 1/ge'

# 3  
Old 03-26-2008
I'm new to this and would like to know what this is doing
# 4  
Old 03-26-2008
s/// is the substitution command you are familiar with from sed.

The regular expression \d+ is just a shorthand for [0-9]+ and putting it in parentheses "captures" it so you can refer back to it.

In the substitution part, we actually put a little Perl script, which subtracts one from the value we captured earlier (now referred to as $1).

The /g option says do this globally (really not necessary here).

The /e option is special to Perl, and says execute the substitution part, rather than just treat it as text.

Hope this helps.
# 5  
Old 03-26-2008
yes thank you very much
this worked perfect
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficiently altering and merging files in perl

I have two files fileA HEADER LINE A CommentLine A Content A .... .... .... TAILER AfileB HEADER LINE B CommentLine B Content B .... .... .... TAILER BI want to merge these two files as HEADER LINE A CommentLine A Content A (4 Replies)
Discussion started by: sam05121988
4 Replies

2. Programming

Altering a jar file

I have a script I am trying to test and run but it runs against a jar file. I wrote an external property file so it would redirect with my script, but it keeps going in search of the previous property file. Is there any way to externally over write the jar file and if not how do you go about... (7 Replies)
Discussion started by: risarose87
7 Replies

3. Shell Programming and Scripting

Altering a variable

Can I take an argument input, lets say it's, hg0000_xy1_v2, in the script it becomes f ... then hack off the end of the filename to change the variable to hg0000 only. I tried using sed but can't figure it out. f="$f" | sed 's/_fg_v//' I could change the variable label if necessary to... (4 Replies)
Discussion started by: scribling
4 Replies

4. Shell Programming and Scripting

Multiply numbers from different files

Hi All, I have tried few things with this but it did not help much. I have some 200,000 files in a directory. There are two sets of files. 1. Files with extension .dat with file names like these (1.dat, 2.dat, 5.dat, 8.dat....200000.dat) 2. Another set of files with .txt extension and... (5 Replies)
Discussion started by: shoaibjameel123
5 Replies

5. Shell Programming and Scripting

Sum Numbers from different files

Hi All, I need to print the sum of numbers from different files. Input files: file1.out 10 20 30 file2.out 10 20 30 (5 Replies)
Discussion started by: saint2006
5 Replies

6. Shell Programming and Scripting

Scripting question: Altering 2 field.

Hi Experts, I want to alter two filed of my data file: The _new should come to 2nd column, and _new to be removed from 4rth column, please advise, datafile.txt aa /dev/vgAA/lvol1 bb /dev/vgAA_new/lvol1 aa /dev/vgAA1/lvol2 bb /dev/vgAA1_new/lvol2 aa /dev/vgAC/lvol1 bb... (5 Replies)
Discussion started by: rveri
5 Replies

7. SuSE

Tape Loader Device /dev/sgN is altering after each reboot

Dear all, we are running SLES 11 where an iscsi tape library is attached. To load the tape in a slot we are using for instance the command #mtx -f /dev/sg1 load 2 which is doing very well. But after a server's reboot the device name of the loader is now /dev/sg4 or /dev/sg5 I mean it is... (1 Reply)
Discussion started by: xunil321
1 Replies

8. UNIX for Dummies Questions & Answers

To get unique numbers from two files

here i have two files: file 1 1 2 3 4 5 5 6 7 8 9 file 2 4 5 6 6 8 8 (6 Replies)
Discussion started by: i.scientist
6 Replies

9. Shell Programming and Scripting

Issue altering end data

I have an inventory program that I would like to have the ability to go and change or alter the field data based on the item number as a key. I have the menu option set but at the end of the script process it just appends the changed data to the database rather than what I would like; which is to... (5 Replies)
Discussion started by: stlitguru
5 Replies

10. UNIX for Dummies Questions & Answers

Get un common numbers from two files

Hi, I have two files: abc : 50040 123123 31703 cde: 104 97 50040 123123 31703 36609 50534 (3 Replies)
Discussion started by: jingi1234
3 Replies
Login or Register to Ask a Question