-1

I have a text file where I am keeping versions:

1.0
2.0
3.0
4.0
5.0

and so on.

I need a way to give, for example, 4.0 as an input and get3.0 as the output. Note that there would be thousands of versions.

I mean I want to give a version number and get the number of the previous version. So if I give 9.0, I would get 8.0.

I tried

  sed '1!G;h;$!d' test.txt 

But this is printing all the numbers.

terdon
  • 242,166
  • is there always will be strict order of lines/numbers? Do you just need to get the previous line? – RomanPerekhrest Nov 24 '17 at 14:58
  • What @RomanPerekhrest said. Also, can you have things like 4.2.1? Will the version numbers always be foo dot bar (e.g. 2.3) or can they be more complex? Will the file always be sorted? – terdon Nov 24 '17 at 15:04
  • not previous line.. i want n-1 number.. example.. if i enter 10.0 then output will 9.0.. sorting also i need to maintain. – user2995458 Nov 24 '17 at 15:06
  • @user2995458 OK, then please [edit] your question and clarify. But we also need to know if you want the previous version or just the -1. For example, if you have versions 1.0, 1.2, 2.0 and give 2.0 as input, do you want 1.0 or 1.2 as output? And what if you have 1.0.1? – terdon Nov 24 '17 at 15:07
  • 3
    if you just want to subtract 1 from input numeric string, then, what's the point of using a file at all? Untill you elaborated, using a file is pointless – RomanPerekhrest Nov 24 '17 at 15:08
  • @RomanPerekhrest--yes my version will be like 4.0 and i want 3.0... there are no complexity in major.minor versions. actually i am maintaing versions in file. – user2995458 Nov 24 '17 at 15:13
  • @RomanPerekhrest Regarding questions like this, is there a consensus on this site (or on StackExchange in general) about whether or not questions should be answered as asked? I share your view that it seems unclear what the real purpose of this question is, but it isn't unintelligible. – igal Nov 24 '17 at 15:14
  • @igal, no one told that it's unintelligible. I'm calling this pointless, cause you don't need a file for just inputting 4.0 to get 3.0 – RomanPerekhrest Nov 24 '17 at 15:17
  • @don_crissti, closing this is more intelligible. Done the same – RomanPerekhrest Nov 24 '17 at 15:23
  • @RomanPerekhrest I didn't say that anyone told me that it's unintelligible. I asked whether or not there's a consensus about how to respond to "pointless" questions. – igal Nov 24 '17 at 15:30
  • @igal, imho, I wouldn't recommend to answer pointless questions – RomanPerekhrest Nov 24 '17 at 15:30
  • See also: https://unix.stackexchange.com/q/40786/117549 – Jeff Schaller Nov 24 '17 at 16:19
  • @igal - not only is this question poorly written, it is definitely unclear: there is no mention that "they wanted to print the new list obtained by subtracting 1 from each number" as you say... That's your interpretation. If this question is about subtracting 1 from an integer then it should be closed as a duplicate so either way, it should be closed. – don_crissti Nov 24 '17 at 17:51
  • 1
    I agree with don_crissti: I read the question as “Given a file *F* and a number *N,* I want to compute *N-1.”  I don’t see anything to support @igal’s interpretation that the OP wants to create file F₋₁.* – G-Man Says 'Reinstate Monica' Nov 24 '17 at 22:09
  • @G-Man The example command was the clue. They said that they have their data in a text file. They showed a list of examples to illustrate the contents of the file. And then they gave a single sed command taking that file as input - that was their solution attempt. – igal Nov 24 '17 at 23:13
  • @G-Man I feel like I'm getting sort of pushed into a position where it looks like I'm endorsing the way this question was written so, just to be clear, I think this question was written poorly. All I'm saying is that (apparently) I was able to figure out what they wanted without too much effort and also that it wouldn't take very much work to improve the statement of the question. – igal Nov 24 '17 at 23:14

1 Answers1

0

You could do this with bc (see the GNU bc manual). You can pass bc an arithmetical expression and bc will evaluate it and return the result, e.g.

echo '9.0 - 1' | bc -l

This command returns 8.0 as its result.

Now suppose your version number is contained in the file version.txt, e.g.:

echo '9.0' > version.txt

Then you could run the following command:

echo "$(cat version.txt) - 1" | bc -l

This would produce 8.0 as its output. Alternatively, suppose that you have a list of version numbers in a file called versions.txt e.g.:

1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0

We could create such a file like so:

for i in {1..10}; do echo "${i}.0" >> versions.txt; done

In this case we could use bc inside of a while-loop:

while read line; do echo "${line}-1" | bc -l; done < versions.txt

This produces the following output:

0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0

Alternatively, you could extract the integer part from the version number and use bash to perform the arithmetic. You could use any number of text-processing tools to do this (e.g. grep, sed, awk, etc.). Here is an example using the cut command:

echo "$(( $(echo '9.0' | cut -d. -f1) - 1)).0"

This produces 8.0 as its output. Putting this into a while-loop gives us the following:

while read line; do \
    echo "$(( $(echo ${line} | cut -d. -f1) - 1)).0";
done < versions.txt
igal
  • 9,886
  • hi Igal, in my system bc: command not found. do i have any alternate way? – user2995458 Nov 24 '17 at 15:04
  • @user2995458 please edit your question and tell us what operating system you use. Also clarify if your versions will always be this simple or if you can have sub versions (4.2.3 etc). – terdon Nov 24 '17 at 15:05
  • @user2995458 I've updated my solution to use a different command. But if you're frequently going to be doing arithmetic at the command-line then it might be worth installing bc anyway. – igal Nov 24 '17 at 15:15