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

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

Related

grep an element in a df and display only selected columns with bash [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hel lo I have a df file such as
col1;col2;col3;col4
A;B;C;D
E;F;G;H
I;J;K;L
and I would like to grep I and only display the col1 and col2
and get
I;J then
because from now I only know how to do :
grep 'I' df.csv
I;J;K;L
Try this:
grep 'I' df.csv | cut -d';' -f1-2
The cut command will treat each input line as a list of fields separated by ; (-d';'), and will select only the first two fields (-f1-2) for output.
Sample session:
$ cat df.csv
col1;col2;col3;col4
A;B;C;D
E;F;G;H
I;J;K;L
$ grep 'I' df.csv | cut -d';' -f1-2
I;J
$

How do I print out the longest word in a file that appears at least 10 times [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Exactly what the title says: what is the bash command for printing out the longest word in a text file that appears at least 10 times.
Try this Denis:
tr -s " " "\n" < file | while read -r l; do echo "${#l} $l"; done | sort -n | awk '$1 >= 10 ' | awk '{print $2}' | tail -n1

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)}'

How to print running commands for connected users in shell? [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 8 years ago.
Improve this question
ps -ef | grep --color "ilir1477" | awk '{print $NF}'
Please explain to me.
Is this what you are after?
$ ps -u ilir1477 -o args=

grep containing "-c" [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 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"

Resources