Checking if output of a command contains a certain string in a shell script
Testing $? is an anti-pattern. if ./somecommand | grep -q ‘string’; then echo “matched” fi
Testing $? is an anti-pattern. if ./somecommand | grep -q ‘string’; then echo “matched” fi
GNU grep can also support positive & negative look-ahead & look-back: For your case, the command would be: echo “Here is a string” | grep -o -P ‘(?<=Here).*(?=string)’ If there are multiple occurrences of Here and string, you can choose whether you want to match from the first Here and last string or match them … Read more
If you have GNU Grep, it should work like this: grep –exclude-dir=”.svn” If happen to be on a Unix System without GNU Grep, try the following: grep -R “whatever you like” *|grep -v “\.svn/*”
Why not: ls *.{mp3,exe,mp4} I’m not sure where I learned it – but I’ve been using this.
The following will print the line matching TERMINATE till the end of the file: sed -n -e ‘/TERMINATE/,$p’ Explained: -n disables default behavior of sed of printing each line after executing its script on it, -e indicated a script to sed, /TERMINATE/,$ is an address (line) range selection meaning the first line matching the TERMINATE … Read more
paste is good for this job: paste -d ” ” – – < filename
This will match a single non-ASCII character: [^\x00-\x7F] This is a valid PCRE (Perl-Compatible Regular Expression). You can also use the POSIX shorthands: [[:ascii:]] – matches a single ASCII char [^[:ascii:]] – matches a single non-ASCII char [^[:print:]] will probably suffice for you.**
3 characters before and 4 characters after $> echo “some123_string_and_another” | grep -o -P ‘.{0,3}string.{0,4}’ 23_string_and
If the files are sorted (they are in your example): comm -23 file1 file2 -23 suppresses the lines that are in both files, or only in file 2. If the files are not sorted, pipe them through sort first… See the man page here
There is a command-line tool called FINDSTR that comes with all Windows NT-class operating systems (type FINDSTR /? into a Command Prompt window for more information) It doesn’t support everything grep does but it might be sufficient for your needs.