Change numbers in a file, incrementing them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change numbers in a file, incrementing them
# 1  
Old 05-16-2012
Change numbers in a file, incrementing them

Hello,

Here's a file of mine:

Code:
key1:431
key2:159
key3:998

I need to change these keys to something bigger - and I actually need to shift them all by a range of 3.

The output would be:

Code:
key1:434
key2:162
key3:1001

I can't find the propper sed/awk line that would alter all my lines.

The plan-B would be to parse each line, get the value, increment it, and replace it. But I'm sure there's something smarter. .

Thanks

Moderator's Comments:
Mod Comment edit by bakunin: Please use [CODE]...[/CODE]-tags when posting code or terminal output. Thank you.

Last edited by bakunin; 05-16-2012 at 12:36 PM..
# 2  
Old 05-16-2012
Quote:
Originally Posted by fzd
The plan-B would be to parse each line, get the value, increment it, and replace it. But I'm sure there's something smarter. .
Actually there isn't and its almost a one-liner (at least for a file as simple as you have presented it).

I hate to admit it but arithmetics in sed is beyond its capabilities. I already tried that for fun but gave up.

bakunin
# 3  
Old 05-16-2012
if all you have is key:val then awk is good at math:

Code:
$ printf '%s\n' key1:431 key2:159 key3:998 | awk -F: '{print $1 FS 3+$2}'
key1:434
key2:162
key3:1001

# 4  
Old 05-16-2012
My file is a bit tricker than that (most of the lines don't have the key:value thing, and the "key:value" thing is more like "id1-id2-key_value_comment-id3_id3" (- and _ are "separators", but I suppose -F with awk will do what I want)

Example of the file:


Code:
name-firstname
number-street-zip_city
phone_email
eggs_value1_discounts-Y/N
tomatoes_value2_discounts-Y/N
bread_value3_discounts-Y/N
wholebread_value4_discounts-Y/N

etc.

We need to increase all the bread things when someone buys some bread.

Moderator's Comments:
Mod Comment edit by bakunin: Please use [CODE]...[/CODE]-tags when posting code or terminal output. Thank you.

Last edited by bakunin; 05-16-2012 at 12:37 PM..
# 5  
Old 05-16-2012
You can use [-_] as field sep. And possibly filter those lines with NF==7. Let us know if you need more help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Advanced & Expert Users

Add an incrementing identity column to a file

Two questions: 1) Is there a way to create a new column (i.e. ID) to an existing file containing data and the new column ID populate as an auto incrementing number? for example: (Current file has headers and is PIPE delimited.) **data looks like this** "COL1"|"COL2"|"COL3"|"COL4"|"COL5"... (4 Replies)
Discussion started by: timlisa20
4 Replies

3. Shell Programming and Scripting

Hint needed for incrementing numbers

Hi All Been trying to get something working but having some trouble in unix bash or ksh scripting. Im trying to increment once a condition has been met Say I have a file that contains: apple orange banana grapes dates kiwi What im after is once a counter has reached every second... (3 Replies)
Discussion started by: chandika_diran
3 Replies

4. Shell Programming and Scripting

Change numbers

Hallo This is the content of the file 3 4 5 6 7 8 9 10 11 12 And I want the following output 1 2 3 4 5 6 7 (4 Replies)
Discussion started by: thailand
4 Replies

5. Shell Programming and Scripting

Change the numbers

Hi friends, i need a command which can be used to change the values in file. I have file which contain some values. Data_Center_costing=XXXX Financial_loss=XXXX operational_cost=XXX I am not aware about the values of XXXX, They may be 4 digit or less/more than that. but i need these... (12 Replies)
Discussion started by: Nakul_sh
12 Replies

6. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

7. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

8. Shell Programming and Scripting

Change numbers in flat file

Hi, I have some datatexts with values and I want to replace only the values of the first file with the result of array loaded in one variable. Also I want the operation difference with the original value replaced and only replace the new value not the character format of the file. Some Idea?... (24 Replies)
Discussion started by: faka
24 Replies

9. 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

10. Shell Programming and Scripting

Port incrementing using one file

Hi I wrote this script with the help of you guyz.My next challenge is to increment the port using single file.So far iam using this code to increment hp1 and hp2 .To increment port numbers, iam using two different files (.default_port_hp1 and .default_port_hp2).The challenge for me is to use... (0 Replies)
Discussion started by: coolkid
0 Replies
Login or Register to Ask a Question