Operating System - HP-UX
1823198 Members
3883 Online
109648 Solutions
New Discussion юеВ

Re: Reading lines in a loop

 
SOLVED
Go to solution
Sachin Patel
Honored Contributor

Reading lines in a loop

HI

I am trying to read line by line in for loop

Here is my section of netgroup file which has \ at the end

B132 (alderon.vdgc.com,-,) (alderon,-,) (babafett.vdgc.com,-,) (babafett,-,) (c3po.vdgc.com,-,) (c3po,-,) (chewie.vdgc.com,-,) (chewie,-,) (droid.vdgc.com,-,) (droid,-,) (dthstar1.vdgc.com,-,) (dthstar1,-,) (endor.vdgc.com,-,) (endor,-,)

while read line
do
echo "$line"
done < file_name

It will gives me all line without \ at the end.

How can I solve this?

Sachin
Is photography a hobby or another way to spend $
10 REPLIES 10
Rodney Hills
Honored Contributor

Re: Reading lines in a loop

Since \ is used for special characters within a shell command line, I would say either strip the \ or add another to the end of the line.
\\ will return a single \.

-- Rod Hills
There be dragons...
S.K. Chan
Honored Contributor

Re: Reading lines in a loop

Not answering your question but just wanted to quickly point out (you may know this already) that you can make the "netgroup" file looks nicer. For example ..
B132(alderon.vdgc.com,-,) (alderon,-,)(babafett.vdgc.com,-,) (babafett,-,).......
endor.vdgc.com,-,) (endor,-,)

B2000(venus.vdgc.com,-,) (venus,-,)(mars.vdgc.com,-,) (mars,-,).......
(rock.vdgc.com,-,) (rock,-,)

That way is more readable (that's how I got mine done).
Sachin Patel
Honored Contributor

Re: Reading lines in a loop

Woo, Even cut paste didn't work either.

It suppose to be
B132 (alderon.vdgc.com,-,) (alderon,-,) (babafett.vdgc.com,-,) (babafett,-,) (c3po.vdgc.com,-,) (c3po,-,) droid.vdgc.com,-,) (droid,-,)
And so on...

Rodney, I can't use \\ on netgroup file. I do not want to change original file to make my script work.

Sachin
Is photography a hobby or another way to spend $
S.K. Chan
Honored Contributor

Re: Reading lines in a loop

Sorry Sachin, the output is meant to look like ..
B132
(alderon.vdgc.com,-,) (alderon,-,)
(babafett.vdgc.com,-,) (babafett,-,)
.......
endor.vdgc.com,-,) (endor,-,)

B2000
(venus.vdgc.com,-,) (venus,-,)
(mars.vdgc.com,-,) (mars,-,)
.......
(rock.vdgc.com,-,) (rock,-,)
Rodney Hills
Honored Contributor
Solution

Re: Reading lines in a loop

According to the "ksh" man page, you can use-

read -r line

The -r indicates raw and it will leave "\" characters alone.

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Reading lines in a loop

PERL time

#!/path2perl
open(INFILE, "while () {
print $_;
}

live free or die
harry
Live Free or Die
Sachin Patel
Honored Contributor

Re: Reading lines in a loop

Look like "\" is not working in this message area either

It suppose to be
B132 "\"
(alderon.vdgc.com,-,) (alderon,-,) "\"
(babafett.vdgc.com,-,) (babafett,-,) "\"
(c3po.vdgc.com,-,) (c3po,-,) "\"
droid.vdgc.com,-,) (droid,-,) "\"

And so on...
So third try with double quote around my \\.

Sachin
Is photography a hobby or another way to spend $
Sachin Patel
Honored Contributor

Re: Reading lines in a loop

Thanks Rodney,
That works.
SK I have my netgroup file same way as you trying to describe. Harry no perl this time.

Sachin
Is photography a hobby or another way to spend $
James R. Ferguson
Acclaimed Contributor

Re: Reading lines in a loop

Hi Sachin:

Have a look at the man pages for 'read'. The '-r' option causes the backslach "\" character not to be treated in any special
way. That is, to consider each backslash to be part of the input line.

Regards!

...JRF...
Hai Nguyen_1
Honored Contributor

Re: Reading lines in a loop

Sachin,

Can you try this? It should work, assuming that the "\" is at the end of the line if any. Otherwise, you can try again without the "$" sign in the sed command.

cat file_name | sed 's:\\$:\\\\:' | while read line
do
echo "$line"
done

Hai