How to grep '---' in Linux? grep: unrecognized option '---'
This happens because grep
interprets ---
as an option instead of a text to look for. Instead, use --
:
grep -- "---" your_file
This way, you tell grep
that the rest is not a command line option.
Other options:
use
grep -e
(see Kent's solution, as I added it when he had already posted it - didn't notice it until now):use
awk
(see anubhava's solution) orsed
:sed -n '/---/p' file
-n
prevents sed
from printing the lines (its default action). Then /---
matches those lines containing ---
and /p
makes them be printed.
Grep unrecognized option '--' while parsion content of html element
The line
response=$(curl ...)
puts the output of the curl
command in a variable named response
.
In your grep
command you try to pass the expansion of the variable as an argument.
output=$(grep -o '<div class="error-icon">[^<]*' "$response" | ...)
grep
tries to interpret the value as command line arguments which may result in various errors depending on the actual output. In my test I got a message grep: <some html code>: File name too long
because it tries to interpret this as a file name argument.
You should save the data in a file and pass this to grep
. Adapt the name and location of the temporary file as necessary. Example:
#!/bin/sh
verifyCard=$1
if [ -z "${verifyCard}" ]; then echo "No argument supplied"; exit 1; fi
curl -o response-file 'https://www.isic.org/verify/' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/x-www-form-urlencoded' -H 'Origin: https://www.isic.org' -H 'Connection: keep-alive' -H 'Referer: https://www.isic.org/verify/' -H 'Cookie: PHPSESSID=46plnh6b31e2pusv1do6thfbm7; AWSELB=AF73034F18B38D7DCED6DEDC728D31BA3F3A73F96747FEE7FA7C4F7A74BC9954E5928CBDDD5C053FFB2A37CE37136C4BA008B15163192B34CFA04D35BEC4ED0D0D2913A2FB; AWSELBCORS=AF73034F18B38D7DCED6DEDC728D31BA3F3A73F96747FEE7FA7C4F7A74BC9954E5928CBDDD5C053FFB2A37CE37136C4BA008B15163192B34CFA04D35BEC4ED0D0D2913A2FB; _ga=GA1.2.650910486.1600495658; _gid=GA1.2.731428038.1600495658; _gat=1' -H 'Upgrade-Insecure-Requests: 1' --data-raw 'verify_card_number=${$verifyCard}'
output=$(grep -o '<div class="error-icon">[^<]*' response-file | grep -o '[^>]*$')
rm response-file
echo "$output"
If you use bash
or zsh
instead of sh
, there are ways to substitute some variable value as an input file, see e.g. the answers in using a Bash variable in place of a file as input for an executable.
grep -r string * error: unrecognized option `--DIRAC3LE--'?
Your shell is probably expanding * and there is a '--DIRAC3LE--' in the working directory. grep
is then confusing the leading -- with command line options.
Try using
grep -r "string" .
This will recursively search for everything in the working directory.
Also try using "--" before the *. POSIX commands use this to indicate the end of the command line options to prevent such ambiguity (see comments for a reference).
Match from beginning to word as long as there are no . in between: Convert grep -Po command to sed
You can use
awk -F'.' '$2 == "enabled"{print $1}' file
sed -n 's/^\([^.]*\)\.enabled.*/\1/p' file
See the online demo.
Details:
awk:
-F'.'
- the field separator is set to a.
$2 == "enabled"
- if Group 2 value isenabled
, then{print $1}
- print Field 1 value
sed:-n
- suppresses default line output in thesed
commands/^\([^.]*\)\.enabled.*/\1/p
- finds any zero or more chars other than.
at the start of string (placing them into Group 1,\1
), then a.enabled
and then the rest of the string and replaces with the Group 1 value, andp
rints the resulting value.
grep command not accepting a string with multiple options in my script
Do not double quote multiple options/flags into as one option. When you quote a string grep
will treat it as single string.
For example:
Valid:
grep -ins --color=auto "foo" inputfile
Valid:
grep "-ins" "--color=auto" "foo" inputfile
Invalid:
grep "-ins --color=auto" "foo" inputfile
Because there is no single option as "-ins --color=auto"
grep search for -- or -- gives unrecognised or invalid option
Well, dashes start options, so it's only natural grep
tries to interpret that as one. You'd have the same problem if searching for -R
.
Use -e
:
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. If this option is used multiple times or is combined with the
-f
(--file
) option, search for all patterns given. This
option can be used to protect a pattern beginning with “-
”.
Related Topics
Identify Other End of a Unix Domain Socket Connection
Remove Log Files Using Cron Job
How to Get the Contents of a Webpage in a Shell Variable
How to Emulate the Raspberry Pi 2 on Qemu
Awk One Liner Select Only Rows Based on Value of a Column
Using Find - Deleting All Files/Directories (In Linux ) Except Any One
Apt-Get Error: Sub-Process /Usr/Bin/Dpkg Returned an Error Code (1)
How to Determine If Code Is Running in Signal-Handler Context
How to Check Status of Urls from Text File Using Bash Shell Script
Bluetoothctl to Hcitool Equivalent Commands
How to Control Backlight by Terminal Command
Best Approach to Writing a Generic Installer for a Linux Application
How to Find Binary Files in a Directory
Linux Kernel System Call Returns -1 Instead of {-1, -256}