Pattern matching with grep [duplicate] - linux

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 9 years ago.
I have a full path of a file say hai/hello/home/something/file.txt .How can I get file.txt as output eliminating full path?
How to do that with grep?

#!/usr/bin/perl
use File::Spec;
use File::Basename;
$n="hai/hello/home/something/file.txt";
my $m = basename $n;
print "$m";

You don't strictly need grep for this, but if you insist, this should work:
grep -o -e "\w*\.\w*$"
Optionally, consider the command basename:
basename hai/hello/home/something/file.txt

You can do it using sed:
$ echo hai/hello/home/something/file.txt | sed "s|.*/||g"
file.txt
or, easier, basename:
$ basename hai/hello/home/something/file.txt
file.txt

Related

How to use sed and egrep in bash shell script [duplicate]

This question already has an answer here:
shell variable in a grep regex
(1 answer)
Closed 4 years ago.
I am writing a bash shell script in which I want to use two shell commands sed and egrep.
My bash shall script read a text file q2.txt and then do some actions using egrep and sed.
The code is as given below.
#!/bin/bash
var=$(<q2.txt)
sed "s/^[ \t]*//" -i var
grep -v '^/\*.*\*/$' var
echo "$var"
I read the content of q2.txt in variable var. Then remove the tabs and spaces using sed s/^[ \t]*//" -i var and update my var.
Then execute grep -v '^/\*.*\*/$' var on my updated variable to select some lines with specific start and end.
But in the ouput, It seems like grep and sed are not applicable to var.
Output
sed: can't read var: No such file or directory
grep: var: No such file or directory
Here is a much simpler approach
cat q2.txt | sed "s/^[ \t]*//" | grep -v '^/\*.*\*/$'
The problem is more about shell variables, rather than sed/grep.
See:
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
http://wiki.bash-hackers.org/syntax/pe
Also better to use the Pipe (|) as the other answer has showed, so you don't have to store the intermediate result.

Unable to use sed to replace text with shell variable [duplicate]

This question already has answers here:
Environment variable substitution in sed
(12 answers)
Closed 5 years ago.
For some reason, the answer in the post below doesn't work for me. Any thoughts?
how to use sed to replace a string in a file with a shell variable
I'm running CentOS 6.5
`NEW="new value"
cat myfile.txt | sed -e 's/old/${NEW}' <-- just replaces 'old' with '${NEW}'
cat myfile.txt | sed -e 's/old/$NEW' <-- just replaces 'old' with '$NEW'
cat myfile.txt | sed -e "s/old/${NEW}" <-- gives the error: unknown option to `s'
try taking the 's off the sed e.g
$ new=N
$ cat > j
one
two
three
$ sed -e "s/one/${new}/" j
N
two
three
for a more complete answer try this answer

How to remove "-" and a space from the beginning in a bash script? [duplicate]

This question already has answers here:
Editing/Replacing content in multiple files in Unix AIX without opening it
(2 answers)
Closed 6 years ago.
I have an output that looks as below
- 0.1-1
- 0.1-2
- 0.1-3
- 0.1-6
- 0.1-7
- 0.1-9
How to use grep or something else so as to remove "-" and a space from the beginning.
0.1-1
0.1-2
0.1-3
0.1-6
0.1-7
0.1-9
With sed:
sed -e 's/^- //' input.txt
Or with GNU grep:
grep -oP '^- \K.*' input.txt
You may use grep also,
grep -oE '[0-9].*' file
With awk:
awk '{print $2}' file
You can use cut to remove the first two columns of every line:
cut -c3- input.txt

How to grep for specific pattern in a file [duplicate]

This question already has answers here:
Can grep show only words that match search pattern?
(15 answers)
Closed 8 years ago.
I have a bash file that has below line along with other lines.
var BUILD_VERSION = '2014.17.10_23';
I just want to extract 2014.17.10_23 and this value may change so something like grep for 2014* . However when I do that I get the whole line returned instead of the value 2014.17.10_23.
What would be the best way to achieve this?
Thanks
Using awk:
awk -F= '/BUILD_VERSION/{print $2}' input | tr -d "[' ;]"
And with sed:
sed -n "/BUILD_VERSION/s/.*'\([^']*\)'.*/\1/p" input
grep 'BUILD_VERSION' <your file> | sed -e 's/var BUILD_VERSION = //g'
Would get you '2014.17.10_23'; tweak the sed expression (or pipe it through a few more) to get rid of quotes.
It would be a 1 liner regex in Perl...
Here is another awk solution:
awk -F' = ' '/BUILD_VERSION/ {gsub(/\x27|;/,""); print $NF}'
You can use this awk
awk -F\' '/BUILD_VERSION/ {print $2}' file
2014.17.10_23

How do I get sed to read from standard input? [duplicate]

This question already has answers here:
sed unknown option to `s' in bash script [duplicate]
(4 answers)
Closed 2 years ago.
I am trying
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
and getting
unknown option to `s'
What am I doing wrong?
Edit:
As per the comments the code is actually correct. My full code resembled something like the following
grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
# my comment
And it appears that for some reason my comment was being fed as input into sed. Very strange.
use the --expression option
grep searchterm myfile.csv | sed --expression='s/replaceme/withthis/g'
use "-e" to specify the sed-expression
cat input.txt | sed -e 's/foo/bar/g'
To make sed catch from stdin , instead of from a file, you should use -e.
Like this:
curl -k -u admin:admin https://$HOSTNAME:9070/api/tm/3.8/status/$HOSTNAME/statistics/traffic_ips/trafc_ip/ | sed -e 's/["{}]//g' |sed -e 's/[]]//g' |sed -e 's/[\[]//g' |awk 'BEGIN{FS=":"} {print $4}'
If you are trying to do an in-place update of text within a file, this is much easier to reason about in my mind.
grep -Rl text_to_find directory_to_search 2>/dev/null | while read line; do sed -i 's/text_to_find/replacement_text/g' $line; done
Open the file using vi myfile.csv
Press Escape
Type :%s/replaceme/withthis/
Type :wq and press Enter
Now you will have the new pattern in your file.

Resources