From: Ed Morton on
On 3/27/2010 10:46 PM, Cat 22 wrote:
> 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.

You should be using awk for that, not shell.

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)

awk '{print NR,$0}' < <(ssh other_sys cat xp_share/records.txt)

assuming the part from "<" on does what you want wrt providing input to the script.