[regex] grep's at sign caught as whitespace

I ran into a problem while trying to grep a file that contains the following:

@abc abc  abc  @abc@  @abc@   @abc@ acb abc@ assd dsasd abc@  

I tried various grep commands, but there was a problem and abc@ or @abc were caught as whitespace in my regexps. I want the output, in general, to simulate the following script's output (only the "Good" ones):

for res in $(grep -F 'abc' test.txt); do if [[ ! $res == *@* ]]; then echo $res is good; else echo $res is bad; fi; done

This question is related to regex linux ubuntu grep

The answer is


No -P needed; -E is sufficient:

grep -E '(^|\s)abc(\s|$)' 

or even without -E:

grep '\(^\|\s\)abc\(\s\|$\)' 

After some time with Google I asked on the ask ubuntu chat room.

A user there was king enough to help me find the solution I was looking for and i wanted to share so that any following suers running into this may find it:

grep -P "(^|\s)abc(\s|$)" gives the result I was looking for. -P is an experimental implementation of perl regexps.

grepping for abc and then using filters like grep -v '@abc' (this is far from perfect...) should also work, but my patch does something similar.


Questions with regex tag:

Why my regexp for hyphenated words doesn't work? grep's at sign caught as whitespace Preg_match backtrack error regex match any single character (one character only) re.sub erroring with "Expected string or bytes-like object" Only numbers. Input number in React Visual Studio Code Search and Replace with Regular Expressions Strip / trim all strings of a dataframe return string with first match Regex How to capture multiple repeated groups? How do I use a regex in a shell script? How to find and replace with regex in excel VSCode regex find & replace submatch math? How can I remove the last character of a string in python? Regex Explanation ^.*$ Best way to verify string is empty or null Check for special characters in string Matching strings with wildcard MSSQL Regular expression Phone Number Validation MVC Swift extract regex matches How do I deal with special characters like \^$.?*|+()[{ in my regex? How would I get everything before a : in a string Python Select columns based on string match - dplyr::select How to validate email id in angularJs using ng-pattern Check if string contains only letters in javascript Reference - What does this regex mean? Apache 2.4 - Request exceeded the limit of 10 internal redirects due to probable configuration error Count the number of Occurrences of a Word in a String How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops Python, remove all non-alphabet chars from string Regex for Mobile Number Validation Regex date validation for yyyy-mm-dd regex match any whitespace Design DFA accepting binary strings divisible by a number 'n' Jquery Value match Regex Splitting string with pipe character ("|") How to use regex in XPath "contains" function How to replace all special character into a string using C# Regex pattern inside SQL Replace function? Check if a string matches a regex in Bash script Regular expression to search multiple strings (Textpad) How do I remove all non-ASCII characters with regex and Notepad++? Replace all non Alpha Numeric characters, New Lines, and multiple White Space with one Space Regex replace uppercase with lowercase letters Regex remove all special characters except numbers? Concrete Javascript Regex for Accented Characters (Diacritics) Java: Replace all ' in a string with \' Email address validation in C# MVC 4 application: with or without using Regex bash, extract string before a colon

Questions with linux tag:

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt? How to post raw body data with curl? Copy Files from Windows to the Ubuntu Subsystem How to use local docker images with Minikube? Can Windows Containers be hosted on linux? gradlew command not found? ssh connection refused on Raspberry Pi Composer: file_put_contents(./composer.json): failed to open stream: Permission denied Curl : connection refused boto3 client NoRegionError: You must specify a region error only sometimes gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now sudo: docker-compose: command not found How to upgrade pip3? How can I remove jenkins completely from linux Linux Command History with date and time MySQL fails on: mysql "ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded" What is difference between arm64 and armhf? How to redirect output of systemd service to a file Retrieve last 100 lines logs Failed to find Build Tools revision 23.0.1 Run an Ansible task only when the variable contains a specific string What does `set -x` do? How to edit a text file in my terminal Starting a shell in the Docker Alpine container How to run SUDO command in WinSCP to transfer files from Windows to linux Fail during installation of Pillow (Python module) in Linux How to install Android SDK on Ubuntu? How do I delete virtual interface in Linux? What is the default root pasword for MySQL 5.7 Docker command can't connect to Docker daemon How to find files modified in last x minutes (find -mmin does not work as expected) Can I use Homebrew on Ubuntu? Pycharm and sys.argv arguments Ubuntu: OpenJDK 8 - Unable to locate package Fork() function in C Amazon Linux: apt-get: command not found Ionic android build Error - Failed to find 'ANDROID_HOME' environment variable Ubuntu: Using curl to download an image Docker error response from daemon: "Conflict ... already in use by container" Curl command without using cache Can't get private key with openssl (no start line:pem_lib.c:703:Expecting: ANY PRIVATE KEY)

Questions with ubuntu tag:

grep's at sign caught as whitespace "E: Unable to locate package python-pip" on Ubuntu 18.04 How to Install pip for python 3.7 on Ubuntu 18? "Repository does not have a release file" error ping: google.com: Temporary failure in name resolution How to install JDK 11 under Ubuntu? How to upgrade Python version to 3.7? Issue in installing php7.2-mcrypt Install Qt on Ubuntu Failed to start mongod.service: Unit mongod.service not found When I run `npm install`, it returns with `ERR! code EINTEGRITY` (npm 5.3.0) Cannot open new Jupyter Notebook [Permission Denied] Can't install laravel installer via composer Yarn install command error No such file or directory: 'install' Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? How to completely uninstall python 2.7.13 on Ubuntu 16.04 How to update-alternatives to Python 3 without breaking apt? How to open the terminal in Atom? How to install/start Postman native v4.10.3 on Ubuntu 16.04 LTS 64-bit? How to install "ifconfig" command in my ubuntu docker image? How to install pip for Python 3.6 on Ubuntu 16.10? Default password of mysql in ubuntu server 16.04 How to install PHP intl extension in Ubuntu 14.04 Unable to set default python version to python3 in ubuntu updating nodejs on ubuntu 16.04 Docker Repository Does Not Have a Release File on Running apt-get update on Ubuntu Curl : connection refused How to install all required PHP extensions for Laravel? Getting permission denied (public key) on gitlab Cannot import keras after installation Docker - Ubuntu - bash: ping: command not found Copy Paste in Bash on Ubuntu on Windows How to install php-curl in Ubuntu 16.04 PHP XML Extension: Not installed webpack command not working sudo: docker-compose: command not found How to find which version of TensorFlow is installed in my system? How to fix git error: RPC failed; curl 56 GnuTLS How to tell if tensorflow is using gpu acceleration from inside python shell? Docker Error bind: address already in use MySQL fails on: mysql "ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded" How to use systemctl in Ubuntu 14.04 Package php5 have no installation candidate (Ubuntu 16.04) How to enable php7 module in apache? Is there a keyboard shortcut (hotkey) to open Terminal in macOS? To enable extensions, verify that they are enabled in those .ini files - Vagrant/Ubuntu/Magento 2.0.2 After installing with pip, "jupyter: command not found" How to verify if nginx is running or not? How to install Android SDK on Ubuntu? Docker-Compose can't connect to Docker Daemon

Questions with grep tag:

grep's at sign caught as whitespace cat, grep and cut - translated to python How to suppress binary file matching results in grep Linux find and grep command together Filtering JSON array using jQuery grep() Linux Script to check if process is running and act on the result grep without showing path/file:line How do you grep a file and get the next 5 lines How to grep, excluding some patterns? Fast way of finding lines in one file that are not in another? How to grep with a list of words How to get the process ID to kill a nohup process? Using the grep and cut delimiter command (in bash shell scripting UNIX) - and kind of "reversing" it? How do I find all files containing specific text on Linux? Checking if output of a command contains a certain string in a shell script Output grep results to text file, need cleaner output How to process each output line in a loop? Find and Replace string in all files recursive using grep and sed How to grep a string in a directory and all its subdirectories? How to concatenate multiple lines of output to one line? Display filename before matching line How to grep and replace How to perform grep operation on all files in a directory? PowerShell equivalent to grep -f Waiting for background processes to finish before exiting script Grep only the first match and stop grep from tar.gz without extracting [faster one] How to show grep result with complete path or file name How to use sed/grep to extract text between two words? How to give a pattern for new line in grep? How to grep recursively, but only in files with certain extensions? grep for special characters in Unix How to check if a file contains a specific string using Bash Delete all local git branches Find the line number where a specific word appears with "grep" Grep regex NOT containing string How to grep for contents after pattern? Using grep to search for a string that has a dot in it How to "grep" for a filename instead of the contents of a file? How to merge every two lines into one from the command line? More elegant "ps aux | grep -v grep" How do I use grep to search the current directory for all files having the a string "hello" yet display only .h and .cc files? Can I grep only the first n lines of a file? grep --ignore-case --only How to make GREP select only numeric values? Parsing json and searching through it Grep characters before and after match? Grep to find item in Perl array What are the differences among grep, awk & sed? How to show only next line after the matched one?