How do I grep for all non-ASCII characters?

You can use the command: grep –color=”auto” -P -n “[\x80-\xFF]” file.xml This will give you the line number, and will highlight non-ascii chars in red. In some systems, depending on your settings, the above will not work, so you can grep by the inverse grep –color=”auto” -P -n “[^\x00-\x7F]” file.xml Note also, that the important … Read more

How can I format my grep output to show line numbers at the end of the line, and also the hit count?

-n returns line number. -i is for ignore-case. Only to be used if case matching is not necessary $ grep -in null myfile.txt 2:example two null, 4:example four null, Combine with awk to print out the line number after the match: $ grep -in null myfile.txt | awk -F: ‘{print $2″ – Line number : … Read more

Grep only the first match and stop

-m 1 means return the first match in any given file. But it will still continue to search in other files. Also, if there are two or more matched in the same line, all of them will be displayed. You can use head -1 to solve this problem: grep -o -a -m 1 -h -r … Read more

Capturing Groups From a Grep RegEx

If you’re using Bash, you don’t even have to use grep: files=”*.jpg” regex=”[0-9]+_([a-z]+)_[0-9a-z]*” for f in $files # unquoted in order to allow the glob to expand do if [[ $f =~ $regex ]] then name=”${BASH_REMATCH[1]}” echo “${name}.jpg” # concatenate strings name=”${name}.jpg” # same thing stored in a variable else echo “$f doesn’t match” >&2 … Read more

Delete all local git branches

The ‘git branch -d’ subcommand can delete more than one branch. So, simplifying @sblom’s answer but adding a critical xargs: git branch -D `git branch –merged | grep -v \* | xargs` or, further simplified to: git branch –merged | grep -v \* | xargs git branch -D Importantly, as noted by @AndrewC, using git … Read more