LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-17-2012, 06:56 AM   #1
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Rep: Reputation: 1
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
 
Old 03-17-2012, 07:07 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
1 members found this post helpful.
Old 03-17-2012, 07:26 AM   #3
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
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 "
 
Old 03-17-2012, 07:29 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,
Quote:
Originally Posted by mmhs View Post
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).
 
1 members found this post helpful.
Old 03-17-2012, 07:51 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Solved?

What did you do to accomplish that (might be of interest for people that stumble upon this thread).
 
Old 03-17-2012, 10:41 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Just as slight alternative:
Code:
for i in $(< number.txt);do echo $((--i));done
 
1 members found this post helpful.
Old 03-17-2012, 02:11 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
1 members found this post helpful.
Old 03-17-2012, 02:33 PM   #8
mmhs
Member
 
Registered: Oct 2010
Posts: 101

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by druuna View Post
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 !!!!
 
Old 03-17-2012, 11:53 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Let me just look up my crystal ball to help solve that error???
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
List directories and number of files inside them. arizonagroovejet Linux - General 4 03-20-2011 10:33 AM
[SOLVED] List number of packages available / installed replica9000 Debian 2 03-17-2011 12:17 PM
counting the number of unique items in a list UnixKiwi Programming 4 11-29-2008 02:49 PM
List total number of directories paraiso Linux - Newbie 5 04-18-2005 04:04 AM
Command to list total number of files. WillieB_72 Linux - General 3 01-29-2003 09:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration