Find the answer to your Linux question:
Results 1 to 7 of 7
Sorry, it's been a while since I've scripted so I have a couple questions. Hope someone has some insight. Question 1) Say I wanna write a script that counts the ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2

    A few basic bash script questions

    Sorry, it's been a while since I've scripted so I have a couple questions. Hope someone has some insight.

    Question 1)

    Say I wanna write a script that counts the number of lines in a file and assigns it to a variable. From there, I can do whatever I want to the variable.

    The command line call is easy enough: wc -l blah.txt | awk '{print $1}'

    Though, I have a script, temp.sh, and all it has in it is this:

    nrows="wc -l $1 | awk '{print $1}' "
    echo $nrows

    I tried printing/echo'ing nrows to the stdout, but apparently I just assigned it a bunch of characters instead of a numerical value. i.e. when temp.sh is called, this is displayed to the stdout instead of a number:

    wc -l blah.txt | awk '{print blah.txt}' but I need nrows to have a numerical value.

    Thoughts?


    Question 2) Say I have a file that has, for the sake of argument, 10 columns and multiple rows, how would I go about traversing the file and removing any rows where the 8th column is a '0'?

    The file is a list of file attributes, but I simply want to remove all rows that have a file size of 0.

    Question 3) I'm also have a bit of trouble reading the file when the filename is provided as an argument. I've done a bunch of searching and it seems that the most popular way to read a script is to pipe it into the script. i.e.

    $ ./temp.sh < listing.txt

    Is there a simple way to loop through the file where the file is provided as an argument vs piping it in?

    Sorry for the long-winded questions!

    Thanks in advance!

  2. #2
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2
    Ooops. Figured out the answer to question 1.

    nrows=$(wc -l $1 | awk '{print $1}')

  3. #3
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    You could also use:

    Code:
    nrows=`wc -l $1 | awk '{print $1}'`
    For 3 you can use the code:

    Code:
    while read line
    do
        echo $line
    done < "listing.txt"
    Linux User #453176

  4. #4
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    506
    Quote Originally Posted by the_fornicator View Post
    Question 2) Say I have a file that has, for the sake of argument, 10 columns and multiple rows, how would I go about traversing the file and removing any rows where the 8th column is a '0'?

    The file is a list of file attributes, but I simply want to remove all rows that have a file size of 0.
    If it's the output of ls -l, this might work, although the columns change a little depending on the content. It tests for any 24 characters, 1 or more spaces, a single 0 and a single space. It deletes lines that match.
    Code:
    sed '/^.\{24\} \+0 /d' infile.txt > outfile.txt
    Quote Originally Posted by the_fornicator View Post
    Question 3) I'm also have a bit of trouble reading the file when the filename is provided as an argument. I've done a bunch of searching and it seems that the most popular way to read a script is to pipe it into the script. i.e.

    $ ./temp.sh < listing.txt

    Is there a simple way to loop through the file where the file is provided as an argument vs piping it in?
    This is a simple example that doesn't have any tests for errors, but it works:
    Code:
    #!/bin/sh
    while read line; do
        echo $line
    done < "$1"

  5. #5
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by the_fornicator View Post
    Ooops. Figured out the answer to question 1.

    nrows=$(wc -l $1 | awk '{print $1}')

    That will fail if the filename contains spaces.

    AWK is unnecessary:

    Code:
    nrows=$(wc -l < "$1")

  6. #6
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by KenJackson View Post
    This is a simple example that doesn't have any tests for errors, but it works:
    Code:
    #!/bin/sh
    while read line; do
        echo $line
    done < "$1"

    It may work for some limited definition of "work" or with some files.

    It will strip leading and trailing spaces, it will swallow backslashes, and it will expand any wildcards contained in $line; the echo command will reduce multiple spaces to a single space.


    Code:
    while IFS= read -r line; do
        echo "$line"
    done < "$1"

  7. #7
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Question #2 is easy with awk:
    Code:
    awk '$8!=0 {print $0}' 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
  •  
...