Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bash echo problem...

0 views
Skip to first unread message

T

unread,
Nov 24, 2009, 8:36:51 AM11/24/09
to
Greetings:

I have a file like so:

Altera 1805@system1 (Quartus)
Ansoft 1670@system1 (Signal Integerty Tool)
Cadence 5280@system2 (PCB Board Layout Tools)
# Denali 1783@system2 (Memory Models for Simulation)
...

I have the following bash code:

for line in `cat license.lst | grep -v ^# | awk '$2 ~ /^[0-9]+/ {print
$0}'`
do
echo "${line}"
done

When this runs I get:

Altera
1805@system1
(Quartus)
Ansoft
1670@system1
(Signal
Integerty
Tool)
Cadence
5280@system2
(PCB
...

I have run dos2unix on the "license.lst" file, but still get multiple
lines where I was expecting one. This works on the command line, so I
don't under stand why it's doing this in the script. Any Ideas?

Thanks for any help in Advance!
Tom


Jens Thoms Toerring

unread,
Nov 24, 2009, 9:14:24 AM11/24/09
to

The for splits the lines it gets from awk at white space. I
don't see what it's good for at all, you could instead do

cat license.lst | grep -v ^# | awk '$2 ~ /^[0-9]+/ {print $0}'

or to avoid the useless-use-of-cat award

grep -v ^# license.lst | awk '$2 ~ /^[0-9]+/ {print $0}'

or just

awk '$1 !~ /^#/ && $2 ~ /^[0-9]+/ {print}' license.lst

If you need to do more with the lines than printing them one
way is to set a different separator character during the loop,
e.g.

old_ifs=$IFS
IFS=$'\n'
for line in `awk '$1 !~ /^#/ && $2 ~ /^[0-9]+/ {print}' license.lst`; do
echo $line;
done
IFS=$old_IFS

should do the trick.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de

Michael Tosch

unread,
Nov 30, 2009, 1:57:21 PM11/30/09
to

alt 1)
for line in "`awk '! /^#/ && $2 ~ /^[0-9]+/' license.lst`"
do
echo "$line"
done

alt 2)
awk '! /^#/ && $2 ~ /^[0-9]+/' license.lst |
while IFS="" read -r line
do
echo "$line"
done

alt 3)
while read -r w1 rest
do
case $w1 in
\#*) continue;;
esac
case $rest in
[0-9]*)
echo "$w1 $rest"
;;
esac
done < license.lst

--
echo imhcea\.lophc.tcs.hmo |
sed 's2\(....\)\(.\{5\}\)2\2\122;s1\(.\)\(.\)1\2\11g;1s;\.;::;2'

0 new messages