grep containing "-c" [closed] - bash

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I want to search for -c using grep
For example:
$>ls -al | grep '-c'
But grep thinks it is an option.
$>Usage: grep -hblcnsviw pattern file . . .
How can I search -c as a string?

Tell grep where your options end:
grep -- -c

You could either:
Escape the hyphen.
grep '\-c'
Use the -e (--regexp=) flag
grep -e -c
grep --regexp=-c # Not in POSIX, but supported at least in Linux and OS X.

You use the -e option:
$ ls -a1 | grep -e -c
This is of course mentioned in the documentation, thusly:
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-). (-e is specified by POSIX .)

To stay on topic, the answer to your original question is to use either -- to signal the end of options to process, or use -e to explicitly mark the option.
However, parsing the output of ls will produce an incorrect result if any of the file names contain a new line. Use find instead:
find . -depth 1 -name "-c"

Related

grep -v excludes a file it should not exclude [duplicate]

This question already has answers here:
Using grep to search for a string that has a dot in it
(9 answers)
Closed 5 years ago.
It seemed to me that grep -v displays the files that don't contain the following string.
How comes the file named highscore.txt doesn't appear when using grep -v ".c" ?
$ ls -1
a.out
easy.txt
hard.txt
highscores.txt
main.c
main.txt
util.c
$ ls -1 | grep -v ".c"
a.out
easy.txt
hard.txt
medium.txt
The ".c" in your grep command is a regular expression, and . means "any character".
To fix this, you can
Escape the period:
grep -v '\.c$'
I've added the "end of string" anchor $ to exclude false positives for files like something.cpp.
Use the -F option for "fixed strings":
grep -vF '.c'
Notice that this would also exclude something.cpp, which probably isn't what you want.
Use extended glob patterns to exclude anything ending in .c:
shopt -s extglob
ls -1 !(*.c)
Here, *.c is not a regular expression, but a glob pattern, where . is a literal period and has no special meaning.

bash shell tr -d -c options combination usage [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
echo $PATH | tr -d -c :
this output is:
::::
The value of $PATH is:
/import/adams/2/z1/bin-pc.i86.linux:/import/adams/2/z1/bin:/usr/local/bin:/usr/bin:/bin
why I get such an output ;;;;? I cannot understand -d -c :. -c option needs two sets, but -d needs only one set. which option is executed first? how does this result be generated?
Thanks.
$ p=/import/adams/2/z1/bin-pc.i86.linux:/import/adams/2/z1/bin:/usr/local/bin:/usr/bin:/bin
$ echo "$p" | tr -d -c :
::::
The -d option tells tr to delete characters.
The -c option says to use the complement of the character set that follows.
Because the character set which follows is :, everything except : is deleted. That is why you see the output that you see.
More examples
In the following, the character set consists of not just : but also /. Consequently, everything except : and / are deleted:
$ echo "$p" | tr -d -c :/
/////://///:///://:/
In the following, we omit -c and specify a character set of :. Consequently, all colons are deleted:
$ echo "$p" | tr -d :
/import/adams/2/z1/bin-pc.i86.linux/import/adams/2/z1/bin/usr/local/bin/usr/bin/bin
I cannot reproduce that you'd get ; in your output and not :, but you're asking tr to -delete everything that is in the -complement of :, so all non-: characters.
As man 1 tr says:
-c, -C, --complement
use the complement of SET1
-d, --delete
delete characters in SET1, do not translate

How to create dynamic substring with awk [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Let say i have file like below.
ABC_DEF_G-1_P-249_8.CSV
I want to cut to be like this below.
ABC_DEF_G-1_P-249_
I use this awk command to do that like below.
ls -lrt | grep -i .CSV | tail -1 | awk -F ' ' '{print $8}' | cut -c 1-18
Question is, if the number 1, is growing, how to make the substring is dynamic
example like below...
ABC_DEF_G-1_P-249_
....
ABC_DEF_G-10_P-249_
ABC_DEF_G-11_P-249_
...
ABC_DEF_G-1000_P-249_
To display the file names of all .CSV without everything after the last underscore, you can do this:
for fname in *.CSV; do echo "${fname%_*}_"; done
This removes the last underscore and evertyhing that follows it (${fname%_*}), and then appends an underscore again. You can assign that, for example, to another variable.
For an example file list of
ABC_DEF_G-1_P-249_9.CSV
ABC_DEF_G-10_P-249_8.CSV
ABC_DEF_G-1000_P-249_4.CSV
ABC_DEF_G-11_P-249_7.CSV
ABC_DEF_G-11_P-249_7.txt
this results in
$ for fname in *.CSV; do echo "${fname%_*}_"; done
ABC_DEF_G-1_P-249_
ABC_DEF_G-10_P-249_
ABC_DEF_G-1000_P-249_
ABC_DEF_G-11_P-249_
You can do this with just ls and grep
ls -1rt | grep -oP ".*(?=_\d{1,}\.CSV)"
If you are concerned about the output of ls -1, as mentioned in the comments you can use find as well
find -type f -printf "%f\n" | grep -oP ".*(?=_\d{1,}\.CSV)"
Outputs:
ABC_DEF_G-1_P-249
ABC_DEF_G-1000_P-249_
This assumes you want everything except the _number.CSV, if it needs to be case insensitive then you can the -i flag to the grep. The \d{1,} allows for the number between _ and .CSV to grow from one to many digits. Also doing it this way you don't have to worry about if the number 1 in your example increases:
ABC_DEF_G-1_P-249
You should not be parsing ls. Perhaps you are looking for something like this:
base=$(printf "%s\n" * | grep -i .CSV | tail -1 | awk -F ' ' '{print $8}' | cut -c 1-18)
However, that's a useless use of grep you want to get rid of right there -- Awk does everything grep does, and everything tail does, too, and actually, everything cut does as well. The grep can also be avoided by using a better wildcard, though:
base=$(printf "%s\n" *.[Cc][Ss][Vv] | awk 'END { print substr($8, 1, 18) }')
In the shell itself, you can do much the same thing with no external processes at all. Proposing a suitable workaround would perhaps require a better understanding of what you are trying to accomplish, though.

How to crop a word [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this list of inputs :
imalex
thislara
heiscarl
how to get :
alex
lara
carl
grep
Use grep to take the last four chars:
grep -o '.\{4\}$' file
The -o option makes sure only matched parts are printed.
sed
Using sed we can achieve a similar result:
sed 's/.*\(.\{4\}\)$/\1/' a
Here we capture the last four digits and replace each line with those last four digits. They are captured in a group \( \) and inserted \1.
read & tail
We can also grab the last five chars (including the newline) of each line using tail and a -c option. We do that for each line using read.
while read line; do
tail -c 5 <<< $line
done < file
2 answers using substring arithmetic
bash:
while read word; do
echo "${word:${#word}-4}"
done <<<"$list"
awk
echo "$list" | awk '{print substr($NF, length($NF)-4+1)}'

Invoke grep from a shell script and keep the colored patterns [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
For some mysterious reason, whenever I run grep from a "standalone" shell script as opposed to a simple function, the coloring of the output is not preserved.
Why is this happening and how can I prevent it?
This is best illustrated with an example:
You should try in your script :
grep --color
But please, no need to
echo `ls` | grep ".txt"
just
ls -1 | grep --color ".txt"
or
printf '%s\n' | grep --color ".txt"
See http://porkmail.org/era/unix/award.html
EDIT
To change the defautl colors of grep, see man grep and search GREP_COLORS

Resources