1834137 Members
2388 Online
110064 Solutions
New Discussion

More script help?

 
SOLVED
Go to solution
Greg Stark_1
Frequent Advisor

More script help?

We have a file in which we would like to parse out lines in which the first character is the letter s. I have written the script below which works, but I was wondering if there is a better/shorter way to do this.

cat $file |while read line
do
fstchar=`echo $line |cut -c1-1`
if [[ $fstchar = "s" ]];
then
echo "$line"
fi
done

Also, I noticed that if I didn't enclose the last echo variable in "'s, it would strip all white space. Why?

Thanks again,
Greg
2 REPLIES 2
Rodney Hills
Honored Contributor
Solution

Re: More script help?

grep is ideal for this...

grep "^s" $file >all_lines_with_s

-- Rod Hills
There be dragons...
Sridhar Bhaskarla
Honored Contributor

Re: More script help?

Hi Greg,

grep "^s" your_file

If there are no white spaces before the line this will not work. It will be complicated with sed's or the way you are doing.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try