Grep and print only matching word and the next words - linux

I have a text file, install.history
Wed June 20 23:16:32 CDT 2014, EndPatch, FW_6.0.0, SUCCESS
I would only need to print out the word starting from EndPatch to the end that is FW_6.0.0, SUCCESS
The command below that I have only prints out EndPatch, so what do I need to do so that it prints out the remaining of the words so that my result would be:
EndPatch, FW_6.0.0, SUCCESS
Here is the command that I have:
grep -oh "EndPatch[[:alpha:]]*" 'install.history'

This is arguably, easier to do with sed:
sed -n 's/.*EndPatch, //p' install.history
to get the word after EndPatch:
sed -n 's/.*EndPatch, \([^,]*\).*/\1/p' install.history
or:
sed -n 's/.*EndPatch, //p' install.history | cut -d, -f

You could try the below grep command to get everything from EndPatch upto the last,
grep -oP 'EndPatch, (.*)$' file
or
grep -o 'EndPatch.*$' file
Example:
$ grep -oP 'EndPatch, (.*)$' file
EndPatch, FW_6.0.0, SUCCESS
$ grep -o 'EndPatch.*$' file
EndPatch, FW_6.0.0, SUCCESS
Or
You could try the below command to get all the characters which was just after to EndPatch,
$ grep -oP 'EndPatch, \K(.*)$' file
FW_6.0.0, SUCCESS

You need grep -e for regular expression matching
grep -oh 'install.history' -e "EndPatch[[:alpha:]]*"

You can use awk
awk -F "EndPatch, " '{print FS$2}' file
EndPatch, FW_6.0.0, SUCCESS

Related

grep a word from a list of file as a result of grep before

I have a command to grep a file with fullpath that contain a "TypeId: 0", here is the command
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'
and here is the result:
/home/username/app/data/store/0/part/.mv/521/1673332792072/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/521/1673333077920/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/515/1672993850766/segmentconfig.yaml.old /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml /home/username/app/data/store/0/part/.mv/703/1672987004847/segmentconfig.yaml.old
Now I confuse how to grep "numofvertice" from each file from that list.
Anyone have an idea to solve this?
You could try this:
grep -rnw /home/username/app/data/store/0/part/.mv -e "TypeId: 0" | awk -F ":" '{print $1}'|xargs -I{} grep "numofvertice" {}
Like this (GNU grep):
<STDIN> | grep -oP '\b\S+\.yaml' | xargs cat
Or with ack:
cd /home/username/app/data/store/0/part/.mv
ack -wl -e "TypeId: 0" | xargs cat
From ack --help:
-l, --files-with-matches
Only print filenames containing matches

Bash issue with floating point numbers in specific format

(Need in bash linux)I have a file with numbers like this
1.415949602
91.09582241
91.12042924
91.40270349
91.45625033
91.70150341
91.70174342
91.70660043
91.70966213
91.72597066
91.7287678315
91.7398645966
91.7542977976
91.7678146465
91.77196659
91.77299733
abcdefghij
91.7827827
91.78288651
91.7838959
91.7855
91.79080605
91.80103075
91.8050505
sed 's/^91\.//' file (working)
Any way possible I can do these 3 steps?
1st I try this
cat input | tr -d 91. > 1.txt (didnt work)
cat input | tr -d "91." > 1.txt (didnt work)
cat input | tr -d '91.' > 1.txt (didnt work)
then
grep -x '.\{10\}' (working)
then
grep "^[6-9]" (working)
Final 1 line solution
cat input.txt | sed 's/\91.//g' | grep -x '.\{10\}' | grep "^[6-9]" > output.txt
Your "final" solution:
cat input.txt |
sed 's/\91.//g' |
grep -x '.\{10\}' |
grep "^[6-9]" > output.txt
should avoid the useless cat, and also move the backslash in the sed script to the correct place (and I added a ^ anchor and removed the g flag since you don't expect more than one match on a line anyway);
sed 's/^91\.//' input.txt |
grep -x '.\{10\}' |
grep "^[6-9]" > output.txt
You might also be able to get rid of at least one useless grep but at this point, I would switch to Awk:
awk '{ sub(/^91\./, "") } /^[6-9].{9}$/' input.txt >output.txt
The sub() does what your sed replacement did; the final condition says to print lines which match the regex.
The same can conveniently, but less readably, be written in sed:
sed -n 's/^91\.([6-9][0-9]\{9\}\)$/\1/p' input.txt >output.txt
assuming your sed dialect supports BRE regex with repetitions like [0-9]\{9\}.

printing "grep -o" output in single line

How to print output of grep -o in a single line ? I am trying to print :
$ echo "Hello Guys!" |grep -E '[A-Z]'
Hello Guys!
$ echo "Hello Guys!" |grep -Eo '[A-Z]' <----Multiple lines
H
G
$ echo "Hello Guys!" |grep -Eo '[A-Z]'
Desired output:
HG
I am able to cheaply achieve it using following command ,but the issue is that number of letters(3 in this case) could be dynamic. So this approach cannot be used.
echo "HEllo Guys!" |grep -oE '[A-Z]' |xargs -L3 |sed 's/ //g'
HEG
You could do it all with this sed instruction
echo "Hello Guys!" |sed 's/[^A-Z]//g'
UPDATE
Breakdown of sed command:
The s/// is sed's substitute command. It simply replaces the first RegEx (the one between the first and the second slash) with the Expression between slash two and three. The trailing g stands for global, i.e, do this for every match of the RegEx in the current line. Without the g it would just stop processing after the first match. The RegEx itself is matching any non-capital letter and then those letters are replaced with nothing, i.e., effectively deleted.
You can use awk:
echo "Hello Guys!" | awk '{ gsub(/[^A-Z]/,"", $0); print;}'
HG
Also with tr:
echo "Hello Guys!" | tr -cd [:upper:]
HG
Also with sed :
echo "Hello Guys!" | sed 's/[^\[:upper:]]//g'
HG
You just need to remove the newline characters. You can use tr for that:
echo "HEllo Guys!" |grep -Eo '[A-Z]' |tr -d '\n'
HEG
Though, it cuts the last newline too.
You can use perl instead of grep
echo 'HEllo Guys!' | perl -lne 'print /([A-Z])/g'
HEG

grep only one occurrence

I am trying to grab some content, but there are multiple instances of it in the same line. I am using this command.
grep -o -m 1 -P '(?<=sk).*(?=fa)' test.txt | head -1
However, the search ends after the second/last match. Running it on Ubuntu 14.04.2
test.txt: skjahfasdkl aklsdj laks skjahfasdkl aklsdj laks
Current Output: jahfasdkl aklsdj laks skjah
Desired output: jah
You just need non-greedy:
grep -m1 -oP '(?<=sk).*?(?=fa)' file | head -1
# ...................^^^
The -m1 will stop after the first line, but you still need head to limit to the first match.
It's greedy match, you want to treat space as delimiters so specify the match to nonspace chars, i.e.
... '(?<=sk)[^ ]*(?=fa)'
if the condition is non spaces between sk and fa ( matching in words ), you can use can use [^ ]* instead .*, as the following:
grep -o -m 1 -P '(?<=sk)[^ ]*(?=fa)' test.txt | head -1
else you can use this :
sed -e "s/sk\(.*\)fa.*$/\1/g" test.txt | sed -e "s/fa.*$//g"
test :
echo "skjahz z zfasdkl aklsdj laks skjahppppfasdkl aklsdj laks" | sed -e "s/sk\(.*\)fa.*$/\1/g" | sed -e "s/fa.*$//g"
#jahz z z
If you consider non-grep answer then this gnu-awk can do the job:
awk -v FPAT='sk[^[:blank:]]*fa' '{gsub(/^sk|fa$/, "", $1); print $1; exit}' file

Get line number while using grep

I am using grep recursive to search files for a string, and all the matched files and the lines containing that string are print on the terminal. But is it possible to get the line numbers of those lines too??
ex: presently what I get is /var/www/file.php: $options = "this.target", but what I am trying to get is /var/www/file.php: 1142 $options = "this.target";, well where 1142 would be the line number containing that string.
Syntax I am using to grep recursively is sudo grep -r 'pattern' '/var/www/file.php'
One more question is, how do we get results for not equal to a pattern. Like all the files but not the ones having a certain string?
grep -n SEARCHTERM file1 file2 ...
Line numbers are printed with grep -n:
grep -n pattern file.txt
To get only the line number (without the matching line), one may use cut:
grep -n pattern file.txt | cut -d : -f 1
Lines not containing a pattern are printed with grep -v:
grep -v pattern file.txt
If you want only the line number do this:
grep -n Pattern file.ext | gawk '{print $1}' FS=":"
Example:
$ grep -n 9780545460262 EXT20130410.txt | gawk '{print $1}' FS=":"
48793
52285
54023
grep -A20 -B20 pattern file.txt
Search pattern and show 20 lines after and before pattern
grep -nr "search string" directory
This gives you the line with the line number.
In order to display the results with the line numbers, you might try this
grep -nr "word to search for" /path/to/file/file
The result should be something like this:
linenumber: other data "word to search for" other data
When working with vim you can place
function grepn() {
grep -n $# /dev/null | awk -F $':' '{t = $1; $1 = $2; $2 = t; print; }' OFS=$':' | sed 's/^/vim +/' | sed '/:/s// /' | sed '/:/s// : /'
}
in your .bashrc and then
grepn SEARCHTERM file1 file2 ...
results in
vim +123 file1 : xxxxxxSEARCHTERMxxxxxxxxxx
vim +234 file2 : xxxxxxSEARCHTERMxxxxxxxxxx
Now, you can open vim on the correspondending line (for example line 123) by simply copying
vim +123 file1
to your shell.

Resources