Results 1 to 10 of 10
Hi,
This is my first post here so hello everyone! Im a Linux newb and Im trying to create a script that will take the first line of a .txt ...
- 09-18-2008 #1Just Joined!
- Join Date
- Sep 2008
- Posts
- 17
Bash Beginner!
Hi,
This is my first post here so hello everyone!
Im a Linux newb and Im trying to create a script that will take the first line of a .txt file and set it as a variable, then move to the second line and set it as a variable, then move to the third line etc.. Ive been playing with grep and head but I cant seem to get it. Any help is greatly appreciated!!
- 09-18-2008 #2Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Are you proposing to have as many variables as lines in the file? That's not really what the shell was designed to do. Maybe if you outlined what you plan to do with each line we could be more helpful. For example, the following loop will do "stuff" with each line in turn:
I've quoted the echo-ed variable to preserve whitespace - echo outputs each of its arguments with a single space between them so quoting the variable makes echo see the entire contents as a single argument.Code:#!/bin/bash while read line do # do stuff with the variable "line" echo "$line" echo "$line" | awk '{print "line contains " NF " words"}' done
- 09-19-2008 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 09-19-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 09-19-2008 #5Just Joined!
- Join Date
- Sep 2008
- Posts
- 17
Thanks for the replies, what I have is a text file with a number on each line (just a number nothing else), I need to add the numbers together and get the total. So I was going to try to set each number as a variable and then add them together, or is there a better way to do it?
- 09-19-2008 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458
Code:# echo `tr "\n" " " < file | sed 's/ /+/g'` | bc
- 09-22-2008 #7Just Joined!
- Join Date
- Sep 2008
- Posts
- 17
Thanks ghostdog, when I try to run the command I get the following error:
sudo echo `tr "\n" " " < numbers.txt | sed 's/ /+/g'` | bc
(standard_in) 2: parse error
Any idea what the problem is? I also tried the following but got another error:
sudo echo `tr "\n" " " > numbers.txt | sed 's/ /+/g'` | bc
bash: numbers.txt: Permission denied
If I switch to root and run it it doesnt give me an error, but it doesnt seem to do anything, I have to CTRL+C to cancel it
**Actually when I checked the numbers.txt file after running the second command as root it had deleted the numbers from the file!
- 09-22-2008 #8Linux User
- Join Date
- Aug 2006
- Posts
- 458
it works fine for me.
Code:# more file 1 2 3 4 5 6 7 8 9 10 # echo `tr "\n" " " < file | sed 's/ /+/g'` | bc 55
- 09-22-2008 #9Just Joined!
- Join Date
- Sep 2008
- Posts
- 17
I couldn't seem to get it to work for me, thanks for your help though. My manager just put this together for me which does exactly what I need:
Code:#!/bin/sh SUM=0 for DATA in `cat numbers.txt` do SUM=`expr $DATA + $SUM` done echo $SUM
- 09-22-2008 #10Linux User
- Join Date
- Aug 2006
- Posts
- 458
Code:awk '{s+=$0}END{print s}' file


Reply With Quote
