LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   subtraction a list of number (https://www.linuxquestions.org/questions/linux-newbie-8/subtraction-a-list-of-number-934928/)

mmhs 03-17-2012 06:56 AM

subtraction a list of number
 
hi guys

i have a file which list some numbers

this file is like

Quote:

2
3
4
5
7
123
43
45
656
435
345
123
5235
.
.
.
.
.
i want to subtract all numbers with 1

i wrote a for loop

Quote:

for i in `cat number.txt`; do $i=$(($i-1));done >> sub.txt
but i gave an error :(

how can i solve this problem

druuna 03-17-2012 07:07 AM

Hi,

Try this:
Code:

for i in $(cat number.txt ); do let i=i-1 ; echo $i ; done
This might clear things up: Bash - ArithmeticExpression

Hope this helps.

mmhs 03-17-2012 07:26 AM

Quote:

Originally Posted by druuna (Post 4629079)
Hi,

Try this:
Code:

for i in $(cat number.txt ); do let i=i-1 ; echo $i ; done
This might clear things up: Bash - ArithmeticExpression

Hope this helps.

thx man for help but it gives ")syntax error: operand expected (error token is "

druuna 03-17-2012 07:29 AM

Hi,
Quote:

Originally Posted by mmhs (Post 4629084)
thx man for help but it gives ")syntax error: operand expected (error token is "

Not on my side:
Code:

$ cat number.txt
2
3
4
5
7
123
43
45
656
435
345
123
5235
$ for i in $(cat number.txt ); do let i=i-1 ; echo $i ; done
1
2
3
4
6
122
42
44
655
434
344
122
5234

Is the input file a normal text file? (file number.txt to check).

druuna 03-17-2012 07:51 AM

Solved?

What did you do to accomplish that (might be of interest for people that stumble upon this thread).

grail 03-17-2012 10:41 AM

Just as slight alternative:
Code:

for i in $(< number.txt);do echo $((--i));done

David the H. 03-17-2012 02:11 PM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


Don't Read Lines With For. You should almost always use a while+read loop instead when working with files or the output of commands.


One possibility it's failing could be line endings. If the file was created on a Windows machine, it will have dos-style cr+lf line endings, instead of unix-style lf-only line endings, and the extra carriage return could be messing it up. This is particularly true in an arithmetic context, since anything not an integer would break syntax.

The output of file will tell you if it has crlf line endings. If so, there are many options for fixing them, such as tofrodos. Google or LQ search it.

mmhs 03-17-2012 02:33 PM

Quote:

Originally Posted by druuna (Post 4629100)
Solved?

What did you do to accomplish that (might be of interest for people that stumble upon this thread).

your script worked perfect however it gave error !!!!

grail 03-17-2012 11:53 PM

Let me just look up my crystal ball to help solve that error???


All times are GMT -5. The time now is 05:08 AM.