extracting the values using grep - linux

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.

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\}.

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"

How get value from text file in linux

I have some file xxx.conf in text format. I have some text "disablelog = 1" in this file.
When I use
grep -r "disablelog" oscam.conf
output is
disablelog = 1
But i need only value 1.
Do you have some idea please?
one way is to use awk to print just the value
grep -r "disablelog" oscam.conf | awk '{print $3}'
you could also use sed to replace diablelog = with empty
grep -r 'disablelog' oscam.conf | sed -e 's/disablelog = //'
If you also want to get the lines with or without space before and after = use
grep -r 'disablelog' oscam.conf | sed 's/disablelog\s*=\s*//'
above command will also match
disablelog=1
Assuming you need it as a var in a script:
#!/bin/bash
DISABLELOG=$(awk -F= '/^.*disablelog/{gsub(/ /,"",$2);print $2}' /path/to/oscam.conf)
echo $DISABLELOG
When calling this script, the output should be 1.
Edit: No matter wether there is whitespace or not between the equals sign and the value, the above will handle that. The regex should be anchored in either way to improve performance.
Try:
grep -r "disablelog" oscam.conf | awk -F= '{print $2}'
Just for fun a solution without awk
grep -r disablelog | cut -d= -f2 | xargs
xargs is used here to trim the whitespace

extracting-the-values-using-grep

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

Resources