Increment Numbers in File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Increment Numbers in File
# 1  
Old 11-02-2012
Increment Numbers in File

Hello,

I have a text file withe some records

Code:
20121031|5
20121030|3
20121029|1
20121028|4
20121027|6

I want to search for a patten with '20121030' and then increment the second part of the delimiter i.e. 3 by 1 to make it 4 to look like

Code:
20121031|5
20121030|4
20121029|1
20121028|4
20121027|6

thanks for the help.
# 2  
Old 11-02-2012
Code:
awk -F"|" ' { OFS="|" } /20121030/ { print $1,$2+1; } ' file

# 3  
Old 11-02-2012
Thanks bipinajith. Works perfectly. Smilie
# 4  
Old 11-02-2012
Quote:
Originally Posted by bipinajith
Code:
awk -F"|" ' { OFS="|" } /20121030/ { print $1,$2+1; } ' file

@bipinajith - It will print only lines having 20121030 in it...

Try

Code:
awk '{FS=OFS="|" }/20121030/{$2+=1}1' file

# 5  
Old 11-02-2012
Quote:
Originally Posted by pamu
@bipinajith - It will print only lines having 20121030 in it...
Oh right, I misread requester's requirement.Smilie Thanks for correcting.Smilie
# 6  
Old 11-02-2012
Thanks bipinajith and pamu Smilie
# 7  
Old 11-02-2012
Code:
perl -F'/[|]/' -alpe 'if($F[0] eq "20121030"){$F[1]+=1;print join "|",@F;next}' file


Last edited by elixir_sinari; 11-02-2012 at 05:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Shell Programming and Scripting

Increment value in text file

Hi Guys, I am new to shell programing, I have a csv file which has 50k records and I have got the requirement to increment the value in second column after each 5000 records. for example below A,B,C,D //Header 1,1,London,UK 1,1,Manchester,UK 1,1,Glasgow,UK . . . 1,1,Newyork,USA... (7 Replies)
Discussion started by: rizzu1555
7 Replies

4. Shell Programming and Scripting

To increment the values from the file

Hi I have the file called "file.txt" which contains the following output $cat file.txt sandy <version>1</version> karen <version>2</version> Rob <version>3</version> peter <version>4</version> i want to write a command which will add the value 1 to the digits and show the output... (2 Replies)
Discussion started by: sidh_arth85
2 Replies

5. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

6. Shell Programming and Scripting

Increment a value in a configuration file.

Experts, I would appreciate if someone took the time to express there opinion /approach in creating a new change daily to a configuration file. I create a new log file each day and I wish to have a browser based reader display the new file. To achieve this I would need to create a new... (2 Replies)
Discussion started by: jaysunn
2 Replies

7. UNIX for Dummies Questions & Answers

increment numbers in several parts of a string

I know how to do produce this: string01 string02 string03 several different ways. But how do I do produce this (without getting lost in recursion): string01morestring100yetmore10 string02morestring101yetmore20 string03morestring102yetmore30 ...... (2 Replies)
Discussion started by: uiop44
2 Replies

8. Shell Programming and Scripting

File existence and increment

count=0; while read line; do ] && let count=count+1; done < file_name.txt echo echo "$count of 10 files found " echo The scenario is a follows : I have a file which contains a list of filenames present in particular directory . I am checking fo the existence of the file and... (5 Replies)
Discussion started by: ultimatix
5 Replies

9. Shell Programming and Scripting

Check file and increment

My scripts excepts 4 files ABCD_01 ABCD_02 ABCD_03 ABCD_04 I want to check for these files , and increment counter one by one . at the end i would like to echo as 4 of 4 expected instances of file found . I tried something like thsi $counter =1 if counter=counter+1 i need... (5 Replies)
Discussion started by: ultimatix
5 Replies

10. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies
Login or Register to Ask a Question

Featured Tech Videos