From: Cat 22 on
In a bash script I have a variable called recs
recs="this is line 1\r\nThis is line 2\r\nthis is line3\r\n"

Actually recs gets loaded via ssh like this but the sample i am
showing is correct:
recs=$(ssh other_sys cat xp_share/records.txt)
and recs ends up containing a lot of lines delimited this way

The \r\n is an 0D 0A sequence (file produced on winders)

How can i echo out the individual lines in a loop?
I can use tr to change the 0d 0a to \r but it still
prints out in 1 long line
e.g
ct=0
while read line
do
echo "$ct $line"
((ct++))
done < <(echo $recs)

This only prints out 1 long line like this
1 this is line 1^MThis is line 2^Mthis is line3^M

What i am after is this:
1 this is line 1
2 this is line 2
3 this is line 3

I tried a number of experiments but cant get it right.
Thanks
Cat22

From: Janis Papanagnou on
Cat 22 wrote:
> In a bash script I have a variable called recs
> recs="this is line 1\r\nThis is line 2\r\nthis is line3\r\n"

In your string a sequence of \r\n is four characters, \ r \ and n.
Below I see ^M (ctrl-M) and you're speaking of OD OA sequences, so I
assume you rather have the equivalent of

recs=$'this is line 1\r\nThis is line 2\r\nthis is line3\r\n'

>
> Actually recs gets loaded via ssh like this but the sample i am
> showing is correct:
> recs=$(ssh other_sys cat xp_share/records.txt)
> and recs ends up containing a lot of lines delimited this way
>
> The \r\n is an 0D 0A sequence (file produced on winders)
>
> How can i echo out the individual lines in a loop?

You need no loop if you just write

printf "%s\n" "$recs"

Printf does the loop for you.

> I can use tr to change the 0d 0a to \r but it still

Are you sure you mean \r (i.e. 0D, CR) and not \n (0A, LF)?

To remove characters (like \r) you can either use tr -d

tr -d '\r' <<< "$recs"

(or use '\015' instead of '\r' in case it does not work for
you on your system, or $'\r' ), or alternatively let the shell
do the replacement, try

printf "%s\n" "${recs//$'\r'}"

> prints out in 1 long line

What do you want to achieve here? If you replace some control
characters and keep the others those will still be active. On
Unix systems a \r will likely overwrite the characters from
the previous lines. If you want to see the whole line you will
have to replace \r as well to something visible or remove them
as well. Or, as said above, remove the '\r' instead of changing
\r\n to \r.

Janis

> e.g
> ct=0
> while read line
> do
> echo "$ct $line"
> ((ct++))
> done < <(echo $recs)
>
> This only prints out 1 long line like this
> 1 this is line 1^MThis is line 2^Mthis is line3^M
>
> What i am after is this:
> 1 this is line 1
> 2 this is line 2
> 3 this is line 3
>
> I tried a number of experiments but cant get it right.
> Thanks
> Cat22
>
From: Chris F.A. Johnson on
On 2010-03-27, Cat 22 wrote:
> In a bash script I have a variable called recs
> recs="this is line 1\r\nThis is line 2\r\nthis is line3\r\n"
>
> Actually recs gets loaded via ssh like this but the sample i am
> showing is correct:
> recs=$(ssh other_sys cat xp_share/records.txt)
> and recs ends up containing a lot of lines delimited this way
>
> The \r\n is an 0D 0A sequence (file produced on winders)
>
> How can i echo out the individual lines in a loop?
> I can use tr to change the 0d 0a to \r but it still
> prints out in 1 long line
> e.g
> ct=0
> while read line
> do
> echo "$ct $line"
> ((ct++))
> done < <(echo $recs)
>
> This only prints out 1 long line like this
> 1 this is line 1^MThis is line 2^Mthis is line3^M
>
> What i am after is this:
> 1 this is line 1
> 2 this is line 2
> 3 this is line 3
>
> I tried a number of experiments but cant get it right.

printf "%b\n" "$recs"

If you want to remove the carriage returns:

CR=$'\r'
printf "%b\n" "${recs/"$CR"/}"


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
===== My code in this post, if any, assumes the POSIX locale =====
===== and is released under the GNU General Public Licence =====
From: mop2 on
On Sat, 27 Mar 2010 03:38:04 -0300, Cat 22 <cat22(a)invalid.org> wrote:

> In a bash script I have a variable called recs
> recs="this is line 1\r\nThis is line 2\r\nthis is line3\r\n"
>
> Actually recs gets loaded via ssh like this but the sample i am
> showing is correct:
> recs=$(ssh other_sys cat xp_share/records.txt)
> and recs ends up containing a lot of lines delimited this way
>
> The \r\n is an 0D 0A sequence (file produced on winders)
>
> How can i echo out the individual lines in a loop?
> I can use tr to change the 0d 0a to \r but it still
> prints out in 1 long line
> e.g
> ct=0
> while read line
> do
> echo "$ct $line"
> ((ct++))
> done < <(echo $recs)
>
> This only prints out 1 long line like this
> 1 this is line 1^MThis is line 2^Mthis is line3^M
>
> What i am after is this:
> 1 this is line 1
> 2 this is line 2
> 3 this is line 3
>
> I tried a number of experiments but cant get it right.
> Thanks
> Cat22
>

You can try:
#done < <(echo $recs)
done < <(echo "$recs")


and, if ALWAYS 0A is preceded by 0D:
#echo "$ct $line"
echo "$ct ${line%?}"
From: Cat 22 on
Cat 22 wrote:

> In a bash script I have a variable called recs
> recs="this is line 1\r\nThis is line 2\r\nthis is line3\r\n"
>
> Actually recs gets loaded via ssh like this but the sample i am
> showing is correct:
> recs=$(ssh other_sys cat xp_share/records.txt)
> and recs ends up containing a lot of lines delimited this way
>
> The \r\n is an 0D 0A sequence (file produced on winders)
>
> How can i echo out the individual lines in a loop?
> I can use tr to change the 0d 0a to \r but it still
> prints out in 1 long line
> e.g
> ct=0
> while read line
> do
> echo "$ct $line"
> ((ct++))
> done < <(echo $recs)
>
> This only prints out 1 long line like this
> 1 this is line 1^MThis is line 2^Mthis is line3^M
>
> What i am after is this:
> 1 this is line 1
> 2 this is line 2
> 3 this is line 3
>
> I tried a number of experiments but cant get it right.
> Thanks
> Cat22
Thanks all!
For now what i did was (see below) and i wont bother with putting the
lines into an input variable at all.
My main gaol was to read the source file and in a loop deal with each
line, the line will be altered a bit in format and inserted into a
mysql database for later use. I wanted to do it without copying the
file from the other_sys to the local machine each time i did an
update.
Cat22

ct=1
while read line
do
echo "$ct $line"
((ct++))
done < <(ssh other_sys cat xp_share/records.txt)