Normal way to open the command-prompt in tmux is prefix + :. I want to bind the sequence prefix + ; to open the command prompt. I am too lazy to hit the shift key.
When I put this in my tmux.conf: bind-key ; command-prompt, I get this error: /Users/skilbjo/.tmux.conf:19: usage: bind-key [-cnr] [-t mode-table] [-T key-table] key command [arguments]
which is funny, because when I do prefix + ? (alias for tmux list-keys), this is listed: bind-key -T prefix : command-prompt. How does this sorcery work? I even tried bind-key -T prefix ; command-prompt to no avail, same error message
tmux uses semicolon as a command separator.
From the tmux man page:
Multiple commands may be specified together as part of a command sequence. Each command should be separated by spaces and a semicolon; commands are executed sequentially from left to right and lines ending with a backslash continue on to the next line, except when escaped by another backslash. A literal semicolon may be included by escaping it with a backslash (for example, when specifying a command sequence to bind-key).
What you'll want to do is:
unbind-key \;
bind-key \; command-prompt
Related
Content of testfile.txt
/path1/abc.txt
/path2/abc.txt.1
/path3/abc.txt123
Content of pattern.txt
abc.txt$
Bash Command
grep -i -f pattern.txt testfile.txt
Output:
/path1/abc.txt
This is a working solution, but currently the $ in the pattern is manually added to each line and this edited pattern file is uploaded to users. I am trying to avoid the manual amendment.
Alternate solution to loop and read line by line, but required scripting skills or upload scripts to user environment.
Want to keep the original pattern files in an audited environment, users just login and run simple cut-n-paste commands.
Any one liner solution?
You can use sed to add $ to pattern.txt and then use grep, but you might run into issues due to regexp metacharacters like the . character. For example, abc.txt$ will also match abc1txt. And unless you take care of matching only the basename from the file path, abc.txt$ will also match /some/path/foobazabc.txt.
I'd suggest to use awk instead:
$ awk '!f{a[$0]; next} $NF in a' pattern.txt f=1 FS='/' testfile.txt
/path1/abc.txt
pattern.txt f=1 FS='/' testfile.txt here a flag f is set between the two files and field separator is also changed to / for the second file
!f{a[$0]; next} if flag f is not set (i.e. for the first file), build an array a with line contents as the key
$NF in a for the second file, if the last field matches a key in array a, print the line
Just noticed that you are also using -i option, so use this for case insensitive matching:
awk '!f{a[tolower($0)]; next} tolower($NF) in a'
Since pattern.txt contains only a single pattern, and you don't want to change it, since it is an audited file, you could do
grep -i -f "$(<pattern.txt)'$' testfile.txt
instead. Note that this would break, if the maintainer of the file one day decided to actually write there a terminating $.
IMO, it would make more sense to explain to the maintainer of pattern.txt that he is supposed to place there a simple regular expression, which is going to match your testfile. In this case s/he can decide whether the pattern really should match only the right edge or some inner part of the lines.
If pattern.txt contains more than one line, and you want to add the $ to each line, you can likewise do a
grep -i -f <(sed 's/$/$/' <pattern.txt) testfile.txt
As the '$' symbol indicates pattern end. The following script should work.
#!/bin/bash
file_pattern='pattern.txt' # path to pattern file
file_test='testfile.txt' # path to test file
while IFS=$ read -r line
do
echo "$line"
grep -wn "$line" $file_test
done < "$file_pattern"
You can remove the IFS descriptor if the pattern file comes with leading/trailing spaces.
Also the grep option -w matches only whole word and -n provides with line number.
I am trying to use grep with the pwd command.
So, if i enter pwd, it shows me something like:
/home/hrq/my-project/
But, for purposes of a script i am making, i need to use it with grep, so it only prints what is after hrq/, so i need to hide my home folder always (the /home/hrq/) excerpt, and show only what is onwards (like, in this case, only my-project).
Is it possible?
I tried something like
pwd | grep -ov 'home', since i saw that the "-v" flag would be equivalent to the NOT operator, and combine it with the "-o" only matching flag. But it didn't work.
Given:
$ pwd
/home/foo/tmp
$ echo "$PWD"
/home/foo/tmp
Depending on what it is you really want to do, either of these is probably what you really should be using rather than trying to use grep:
$ basename "$PWD"
tmp
$ echo "${PWD#/home/foo/}"
tmp
Use grep -Po 'hrq/\K.*', for example:
grep -Po 'hrq/\K.*' <<< '/home/hrq/my-project/'
my-project/
Here, grep uses the following options:
-P : Use Perl regexes.
-o : Print the matches only (1 match per line), not the entire lines.
\K : Cause the regex engine to "keep" everything it had matched prior to the \K and not include it in the match. Specifically, ignore the preceding part of the regex when printing the match.
SEE ALSO:
grep manual
perlre - Perl regular expressions
I am trying to look for ">" character in a CSV using the grep command as follows:
grep ">" test_file.csv
grep \> test_file.csv
However, both these commands yield no results. I know for a fact that several instances of this character appear in this file. I am pretty sure this issue is coming up because ">" is also used as the stream input character. How do I search for ">" and get results successfully?
You can try following command
grep -F '>' application.log
grep -e '>' application.log
-F stand for input as fixed string.
-e make string as a pattern.
To print line number as well you can use -n flag.
In the tail output i have following string...Using grep command how can i search for the string "contentState\":\"STOPPED\". I have to search for the whole string "contentState\":\"STOPPED\" rather
than searching for STOPPED or contentState only.
I tried following command: But it is not working.
grep -e ""contentState\":\"STOPPED\" /opt/logs/out.log | tail -1
{\"eventType\":\"appAction\",\"action\":\"CONTENT_STATE_CHANGE\",\"evt\":{\"contentState\":\"STOPPED\"}}}
To search for "contentState\":\"STOPPED\", you need to (a) put the whole string in single-quotes to protect it, and (b) escape (double) the backslashes. Thus:
grep -e '"contentState\\":\\"STOPPED\\"' /opt/logs/out.log
Without the protection provided the outer single-quotes, the unescaped double-quotes would be subject to the shell's quote removal and grep would never see them.
Example
Consider this test file:
$ cat log
good "contentState\":\"STOPPED\"
bad contentState\":\"STOPPED\"
bad "contentState\":\"STOPPED"
Let's run our command:
$ grep -e '"contentState\\":\\"STOPPED\\"' log
good "contentState\":\"STOPPED\"
As we can see, the good line is returned and only the good line.
I'm trying to use Grep to find a string with Tabs, Carriage Returns, & New Lines. Any other method would be helpful also.
grep -R "\x0A\x0D\x09<p><b>Site Info</b></p>\x0A\x0D\x09<blockquote>\x0A\x0D\x09\x09<p>\x0A\x0D\x09</blockquote>\x0A\x0D</blockquote>\x0A\x0D<blockquote>\x0A\x0D\x09<p><b>More Site Info</b></p>" *
From this answer
If using GNU grep, you can use the Perl-style regexp:
$ grep -P '\t' *
Also from here
Use Ctrl+V, Ctrl+M to enter a literal Carriage Return character into your grep string. So:
grep -IUr --color "^M"
will work - if the ^M there is a literal CR that you input as I suggested.
If you want the list of files, you want to add the -l option as well.
Quoting this answer:
Grep is not sufficient for this operation.
pcregrep, which is
found in most of the modern Linux systems can be used ...
Bash Example
$ pcregrep -M "try:\n fro.*\n.*except" file.py
returns
try:
from tifffile import imwrite
except (ModuleNotFoundError, ImportError):