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 ...
- 08-26-2009 #1Just 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!
- 08-26-2009 #2Just Joined!
- Join Date
- Aug 2009
- Posts
- 2
Ooops. Figured out the answer to question 1.
nrows=$(wc -l $1 | awk '{print $1}')
- 08-26-2009 #3
You could also use:
For 3 you can use the code:Code:nrows=`wc -l $1 | awk '{print $1}'`
Code:while read line do echo $line done < "listing.txt"Linux User #453176
- 08-27-2009 #4
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.
This is a simple example that doesn't have any tests for errors, but it works:Code:sed '/^.\{24\} \+0 /d' infile.txt > outfile.txtCode:#!/bin/sh while read line; do echo $line done < "$1"
- 08-29-2009 #5
- 08-29-2009 #6
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"
- 08-29-2009 #7Linux 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


Reply With Quote
