Understanding the behavior of grep [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I am under mac os X and I want to find some words in the documentation of Homebrew.
This is a part of the documentation (man brew):
I tried this command
man brew | grep -e 'another installed' --color -B 2
and it worked well, below the result:
o leaves: Show installed formulae that are not dependencies of
another installed formula.
This command
man brew | grep -e 'leaves' --color -B 2
instead did not produce any output.
So I tried to use
brew | grep -e 'leaves.*' --color -B 2
but it did not work too.
I want to use grep to find the word 'leaves' inside the documentation of Homebrew and print the nearest lines to be able to understand what this option mean.
I know that maybe it is a stupid error but I am not able to figure out where it is.
Can someone help me?

The problem is not with grep so much as with man. When you view a manual page, formatting codes are interspersed in order to make the text appear bold (or underlined, etc).
bash-3.2#yosemite$ man brew | grep l..e..a..v..e..s
o leaves: Show installed formulae that are not dependencies of
bash-3.2#yosemite$ man brew | grep l..e..a..v..e..s | xxd
0000000: 2020 2020 2020 202b 086f 2020 206c 086c +.o l.l
0000010: 6508 6561 0861 7608 7665 0865 7308 733a e.ea.av.ve.es.s:
0000020: 2053 686f 7720 2069 6e73 7461 6c6c 6564 Show installed
0000030: 2020 666f 726d 756c 6165 2020 7468 6174 formulae that
0000040: 2020 6172 6520 206e 6f74 2020 6465 7065 are not depe
0000050: 6e64 656e 6369 6573 2020 6f66 0a ndencies of.
The traditional workaround is to filter formatted output through colcrt:
bash-3.2#yosemite$ man brew | colcrt | grep leaves
+ leaves: Show installed formulae that are not dependencies of
... but the resulting text is kind of crudely forced to ASCII only. A better solution for most people is to use LESS as your manual pager.
bash-3.2#yosemite$ export PAGER=less
bash-3.2#yosemite$ man brew
# ... type /leaves at the `less` prompt
o leaves: Show installed formulae that are not dependencies of
another installed formula.

Related

why use "use -elf" the result return username with "systemd+"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 months ago.
Improve this question
when I use docker run -itd mysql,then to use ps -elf check the process infomation with "4 S systemd+ 257584 257561 1 80 0 - 712611 poll_s Jul17 ? 00:40:16 mysqld".
root#xx:/proc/257584/ns# ps -elf | grep mysqld
4 S systemd+ 257584 257561 1 80 0 - 712611 poll_s Jul17 ? 00:40:20 mysqld
root#xx:/proc/257584/ns# ps -el | grep mysqld
4 S 999 257584 257561 1 80 0 - 712611 poll_s ? 00:40:21 mysqld
But I use "cat /cat/passwd" can't find username equal to "systemd+".
docker Version: 20.10.12
os ubuntu20.04
ps (sadly) trims the username to 8 (if i'm counting right) characters and adds a + after the user name initial part. The username could be systemd-mysql or systemd-something that you can find in passwd.
From manual:
If the length of the username is greater than the length of the display column, the username will be truncated. See the -o and -O formatting options to customize length

How to tail a binary file over ssh? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I have a log file in a custom binary format sitting on a remote machine. I want to sync it with a file on my local machine, so when new bytes are appended to the remote binary file, the file on my local machine will be updated as well.
I learned about how to use tail over ssh here: https://serverfault.com/questions/608723/tail-a-file-from-ssh-and-mirror-to-a-local-file
Then I learned about how to use tail on binary files here: Binary "tail" a file
I tried combining them into
ssh -t remotebox "tail -c +1 -f /path/to/file.bin" > ./mirror.bin
But then I realized that mirror.bin is corrupted. Looking at the hex dump, I see that all 0d0a bytes are truncated into 0as (\r\n got replaced by just \n). However, if I run tail locally (tail -c +1 -f file1.bin > file2.bin), this truncation does not happen.
I also tried to use tee instead of redirection; the problem persists.
Are there shell tricks I can do with tail to prevent this from happening, or are there other programs that suites my needs?
Thanks.
P.S. Both remote and local machines are Linux, running bash.
The following reproduces the problem:
$ echo -e '123\r\n234\r\n' > /tmp/1
$ scp /tmp/1 server:/tmp/1
$ ssh -t server cat /tmp/1 > /tmp/2
Shared connection to closed.
$ diff /tmp/1 /tmp/2
1,3c1,3
< 123
< 234
<
---
> 123
> 234
>
It's possible that ssh -t changes \r\n to \n because it might do stty +igncr on the line. Mine ssh -t does the opposite (I think stty +inlcr) and I have the character doubled:
$ ssh -t server cat /tmp/1 | hexdump -C
Shared connection to closed.
00000000 31 32 33 0d 0d 0a 32 33 34 0d 0d 0a 0d 0a |123...234.....|
0000000e
$ ssh server cat /tmp/1 | hexdump -C
00000000 31 32 33 0d 0a 32 33 34 0d 0a 0a |123..234...|
0000000b
Anyway. remove the -t option if you do not have the intention to use terminal features.
And your command line suggest that you are copying a file. I have no idea why do you use tail for that - tail it's for outputting last part of file, not for printing the whole file... To print the whole file use cat. To copy a file, use scp.

Is there a difference between commands "cp -P" and "cp -d"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
man cp says:
-d same as --no-dereference --preserve=links
-P, --no-dereference never follow symbolic links in SOURCE
I test these two options and find they are same in the practice. Both just keep the symbolic links regardless of whether the symbolic(soft) link is valid or not.
Any ideas?
Thank you.
They have the same effect on symbolic links. But -d has the additional effect of preserving hard links. That is, with -d or --preserve=links, if an invocation of cp encounters multiple links to the same file, it will create multiple links to the same file in the destination. Ordinarily cp doesn't pay attention to hard links and creates files that happen to have identical contents if two source files are hard links.
$ touch foo
$ ln foo bar
$ mkdir d; cp -d foo bar d
$ mkdir P; cp -P foo bar P
$ ls -log d P
P:
total 0
-rw-rw-r-- 1 0 Apr 11 17:09 bar
-rw-rw-r-- 1 0 Apr 11 17:09 foo
d:
total 0
-rw-rw-r-- 2 0 Apr 11 17:09 bar
-rw-rw-r-- 2 0 Apr 11 17:09 foo

Why Ubuntu 18.04 use `/sbin/init` instead of `systemd`? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
First of all, Here is my environment of system:
# cat /proc/version
Linux version 4.15.0-52-generic (buildd#lgw01-amd64-051) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #56-Ubuntu SMP Tue Jun 4 22:49:08 UTC 2019
# cat /etc/issue
Ubuntu 18.04.2 LTS \n \l
Refer to this Ubuntu Wiki, ubuntu has used Systemd by default since 15.04 and Systemd runs with PID 1 as /sbin/init. However, I found the different result on my ubuntu 18.04:
# ps aux | awk '$2==1{print $0}'
root 1 0.0 0.8 159692 8784 ? Ss Oct24 0:21 /sbin/init noibrs splash
# lsof -p 1 | grep txt
systemd 1 root txt REG 252,1 1595792 927033 /lib/systemd/systemd
So, my question is that:
Why Ubuntu 18.04 use /sbin/init instead of /lib/systemd/systemd?
Why lsof -p 1 | grep txt return /lib/systemd/systemd while the process of PID 1 is /sbin/init?
/sbin/init is a symbolic link to /lib/systemd/systemd
Take a look at the output of stat /sbin/init or readlink /sbin/init
This is what they mean by systemd "running as /sbin/init". The systemd binary is linked as /sbin/init and started by that link name.
Update
To further explain the difference between the ps and lsof output: ps is showing the command that started the process, while lsof is showing which files a process has opened.
When systemd was started, it was called by /sbin/init noibrs splash, the file system resolved the link to the file /lib/systemd/systemd which was then read from disk and executed.

how to open all shortcuts files from a directory with chromium-browser in one command [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
On ubuntu 16.04, i have a directory with these files :
-rw-rw-r-- 1 user0 user0 86 jui 7 21:32 vim html picker.url
-rw-rw-r-- 1 user0 user0 104 jui 7 21:32 cocoonjs build android apk.url
-rw-rw-r-- 1 user0 user0 61 jui 7 21:32 Simple Modal Window - Codepad.url
-rw-rw-r-- 1 user0 user0 96 jui 7 21:32 cocoon.js android build apk+++.url
-rw-rw-r-- 1 user0 user0 44 jui 7 21:32 CodePen - Front End Developer Playground & Code Editor in the Browser (1).url
The file "vim html picker.url" have this information :
--> cat vim\ html\ picker.url
[InternetShortcut]
URL=https://github.com/KabbAmine/vCoolor.vim/blob/master/README.md
what i want to do is open all of theses files from this directory in tab in my chromium-browser.
i have tried this in my gnome-terminal :
chromium-browser *.*
but chrome open the text information : URL=https://github.com/KabbAmine/vCoolor.vim/blob/master/README.md and not the url itself :https://github.com/KabbAmine/vCoolor.vim/blob/master/README.md.
wich command allow my desired behaviour ?
grep "^URL=" *.url | cut -d= -f2 | xargs chromium-browser
should do the trick.
Explanation:
grep "^URL=" *.url - cut the line beginning with URL= from each file ending in .url
cut -d= -f2 - split each remaining line into parts delimited by '=' and output the second and all subsequent parts (i.e. the part after the first '=')
xargs chromium-browser - use the list of URLs as arguments to chromium.

Resources