Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

grep differences

grep -C20 123x789 *
what is difference between above and below command
 grep 123x789 *

what -C20 means here. prints 20 additional bottom lies wherever it find 123x789?

please advise

if i have to print both bottom and top 30 lines what command i have to give?
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

You should really use 'man' for questions like these.

>> what -C20 means here.
    -C[num, --context=num]
             Print num lines of leading and trailing context surrounding each match.  The default is 2 and is equivalent to -A 2 -B 2.  Note:
             no whitespace may be given between the option and its argument.


>> if i have to print both bottom and top 30 lines what command i have to give?
head -30 yourfile ; tail -30 yourfile

(there is no single command to do that)
Avatar of skullnobrains
skullnobrains

"grep -B 30 -A 30" if what you actually want is 30 lines above and below the match. ( see man page indeed )
Hi,

Displays N lines around match

-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
Avatar of gudii9

ASKER

Displays N lines around match

-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.

$ grep -C 2 "Example" demo_text

which one shows around
which one shows both before and after


what is A and B here?

any example on this?

any good link or vidoe or resource to understand what it means by match

Default 2 means does it shows both abouve 2 lines and below 2 lines?
Showing both before and after is the same as around.
grep -C 30
is the same as
grep -A 30 -B 30

-A  (After)
-B (Before)
-C ( same as -A and -B combined)

If you do just do
grep -C

There's an implied 2 automatically.
-C is the option which prints the specified N lines before the match.

this is plain wrong and only added confusion to the thread. the rest of same post does not seem very meaningful either.

i recommend
accept :  https://www.experts-exchange.com/questions/29084068/grep-differences.html?anchorAnswerId=42469374#a42469374  ( the very first post though the very last sentence between brackets is actually wrong )
and possibly accept serialband's  https://www.experts-exchange.com/questions/29084068/grep-differences.html?anchorAnswerId=42580132#a42580132 as an assisted solution. ( nice and clear explanation, btw )

sorry prabhin, and sorry mods. this is nothing personal. i'm just tired of seeing some "experts" switching from accepting  their own ( often poor )  answers to including them in some batch accept without reviewing them.

as an example, here is my own post's review : i did not trust gudi's phrasing and thought his question was 100% context relative. at than moment, adding a precision seemed right. reviewing it, my understanding of gudi's need may have been wrong, and i do not believe it added much useful information ( if any given the laconic effortless style ). if i had the same problem, would i need to read that answer ? likely not => not even worth an assist.
I second skullnobrains closure suggestion.

About my remark in brackets (first comment) - I read it as a request to show the bottom 30 and top 30 lines of a file. I know of no one command to do that. Feel free to correct.
Avatar of gudii9

ASKER

any good example link or resource around it


lets say my file is
xyz.log

it has below content



first
second
third
fourth
fifth
sixth
seventh
eight


if i grep

grep -C2 'fourth' xyx.log

i get below out put?

second
third
fifth
sixth


if i have to get below how i have to tweak my grep?(i.e above matching word)
second
third
if i have to get below how i have to tweak my grep?(i.e below matching word)
fifth
sixth

grep fourth xyz.log

above gives below output?

fourth


when we go for exact match when we go for around match when we go for above match when we go for below match

please advise
What is the exact version of Linux/Unix, shell and grep you are using?
Avatar of gudii9

ASKER

how to check it? please advise
grep --version
echo $0
uname -a
I would think your file is broken.

Try following:

F=sometestfile
echo first > $F
echo second >> $F
echo third >> $F
echo fourth >> $F
echo fifth >> $F
echo sixth >> $F
echo seventh >> $F
grep -C 1 fourth $F
grep -A 1 fourth $F
grep -B 1 fourth $F

Open in new window

(copy/paste into shell and press enter)

Besides commands you see, your expected output is:

third
fourth
fifth

fourth
fifth

third
fourth

If you get anything different, copy/paste full output you get here.
gudii9 - a simple question - did you actually read the man page on the grep command? I ask this because in my first post I refer to the man page that explains very clear what A B and C mean. Yet in your first post you ask about C and what A and B are -> that is all in the man page.

Observation: it looks like you are not reading/following up on expert's replies to your questions. If this observation is correct then this could lead to some experts ignoring your questions in the future.
I know of no one command to do that. Feel free to correct.

off topic and for the fun and sake of discussion, see the following example for the first and last 3 lines using sed. ( there is only a single sed : the for loop merely generates 100 lines of input ). i agree this is ugly ;)

for i in `seq 1 100 ` ; do echo $i ; done | sed -n -e '1,3 p ; H ; x ; s/.*\n\(.*\n.*\n.*\)$/\1/ ; x ; $ {x;p}'

best regards
@skullnobrains - Sure, just for fun - if you had one operation in your sed command line I would see it as one 'command' - you're just writing a small program here, same as doing it with awk - t his is technically one awk command:

awk -v nl="$(cat test.txt | wc -l)" '(FNR <= 3 || FNR > nl-3) {print}' test.txt

Good discussion though.
@gj
agreed, though i believe we're closer with sed script ( non turing-complete and flow based ) than with a command interpreter. even if "headtail -n1:30,-30:30" or a similar command existed, one can argue "30,-30:30" is a script... where do we draw the line then ? this is quite subjective.

thanks for the talk. see you around the threads
As the author has not responded yet to our latest comments, I  stay with the previous close advice by skullnobrains  which is:

https:#a42469374 (answer)
https:#a42580132 (assist)

After the close advice there is some more nice discussion but getting repetitive when author basically asks the same again which is answered again https:#a42595605

To be honest, I'm seriously thinking of not answering questions like these again (you may remove this line if you like Moderato).
Avatar of gudii9

ASKER

any video tutorial on -C20 or _C5 etc option with grep

Please advise as i am not able to follow anything
Where's your linux test box/VM?  Run the commands in it on a test file to observe.  It's difficult to learn the command line without a command line environment to run examples.
i'd advise you simply select a file with contents you know and play with the grep command for about 5mn. this is trivial to figure out through empiric trial ( and error ), and i'm unsure anyone bothered making a video tutorial for this. if you need help from our side : post an example of an input file, your command, and state what you expected as a result or why the result you got does not look like what you want. and btw, i hardly ever use -C ( if at all ) in real-life.
Avatar of gudii9

ASKER

let me try
Avatar of gudii9

ASKER

any good example, complete link on this

please advise
Avatar of gudii9

ASKER

F=sometestfile
echo first > $F
echo second >> $F
echo third >> $F
echo fourth >> $F
echo fifth >> $F
echo sixth >> $F
echo seventh >> $F
grep -C 1 fourth $F
grep -A 1 fourth $F
grep -B 1 fourth $F

Open in new window


how to practice above at below link

https://www.tutorialspoint.com/execute_bash_online.php
i get below error



echo first > $F
echo second >> $F
echo third >> $F
echo fourth >> $F
echo fifth >> $F
echo sixth >> $F
echo seventh >> $F

when i execute above

$bash -f main.sh
main.sh: line 1: $F: ambiguous redirect
main.sh: line 2: $F: ambiguous redirect
main.sh: line 3: $F: ambiguous redirect
main.sh: line 4: $F: ambiguous redirect
main.sh: line 5: $F: ambiguous redirect
main.sh: line 6: $F: ambiguous redirect
main.sh: line 7: $F: ambiguous redirect
BashErr.png
You left out the first line in your example F=sometestfile

Coding and scripts are very specific.
Avatar of gudii9

ASKER

it worked now

grep -C 2 fourth $F
grep -A 1 fourth $F
grep -B 1 fourth $F

what is difference between above 3 commands one with A and one with B and other with C
what is meaning of
F=sometestfile

is it creating temporary file called sometestfile?
then adding data to that file using the echo?

Please advise
grepC2.png
Avatar of gudii9

ASKER

now i finally i see what -C 3 means practically
grepC3.png
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial