How to increment number in a file
Asked Answered
K

6

15

I have one file with the date like below,let say file name is file1.txt:

2013-12-29,1

Here I have to increment the number by 1, so it should be 1+1=2 like..

2013-12-29,2 

I tried to use 'sed' to replace and must be with variables only.

oldnum=`cut -d ',' -f2 file1.txt`  
newnum=`expr $oldnum + 1`
sed -i 's\$oldnum\$newnum\g' file1.txt  

But I get an error from sed syntax, is there any way for this. Thanks in advance.

Koeninger answered 30/12, 2013 at 8:49 Comment(0)
C
24

Sed needs forward slashes, not back slashes. There are multiple interesting issues with your use of '\'s actually, but the quick fix should be (use double quotes too, as you see below):

oldnum=`cut -d ',' -f2 file1.txt`  
newnum=`expr $oldnum + 1`
sed -i "s/$oldnum\$/$newnum/g" file1.txt 

However, I question whether sed is really the right tool for the job in this case. A more complete single tool ranging from awk to perl to python might work better in the long run.

Note that I used a $ end-of-line match to ensure you didn't replace 2012 with 2022, which I don't think you wanted.

Camelopardalis answered 30/12, 2013 at 8:56 Comment(3)
Thanks for your quick reply..:) I don't need awk here, as this is a small change. It is working well.Koeninger
No problem; don't forget to pick your favorite reply and accept it by clicking the checkbox.Camelopardalis
Sed does not need forward slashes, it needs a consistent separator. Backslashes just need escaping (doubling) in order to actually reach sed.Nalchik
P
8

usually I would like to use awk to do jobs like this following is the code might work

awk -F',' '{printf("%s\t%d\n",$1,$2+1)}' file1.txt
Phira answered 30/12, 2013 at 9:16 Comment(1)
@Jotne this gave me 1,1 for a file with the value '1' in it. I was not able to increment the number.Inweave
O
3

Here is how to do it with awk

awk -F, '{$2=$2+1}1' OFS=, file1.txt
2013-12-29,2

or more simply (this will file if value is -1)

awk -F, '$2=$2+1' OFS=, file1.txt

To make a change to the change to the file, save it somewhere else (tmp in the example below) and then move it back to the original name:

awk -F, '{$2=$2+1}1' OFS=, file1.txt >tmp && mv tmp file1.txt

Or using GNU awk, you can do this to skip temp file:

awk -i include -F, '{$2=$2+1}1' OFS=, file1.txt
Outfight answered 30/12, 2013 at 9:31 Comment(6)
@Outfight - fails on ...,-1; better to keep the braces, and probably also to make output explicit: awk -F, '{$2=$2+1;print}' OFS=, ...Mahican
@HenkLangeveld Braces should be used yes, since 0 result would not be printed. But as far as I know {$2=$2+1;print} would give the same as {$2=$2+1}1'.Outfight
@Outfight - correct, but the lone 1 me be overlooked (as a typo) to the uninitiated. Better to add a note explaining it in the answer.Mahican
This worked for me, although I had to add a double & between the commands. awk -F, '{printf("%d\n",$1+1)}' nb.txt > nb2.txt && mv nb2.txt nb.txt (note that the file just has a number)Markley
it has to be && and not & otherwise awk is in background and mv could be done when the file is not done.Unwonted
@Unwonted You are correct, fixed. Thanks In Gnu awk you can do awk -i include -F, '{$2=$2+1}1' OFS=, file1.txt to write without temp file.Outfight
D
3

Another one-liner would be:

$ expr `cat /tmp/file 2>/dev/null` + 1 >/tmp/file

this works if the file doesn't exist or if the file doesn't contain a valid number - in both cases the file is (re)created with a value of 1.

Duyne answered 5/5, 2018 at 15:2 Comment(0)
A
2

Bash one liner option with BC. Sample:

$ echo 3 > test
$ echo 1 + $(<test) | bc > test
$ cat test
4

Also works:

bc <<< "1 + $(<test)" > test
Alviani answered 1/7, 2021 at 13:34 Comment(4)
I like the solution, but it doesn't seem to work because of a read-and-write at the same time conflict. i.e. "test" ends up empty.Toxemia
What shell or bash version are you on? Works for me in v5.2.15.Alviani
Is adding 1 via bc faster than adding 1 via Bash?Glaciology
Probably not but bc does present a lot more options if needed.Alviani
P
0

awk is the best for your problem, but you can also do the calculation in shell

In case you have more than one rows, I am using loop here

#!/bin/bash
IFS=,
while read DATE NUM
do
echo $DATE,$((NUM+1))
done < file1.txt
Photoengraving answered 30/12, 2013 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.