A quick question: ls . | grep -E "^[0-9]" gives me the results in the following format:
1
2
3
4
5
How can I let it be simply displayed as 1 2 3 4 5?
Try
ls . | grep -E "^[0-9]" | tr '\n' ' ' ; echo
try this with tr:
your cmd ....|tr "\n" ' '
try ls . | grep -E "^[0-9" | tr '\n' ' '
Using awk
ls . | awk '/^[0-9]/ {printf "%s ",$0}'
Or more clean:
ls . | awk '/^[0-9]/ {printf "%s ",$0} END {print ""}'
If it is available, you can use the column command from bsdmainutils:
ls | grep '^[0-9]' | column
Output:
1 2 3 4 5
Another test:
seq 50 | column
Example output:
1 6 11 16 21 26 31 36 41 46
2 7 12 17 22 27 32 37 42 47
3 8 13 18 23 28 33 38 43 48
4 9 14 19 24 29 34 39 44 49
5 10 15 20 25 30 35 40 45 50
Related
I've used the answer to this question to enable control of my Raspberry Pi GPIO pins from within a Docker container, which runs Alpine.
$ docker run --device /dev/gpiomem whatever
This works with the Python RPi.GPIO module, but not with wiringPi.
Python and its dependencies takes up about the same space as Alpine itself, so I'd like to use wiringPi to save on install time and SD card space, and to keep things simple.
Running wiringPi v2.46 in Raspbian directly (installed using apt-get install wiringpi) works fine, and I can successfully configure and trigger digital output pins.
Running wiringPi v2.26 in the Alpine container (installed using apk add wiringpi) fails when trying to configure a pin:
$ gpio -g mode 26 out
Unable to determine hardware version. I see: Hardware : BCM2835,
- expecting BCM2708 or BCM2709. Please report this to projects#drogon.net
Is there anything I can do to expose the SOC to the container, so that wiringPi recognises it correctly? What else might be required to get this working?
Warning: Thar be hacks ahead.
The fundamental problem, as I noted in my comment, is that your containerized wiringPi is simply too old. Older Pi harwdare (like the Pi 2) used the BCM2708/BCM2709 processors, but newer hardware uses the BCM2835. WiringPi needs to know what processor is in use in order to correctly locate the GPIO control registers.
It's possible that there isn't a substantial difference between the older and newer processors as far as GPIO goes. If -- and only if -- this is the case, you can "trick" your containerized wiringPi into working.
Create a version a /proc/cpuinfo with the desired processor name:
sed s/BCM2835/BCM2709/ < /proc/cpuinfo > /proc/cpuinfo
Bind mount that onto /proc/cpuinfo inside the container. You'll also need to provide /dev/mem, and you'll need to run with --privileged:
docker run -it --rm --privileged --device /dev/mem -v /tmp/cpuinfo:/proc/cpuinfo alpine sh
This gives us:
/ # gpio -v
gpio version: 2.26
Copyright (c) 2012-2015 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty
Raspberry Pi Details:
Type: Model 2, Revision: 1.1, Memory: 1024MB, Maker: Sony [OV]
/ # gpio readall
+-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | IN | 0 | 3 || 4 | | | 5V | | |
| 3 | 9 | SCL.1 | IN | 0 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 0 | 7 || 8 | 0 | IN | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 0 | IN | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 0 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 0 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 0 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 0 | 27 || 28 | 0 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 0 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 0 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 2---+---+------+---------+-----+-----+
You would want to experiment to see if this actually works as intended.
Or just run a Raspbian image instead of an Arch image.
Thanks to larsks for pointing out the difference in version numbers.
Version 2.46 of wiringPi is available for Alpine, but I'd failed to notice it's only in the edge branch of the community repository
To use this version I had to modify the file /etc/apk/repositories, replacing the existing community entry with the edge version.
Since I'm using Docker, I had to do this as part of the image build process, so I added the following to my Dockerfile:
RUN sed -i "s/v[0-9.]*\/community/edge\/community/" /etc/apk/repositories \
&& apk update && apk add wiringpi
So using exim and sed commands I'm getting the results as below
10 /home/user1
20 /home/user2/public_html
30 /home/user3
40 /home/user4/public_html
50 /home/user5
60 /home/user6/public_html
This shows how many mails has been send from user.
How can I get the result in descending order and get the username only?
i.e., from above result I want to grep user6 and then run /scripts/suspendacct user6
With awk and sort:
awk -F '[/ ]' '{print $1,$4}' file | sort -n -r
Output:
60 user6
50 user5
40 user4
30 user3
20 user2
10 user1
Or use cut to get the fields you want and pipe to sort:
$ cut --output-delimiter="" -d / -f 1,3 file | sort -r
60 user6
50 user5
40 user4
30 user3
20 user2
10 user1
I am trying to write a Fortran program which will eat up a lot of memory (for the reasoning behind this, please see the note at the end of this question). I am doing this by allocating a 3 dimensional array of size (n,n,n) and then deallocating it - continually increasing n until I run out of memory (this should happen when ~16 GB of memory is used). Unfortunately, it seems as if my program is running out of memory long before I see the system resources get up to 16 GB.
Here is my sample code:
1 program fill_mem
2 implicit none
3 integer, parameter :: ikind = selected_int_kind(8)
4 integer, parameter :: rkind = 8
5
6 integer(kind = ikind) :: nfiles = 100
7 integer(kind = ikind) :: n = 1200
8 integer(kind = ikind) :: i, nn
9
10 real(kind = rkind), allocatable :: real_arr(:,:,:)
11
12 character(500) :: sysline
13
14
15 call system('echo ''***no_allocation***'' > outfile')
16 call system('ps aux | grep fill_mem.exe >> outfile')
17 !call system('smem | grep fill_mem.exe >> sm.out')
18 allocate(real_arr(n, n, n))
19
20 nn = 100000
21 do i = 1,nn
22 deallocate(real_arr)
23 n = n + 10
24 print*, 'n = ', n
25 allocate(real_arr(n, n, n))
26 call system('echo ''*************'' >> outfile')
27 write(sysline, *) 'allocation', i, '... n = ', n
28
29 write(*, '(f10.5, a)') 100.0*real(i)/real(nn), '%'
30
31 call system(trim(adjustl('echo '//sysline//'>> outfile')))
32 call system('ps aux | grep fill_mem.exe >> outfile')
33 enddo
34
35 end program fill_mem
and here is the sample output:
1 ***no_allocation***
2 1000 12350 0.0 0.0 12780 760 pts/1 S+ 13:32 0:00 ./fill_mem.exe
3 1000 12352 0.0 0.0 4400 616 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
4 1000 12354 0.0 0.0 9384 920 pts/1 S+ 13:32 0:00 grep fill_mem.exe
5 *************
6 allocation 1 ... n = 1210
7 1000 12350 0.0 0.0 13853104 796 pts/1 S+ 13:32 0:00 ./fill_mem.exe
8 1000 12357 0.0 0.0 4400 616 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
9 1000 12359 0.0 0.0 9384 920 pts/1 S+ 13:32 0:00 grep fill_mem.exe
10 *************
11 allocation 2 ... n = 1220
12 1000 12350 0.0 0.0 14199096 952 pts/1 S+ 13:32 0:00 ./fill_mem.exe
13 1000 12362 0.0 0.0 4400 612 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
14 1000 12364 0.0 0.0 9384 920 pts/1 S+ 13:32 0:00 grep fill_mem.exe
15 *************
16 allocation 3 ... n = 1230
17 1000 12350 0.0 0.0 14550804 956 pts/1 S+ 13:32 0:00 ./fill_mem.exe
18 1000 12367 0.0 0.0 4400 612 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
19 1000 12369 0.0 0.0 9384 920 pts/1 S+ 13:32 0:00 grep fill_mem.exe
20 *************
21 allocation 4 ... n = 1240
22 1000 12350 0.0 0.0 14908284 956 pts/1 S+ 13:32 0:00 ./fill_mem.exe
23 1000 12372 0.0 0.0 4400 612 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
24 1000 12374 0.0 0.0 9384 920 pts/1 S+ 13:32 0:00 grep fill_mem.exe
25 *************
26 allocation 5 ... n = 1250
27 1000 12350 0.0 0.0 15271572 956 pts/1 S+ 13:32 0:00 ./fill_mem.exe
28 1000 12377 0.0 0.0 4400 612 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
29 1000 12379 0.0 0.0 9384 916 pts/1 S+ 13:32 0:00 grep fill_mem.exe
30 *************
31 allocation 6 ... n = 1260
32 1000 12350 0.0 0.0 15640720 956 pts/1 S+ 13:32 0:00 ./fill_mem.exe
33 1000 12382 0.0 0.0 4400 616 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
34 1000 12384 0.0 0.0 9384 920 pts/1 S+ 13:32 0:00 grep fill_mem.exe
35 *************
36 allocation 7 ... n = 1270
37 1000 12350 0.0 0.0 16015776 956 pts/1 S+ 13:32 0:00 ./fill_mem.exe
38 1000 12387 0.0 0.0 4400 616 pts/1 S+ 13:32 0:00 sh -c ps aux | grep fill_mem.exe >> outfile
39 1000 12389 0.0 0.0 9384 920 pts/1 S+ 13:32 0:00 grep fill_mem.exe
Now, I see that the VSZ portion gets up to ~15 GB so I am assuming when I try to address more, it fails with
Operating system error: Cannot allocate memory
Allocation would exceed memory limit
because there is not that much RAM. Why is it that RSS is so far below that, though? When I actually look on my system resources I see about 140 MB being used up (I am running this in a Linux VM and monitoring the system resources through Windows - I have given the GM 16 GB of RAM to use though, so I should see the VM memory increasing until it reaches the 16 GB mark - for what it's worth, the VM has VT-x/Nested Paging/PAE/NX so it should use the physical architecture just like the native OS).
Can anyone explain why I do not see my program actually using up the full 16 GB of RAM and how I can write my code to keep these arrays I am creating in RAM - fully utilizing my available hardware?
NOTE: The reason I am trying to write a sample program which reads a lot of memory is that I am working with data which takes up around 14 GB of space in ascii text. I will need to be working with data A LOT throughout the course of this program, so I want to read it all in at once and then reference it from RAM throughout the duration of the program. To make sure I am doing this correctly, I am trying to write a simple program which will store a very large array (~15 GB) in memory all at once.
(Caveat: The Fortran standard doesn't say how such thing ought to be implemented etc., the description below refers to how Fortran compilers are typically implemented on current operating systems.)
When you execute an ALLOCATE statement (or equivalently, calling malloc() in C, FWIW), you're not actually reserving physical memory, but only mapping address space for your process. That's why the VSZ goes up, but not the RSS. Actually reserving physical memory for your process happens only when you first access the memory (typically at page size granularity, that is, 4 KB on most current hw). So only once you start putting some data into your array does the RSS begin to climb. E.g. a statement like
real_arr = 42.
ought to bump up your RSS to the vicinity of the VSZ.
You probably need to increase the memory allocated to the stack. For example, see http://software.intel.com/en-us/articles/intel-fortran-compiler-increased-stack-usage-of-80-or-higher-compilers-causes-segmentation-fault
I have a grep command that gives the following string:
20121121001100 18 0 16 2 18
but I would like to modify this string to get
20121121 001 18 0 16 2 18
the above value are being extracted by the following:
for i in `ls -1 file.txt | sort`; do echo $i`
grep datetime $i | wc -l ``
grep abc $i | wc -l ``
grep def $i | wc -l ``
grep ghi $i | wc -l ``
grep jkl $i | wc -l ` ; done | cut -c9-500
cut -c9-500 is used because the original string is in the form of
datetime20121121001100 18 0 16 2 18
and cut -c9-500 returns
20121121001100 18 0 16 2 18
Can someone please help me to get
20121121 001 18 0 16 2 18
(ie remove the last 3 digits from the date part)
Most of what you want/do can be accomplished with awk. But for the minimum you want:
for i in `ls -1 file.txt | sort`; do echo $i`
grep datetime $i | wc -l ``
grep abc $i | wc -l ``
grep def $i | wc -l ``
grep ghi $i | wc -l ``
grep jkl $i | wc -l ` ; done | cut -c9-500 | awk '{print substr($0,1,11) substr($0,15) }'
awk is very capable at text processing.
Edit: I'm not sure of what are you doing, but, basicly this does (almost) the same:
awk 'FILENAME != oldfilename {oldfilename = FILENAME; dt = 0 ; a = 0; d = 0; g = 0; j = 0}
/datetime/ {dt++}
/abc/ {a++}
/def/ {d++}
/ghi/ {g++}
/j/ {j++}
END {print FILENAME, dt, a, d, g, j}' *
And it's faster, fewer processes, etc... Basically awk process the file, counts the occurences of the specified strings, and when it finishes the file (after the last line) prints the report.
Changed specs:
for i in `ls -1 file.txt | sort`; do echo $i`
grep datetime $i | wc -l ``
grep abc $i | wc -l ``
grep def $i | wc -l ``
grep ghi $i | wc -l ``
grep jkl $i | wc -l ` ; done | cut -c9-500 | awk '{print substr($0,1,8) " " substr($0,9,4) substr($0,15) }'
Pipe to sed:
echo "20121121001100 18 0 16 2 18" | sed -r 's/^([0-9]+)[0-9][0-9][0-9] (.*)$/\1 \2/'
gives
20121121001 18 0 16 2 18
I have a list of number in a file with format: {integer}\n . So a possible list is:
3
12
53
23
18
32
1
4
i want to use grep to get the count of a specific number, but grep -c "1" file results 3 because it takes into account except the 1, the 12, 18 also. How can i correct this?
Although all the answers until now are logical, and i thought of them and tested before, actually nothing works:
username#domain2:~/code/***/project/random/r2$ cat out.txt
2
16
11
1
13
2
1
16
16
9
username#domain2:~/code/***/project/random/r2$ grep -Pc "^1$" out.txt
0
username#domain2:~/code/***/project/random/r2$ grep -Pc ^1$ out.txt
0
username#domain2:~/code/***/project/random/r2$ grep -c ^1$ out.txt
0
username#domain2:~/code/***/project/random/r2$ grep -c "^1$" out.txt
0
username#domain2:~/code/***/project/random/r2$ grep -xc "^1$" out.txt
0
username#domain2:~/code/***/project/random/r2$ grep -xc "1" out.txt
0
Use the -x flag:
grep -xc 1 file
This is what it means:
-x, --line-regexp
Select only those matches that exactly match the whole line.
There a some other ways you can do this besides grep
$ cat file
3 1 2 100
12 x x x
53
23
18
32
1
4
$ awk '{for(i=1;i<=NF;i++) if ($i=="1") c++}END{print c}' file
2
$ ruby -0777 -ne 'puts $_.scan(/\b1\b/).size' file
2
$ grep -o '\b1\b' file | wc -l
2
$ tr " " "\n" < file | grep -c "\b1\b"
2
Use this regex...
\D1\D
...or ^1$ with multiline mode on.
Tested with RegExr and they both work.
Use e.g. ^123$ to match "Beginning of line, 123, End of line"
grep -wc '23' filename.txt
It will count the number of exact matches of digit 23.