Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    17

    Question 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!!

  2. #2
    scm
    scm is offline
    Linux 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:
    Code:
    #!/bin/bash
    while read line
    do
        # do stuff with the variable "line"
        echo "$line"
        echo "$line" | awk '{print "line contains " NF " words"}'
    done
    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.

  3. #3
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by techmad View Post
    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!!
    you can process the lines as you go over them, no need to set variables (unless you want to use them later in your script, you can use arrays). Tell us what you are actually wanting to do with your file.

  4. #4
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by scm View Post
    A
    Code:
    #!/bin/bash
    while read line
    do
        # do stuff with the variable "line"
        echo "$line"
        echo "$line" | awk '{print "line contains " NF " words"}'
    done
    another way, without using awk
    Code:
    # IFS=" "
    # while read line; do set -- $line; echo ${#}; done < file

  5. #5
    Just 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?

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    # echo `tr  "\n" " " < file | sed 's/ /+/g'` | bc

  7. #7
    Just 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!

  8. #8
    Linux 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

  9. #9
    Just 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

  10. #10
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk '{s+=$0}END{print s}' file

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...