LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-16-2008, 01:19 AM   #1
SPF
Member
 
Registered: Jul 2007
Location: /home
Distribution: Debian
Posts: 37

Rep: Reputation: 15
spaces are missing


I'm running bash:

Code:
$ ps
  PID TTY          TIME CMD
28408 pts/20   00:00:00 bash
13908 pts/20   00:00:00 ps
$ bash --version
GNU bash, version 2.04.21(1)-release (i386-redhat-linux-gnu)
This is the input data I want to progress:

Code:
$ top -b -d1 -n1 | tr -d '\r' | head -n 20


  4:55pm  up 40 days,  4:38, 26 users,  load average: 0.23, 0.38, 0.23
313 processes: 312 sleeping, 1 running, 0 zombie, 0 stopped
CPU states:  1.1% user,  0.5% system,  0.0% nice,  0.5% idle
Mem:  1033960K av,  882548K used,  151412K free,       0K shrd,   21428K buff
Swap: 1572944K av,   96712K used, 1476232K free                  560116K cached

  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
    1 root       9   0   508  472   440 S     0.0  0.0   0:08 init
    2 root       9   0     0    0     0 SW    0.0  0.0   0:00 keventd
    3 root      19  19     0    0     0 SWN   0.0  0.0   0:23 ksoftirqd_CPU0
    4 root       9   0     0    0     0 SW    0.0  0.0   0:10 kswapd
    5 root       9   0     0    0     0 SW    0.0  0.0   0:00 bdflush
    6 root       9   0     0    0     0 SW    0.0  0.0   0:09 kupdated
    7 root       9   0     0    0     0 SW    0.0  0.0   0:00 khubd
    8 root      -1 -20     0    0     0 SW<   0.0  0.0   0:00 mdrecoveryd
  593 root       9   0   612  604   516 S     0.0  0.0  42:51 syslogd
But if I change some lines, the spaces at the beginning of the lines are gone (this is an example that should work):

Code:
$ top -b -d1 -n1 | tr -d '\r' | head -n 30 | while read -r line; do echo -e "$line" ; done

8:05am  up 40 days, 19:48,  8 users,  load average: 0.18, 0.18, 0.17
234 processes: 233 sleeping, 1 running, 0 zombie, 0 stopped
CPU states:  1.1% user,  0.5% system,  0.0% nice,  0.8% idle
Mem:  1033960K av,  861712K used,  172248K free,       0K shrd,   17056K buff
Swap: 1572944K av,  105028K used, 1467916K free                  609364K cached
PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
612 rpc       13   0   604  580   516 S     0.9  0.0 506:55 portmap
1 root       9   0   508  472   440 S     0.0  0.0   0:08 init
2 root       9   0     0    0     0 SW    0.0  0.0   0:00 keventd
3 root      19  19     0    0     0 SWN   0.0  0.0   0:23 ksoftirqd_CPU0
4 root       9   0     0    0     0 SW    0.0  0.0   0:10 kswapd
5 root       9   0     0    0     0 SW    0.0  0.0   0:00 bdflush
6 root       9   0     0    0     0 SW    0.0  0.0   0:09 kupdated
7 root       9   0     0    0     0 SW    0.0  0.0   0:00 khubd
8 root      -1 -20     0    0     0 SW<   0.0  0.0   0:00 mdrecoveryd
593 root       9   0   612  604   516 S     0.0  0.0  43:36 syslogd
598 root       9   0  1272  432   432 S     0.0  0.0   0:00 klogd
630 rpcuser    9   0   692  596   596 S     0.0  0.0   0:00 rpc.statd
676 root       9   0     0    0     0 SW    0.0  0.0   0:00 rpciod
677 root       9   0     0    0     0 SW    0.0  0.0   0:00 lockd
728 root       9   0   584  500   484 S     0.0  0.0   0:00 automount
740 daemon     9   0   548  488   476 S     0.0  0.0   0:00 atd
775 root       9   0   680  632   488 S     0.0  0.0   0:37 smartd
787 root       8   0  1084  984   900 S     0.0  0.0   5:23 sshd
808 root       9   0   912  820   728 S     0.0  0.0   2:12 xinetd
If you view it in hex, you'll see that there are indeed spaces (hex code 20):

Code:
$ top -b -d1 -n1 | tr -d '\r' | head -n 30 | od -cx
0001620                   6       r   o   o   t                        
        2020 2020 2036 6f72 746f 2020 2020 2020
You need to use the "" characters to preserve spaces in the variables, but somehow it doesn't work here?

I also tried it with:

Code:
$ bash --version
GNU bash, version 3.1.17(1)-release (i486-pc-linux-gnu)
But with no luck.
What do I want with this printout? I'm getting this printout from various systems. But every printout comes with extra data which I have to delete.
That part is already finished. But I noticed during processing that the spaces at the beginning of the lines were deleted.
 
Old 01-16-2008, 02:10 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
This is because the read command removes the leading spaces.

Have you though of using Perl?

Code:
#!/usr/bin/perl
##
## f_perl_read
##
## A simple perl script to read from Standard Input
##
@l_array=<STDIN>;
foreach $l_var (@l_array)
{
   ###
   ### Any checks or additional processing can go here
   ###
   print "$l_var";
}
Code:
top -b -d1 -n1 | tr -d '\r' | head -n 30 |{dir_to_script}/f_perl_read
 
Old 01-16-2008, 02:20 AM   #3
SPF
Member
 
Registered: Jul 2007
Location: /home
Distribution: Debian
Posts: 37

Original Poster
Rep: Reputation: 15
Thanks Disillusionist, I didn't know that read removed the leading spaces.

I'll consider using perl.
 
Old 01-18-2008, 02:50 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
This could (and probably should) be written as:

Code:
#!/usr/bin/perl
##
## Second script example
##
while (<STDIN>) {
   ##
   ## Perform any checks here!
   ##
   print $_;
}
Notes:

$_ is a special variable in Perl and is used to store results of read requests where a variable hasn't been stated.

In this case it contains the current line from STDIN.

The " " symbols are not required in the print statement.

The script loops through STDIN without building an array, this should reduce the amount of RAM being used. (Especially if you are passing in a lot of information)
 
Old 01-18-2008, 04:36 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
If you want to continue using the shell (instead of e.g., perl), you can temporarily disable the field separation functionality of read. For example,
Code:
top -b -d1 -n1 | tr -d '\r' | head -n 30 | (IFS=''; while read -r line; do echo -e "$line" ; done)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem running UT Demo, Xlib missing on 0.0.. gldrv missing etc. RHLinuxGUY Linux - Games 3 05-21-2006 11:02 PM
Way to many spaces LuderForChrist Linux - Newbie 4 06-25-2004 02:29 PM
cd to a directory with spaces? GT_Onizuka Linux - Newbie 3 08-28-2003 05:00 PM
dump name spaces mnauta Linux - General 4 05-29-2003 01:43 PM
spaces WindowsDefector Linux - Newbie 2 05-23-2002 02:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration