Puzzling Behaviour From tr '\n' ' '

Puzzling Behaviour From tr '\n' ' '

Post by absint » Wed, 16 Mar 2005 08:49:43



Hi,

I'm trying to read in from an SQL*Loader log file and read the four
numbers provided as summary load counts into a shell script array
using:

grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
'\n' ' '

Except tr gives me a null output and whilst sed gives me:
xxx
xxx
xxx

Where xxx is a number. Any ideas?

The gnis_native.log is available:
http://examples.oreilly.com/orsqlloader/

See the logs below.

grep "^Total logical records" gnis_native.log
+ grep ^Total logical records gnis_native.log
Total logical records skipped:          0
Total logical records read:            58
Total logical records rejected:        51
Total logical records discarded:        0

grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
+ sed s/.*: *//g
+ grep Total logical records gnis_native.log
0
58
51
0

grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
'\n' ' '
+ grep Total logical records gnis_native.log
+ tr \n
+ sed s/.*: *//g

 
 
 

Puzzling Behaviour From tr '\n' ' '

Post by Bill Seiver » Wed, 16 Mar 2005 14:00:23



> Hi,

> I'm trying to read in from an SQL*Loader log file and read the four
> numbers provided as summary load counts into a shell script array
> using:

> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
> '\n' ' '

> Except tr gives me a null output and whilst sed gives me:
> xxx
> xxx
> xxx

> Where xxx is a number. Any ideas?

> The gnis_native.log is available:
> http://examples.oreilly.com/orsqlloader/

> See the logs below.

> grep "^Total logical records" gnis_native.log
> + grep ^Total logical records gnis_native.log
> Total logical records skipped:          0
> Total logical records read:            58
> Total logical records rejected:        51
> Total logical records discarded:        0

> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
> + sed s/.*: *//g
> + grep Total logical records gnis_native.log
> 0
> 58
> 51
> 0

> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
> '\n' ' '
> + grep Total logical records gnis_native.log
> + tr \n
> + sed s/.*: *//g

I'm guessing that the line is still in a buffer somewhere,
since tr '\n' ' ' also changes the last \n to a space.

Try:

(grep ... | tr '\n' ' '; echo "")

The echo will add a newline, allowing the line to print.

Bill Seivert

 
 
 

Puzzling Behaviour From tr '\n' ' '

Post by Ed Morto » Wed, 16 Mar 2005 23:09:30



> Hi,

> I'm trying to read in from an SQL*Loader log file and read the four
> numbers provided as summary load counts into a shell script array
> using:

> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
> '\n' ' '

> Except tr gives me a null output and whilst sed gives me:
> xxx
> xxx
> xxx

> Where xxx is a number. Any ideas?

Maybe you've got control-Ms or some other control characters in the file.

Quote:> The gnis_native.log is available:
> http://examples.oreilly.com/orsqlloader/

I didn't see it there.

- Show quoted text -

Quote:> See the logs below.

> grep "^Total logical records" gnis_native.log
> + grep ^Total logical records gnis_native.log
> Total logical records skipped:          0
> Total logical records read:            58
> Total logical records rejected:        51
> Total logical records discarded:        0

> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
> + sed s/.*: *//g
> + grep Total logical records gnis_native.log
> 0
> 58
> 51
> 0

Why does the "sed" appear before the "grep" in the "set -x" trace?

Quote:> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
> '\n' ' '
> + grep Total logical records gnis_native.log
> + tr \n
> + sed s/.*: *//g

That appears to be doing a grep then a tr then a sed. Try this:

awk '/^Total logical records/{printf "%s ",$NF}END{print ""}'
gnis_native.log

and see if it fares any better.

        Ed.

 
 
 

Puzzling Behaviour From tr '\n' ' '

Post by Kevin Collin » Thu, 17 Mar 2005 09:25:13




>> Hi,

>> I'm trying to read in from an SQL*Loader log file and read the four
>> numbers provided as summary load counts into a shell script array
>> using:

>> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
>> '\n' ' '

>> Except tr gives me a null output and whilst sed gives me:
>> xxx
>> xxx
>> xxx

>> Where xxx is a number. Any ideas?

> Maybe you've got control-Ms or some other control characters in the file.

>> The gnis_native.log is available:
>> http://examples.oreilly.com/orsqlloader/

> I didn't see it there.

>> See the logs below.

>> grep "^Total logical records" gnis_native.log
>> + grep ^Total logical records gnis_native.log
>> Total logical records skipped:          0
>> Total logical records read:            58
>> Total logical records rejected:        51
>> Total logical records discarded:        0

>> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
>> + sed s/.*: *//g
>> + grep Total logical records gnis_native.log
>> 0
>> 58
>> 51
>> 0

> Why does the "sed" appear before the "grep" in the "set -x" trace?

Ed - that is how pipelines are displayed with shell tracing turned on...

Quote:>> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g" | tr
>> '\n' ' '
>> + grep Total logical records gnis_native.log
>> + tr \n
>> + sed s/.*: *//g

> That appears to be doing a grep then a tr then a sed. Try this:

> awk '/^Total logical records/{printf "%s ",$NF}END{print ""}'
> gnis_native.log

> and see if it fares any better.

Kevin
 
 
 

Puzzling Behaviour From tr '\n' ' '

Post by Ed Morto » Thu, 17 Mar 2005 10:05:02





<snip>
>>>grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
>>>+ sed s/.*: *//g
>>>+ grep Total logical records gnis_native.log
>>>0
>>>58
>>>51
>>>0

>>Why does the "sed" appear before the "grep" in the "set -x" trace?

> Ed - that is how pipelines are displayed with shell tracing turned on...

Not in any of the environments I use, e.g. bash under cygwin:

PS1> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
+ grep '^Total logical records' gnis_native.log
+ sed 's/.*: *//g'

Good to know it can become twisted in some other environments though -
could save me some needless debugging in future!

        Ed.

 
 
 

Puzzling Behaviour From tr '\n' ' '

Post by Kevin Collin » Fri, 18 Mar 2005 03:13:12






><snip>
>>>>grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
>>>>+ sed s/.*: *//g
>>>>+ grep Total logical records gnis_native.log
>>>>0
>>>>58
>>>>51
>>>>0

>>>Why does the "sed" appear before the "grep" in the "set -x" trace?

>> Ed - that is how pipelines are displayed with shell tracing turned on...

I guess I should amend that to say "could be" instead of "are".

Quote:> Not in any of the environments I use, e.g. bash under cygwin:

> PS1> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
> + grep '^Total logical records' gnis_native.log
> + sed 's/.*: *//g'

> Good to know it can become twisted in some other environments though -
> could save me some needless debugging in future!

Here it is from HP-UX 11.11 running ksh:

-> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
+ sed s/.*: *//g
+ grep ^Total logical records gnis_native.log

Here it is on RHEL 3 running pdksh (in the "right" order):

-> grep "^Total logical records" gnis_native.log | sed "s/.*: *//g"
+ grep ^Total logical records gnis_native.log
+ sed s/.*: *//g

Since I do most of my work on HP-UX (and have for a long time), I am used to
the "backwards" listing... :)

Kevin

 
 
 

1. different behaviour of 'while read' and 'for' loops in bash

I have a command I want to execute on several hosts via ssh. The hosts
are listed in a file "hosts.txt", one per line. I'm using bash.

First I tried this:

while read host; do
   ssh $host hostname
done < hosts.txt

but for some reason this only does the first host listed in hosts.txt,
not the rest. The uglier:

for host in `cat hosts.txt`; do
   ssh $host hostname
done

works fine and processes all hosts as it should.

All I can think is that the ssh in the first form somehow causes bash
to lose its place in the file "hosts.txt", but I have no idea how to
check that, or if that is to be expected...?

TIA for any clarification.

2. Dubugger in Solaris2.4?

3. Misterious behavior of 'dip' and 'find', help if u can...

4. Has anyone compiled Emacs 19.28?

5. 'tr' command doesn't work with ranges on 5.0.2?

6. HELP

7. 'double' or 'dbl' or 'dble', the disk space utilty

8. Unable to open a initial console.

9. sed -e 's/\'a\'/\';\'/' ?

10. What is 'stty', 'term' & 'tty'

11. Simple 'sed', 'awk', 'cut' problem

12. LinuxPPC99: 'INIT: ld ''x'' respawning too fast...'??

13. Can 'require' override 'allow'/'deny' (Apache)