I have a list of strings in a file called users.txt.
For instance, this file contains a list of username separated by a new line:
Jhon
Paul
Mark
Harry
I have to grep over this list of strings in order to extract from another file (called myLog.out) the lines containing that usernames.
To achive my target I'm using the following command:
grep -wFf users.txt myLog.out > out.txt
Now, my out.txt only contains the occurrences of the last name of the users.txt file, that is:
2017-12-17 15:13:41,191 [13] INFO it.test.Service- user: Harry
2017-12-17 15:13:41,191 [13] INFO it.test.Service- user: Harry
2017-12-17 15:13:41,191 [13] INFO it.test.Service- user: Harry
There are occurrences of Jhon and Paul in myLog.out file but I'm not able to retrive them using the previous grep command.
Related
I am trying to extract a sub-string from each line in a file between 2 groups of characters :
each line in the input file is :
https://github.com/myname/repo1 | GitHub - repo description
https://github.com/myname/repo2 | GitHub - repo description
https://github.com/myname/repo3 | GitHub - repo description
....
https://github.com/myname/repoN | GitHub - repo description
I extract the sub-string between "https://github.com/" and " | GitHub" to get :
myname/repo1
myname/repo2
myname/repo3
...
myname/repoN
And I use GNU grep. :
grep -nPo 'github.com\/\K.*?(?= \|)' ~/Desktop/forksonGithub.txt
This displays the correct list in the console with line number
1:myname/repo1
2:myname/repo2
3:myname/repo3
...
4:myname/repoN
how can I get this list in an output file without the lin numbers ?
thanks for feedback
The option -n is responsible for the line numbers. You just need to remove it:
grep -Po 'github.com\/\K.*?(?= \|)' ~/Desktop/forksonGithub.txt
myname/repo1
myname/repo2
myname/repo3
...
myname/repoN
I am passing all my svn commit log messages to a file and want to grep only the JIRA issue numbers from that.
Some lines might have more than 1 issue number, but I want to grab only the first occurrence.
The pattern is XXXX-999 (number of alpha and numeric char is not constant)
Also, I don't want the entire line to be displayed, just the JIRA number, without duplicates. I use the following command but it didn't work.
Could someone help please?
cat /tmp/jira.txt | grep '^[A-Z]+[-]+[0-9]'
Log file sample
------------------------------------------------------------------------
r62086 | userx | 2015-05-12 11:12:52 -0600 (Tue, 12 May 2015) | 1 line
Changed paths:
M /projects/trunk/gradle.properties
ABC-1000 This is a sample commit message
------------------------------------------------------------------------
r62084 | usery | 2015-05-12 11:12:12 -0600 (Tue, 12 May 2015) | 1 line
Changed paths:
M /projects/training/package.jar
EFG-1001 Test commit
Output expected:
ABC-1000
EFG-1001
First of all, it seems like you have the second + in the wrong place, it should be at the end of [0-9] expression.
Second, I think all you need to do this is use the -o option to grep (to display only the matching portion of the line), then pipe the grep output through sort -u, like this:
cat /tmp/jira.txt | grep -oE '^[A-Z]+-[0-9]+' | sort -u
Although if it were me, I'd skip the cat step and just give the filename to grep, as so:
grep -oE '^[A-Z]+-[0-9]+' /tmp/jira.txt | sort -u
Six of one, half a dozen of the other, really.
I want to print all lines in a tomcat catalina.out log containing xxx. A simple thing to accomplish using:
cat catalina.out | grep xxx
However. In the logfile I get the lines containing xxx, the line above this line is containing the date and time when the item was logged. I would like to see those lines above the grepped lines too. How could I accomplish this?
grep -B1
-B[n] lets you see [n] lines before the pattern that you are looking for.
You can also use -A for 'lines after', and -C for 'context' (lines both above and below).
You can also simplify your grep call and remove the pipe with grep xxx -B1 catalina.out.
I'm using grep to found matching lines from a file in two different files. It finds the matching files just fine from File1 into File2 and File3, but from the moment there is more than one file, it prints the file name in which it was found next to the line.
grep -w -f File1 File2 File3
Output:
File2: pattern
File2: pattern
File3: pattern
Is there an option to avoid the print of File2: and File3:?
grep --no-filename -w -f File1 File2 File3
If you're on a UNIX system, please refer to the man pages. Whenever you encounter a problem, your first step should be man $programName. In this case, man grep. It appears that you want the "-h" option. Here's an excerpt from the man page:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
I have a large file where each line contains a substring such as ABC123. If I execute
grep ABC file.txt
or
grep ABC1 file.txt
I get those lines back as expected, but if I execute
grep ABC12 file.txt
grep fails to find the corresponding lines.
This seems pretty trivial functionality, but I'm not a heavy user of grep so perhaps I'm missing some gotcha.
Use something like
od -x -a < filename
to dump out the file contents in hex. That'll immediately show you if what you have in your file is what you expect. Which I suspect it isn't :-)
Note: od has lots of useful options to help you here. Too many to list, in fact.
Is there a chance your file contains some hidden character, such as 0x00 ?
This doesn't make sense. Are you sure the file contains "ABC123"?
You can verify this by running following command in a shell
echo "ABC123" | grep ABC12
If the lines contain ABC123, then "grep ABC12" should get them. Do you perhaps mean that you want to match several different strings, such as ABC1, ABC2 and ABC3? In that case you can try this:
grep -E 'ABC1|ABC2|ABC3'
I'm not sure what the problem is.. grep works exactly as it should.. For example, the contents of my test file:
$ cat file.txt
ABC
ABC1
ABC12
ABC123
..and grep'ing for ABC, ABC1, ABC12, ABC123:
$ grep ABC file.txt
ABC
ABC1
ABC12
ABC123
$ grep ABC1 file.txt
ABC1
ABC12
ABC123
$ grep ABC12 file.txt
ABC12
ABC123
$ grep ABC123 file.txt
ABC123
grep is basically a filter, any line containing the first argument (ABC, or ABC1 etc) will be displayed. If it doesn't contain the entire string, it will not be displayed