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.