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

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.

Related

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 capture the output of a bash command into a variable when using pipes and apostrophe? [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I am not sure how to save the output of a command via bash into a variable:
PID = 'ps -ef | grep -v color=auto | grep raspivid | awk '{print $2}''
Do I have to use a special character for the apostrophe or for the pipes?
Thanks!
To capture the output of a command in shell, use command substitution: $(...). Thus:
pid=$(ps -ef | grep -v color=auto | grep raspivid | awk '{print $2}')
Notes
When making an assignment in shell, there must be no spaces around the equal sign.
When defining shell variables for local use, it is best practice to use lower case or mixed case. Variables that are important to the system are defined in upper case and you don't want to accidentally overwrite one of them.
Simplification
If the goal is to get the PID of the raspivid process, then the grep and awk can be combined into a single process:
pid=$(ps -ef | awk '/[r]aspivid/{print $2}')
Note the simple trick that excludes the current process from the output: instead of searching for raspivid we search for [r]aspivid. The string [r]aspivid does not match the regular expression [r]aspivid. Hence the current process is removed from the output.
The Flexibility of awk
For the purpose of showing how awk can replace multiple calls to grep, consider this scenario: suppose that we want to find lines that contain raspivid but that do not contain color=auto. With awk, both conditions can be combined logically:
pid=$(ps -ef | awk '/raspivid/ && !/color=auto/{print $2}')
Here, /raspivid/ requires a match with raspivid. The && symbol means logical "and". The ! before the regex /color=auto/ means logical "not". Thus, /raspivid/ && !/color=auto/ matches only on lines that contain raspivid but not color=auto.
A more straightforward approach:
pid=$(pgrep raspivid)
... or a little different
echo pgrep [t]eleport

Shell script to replace string dynamically

I am writing a shell script for linux which takes argument as port no.
inside file following is a line which needs to be updated:
define('NO_OF_PORTS',10);
I need to replace that 10 by the argument passed.
But this should be dynamic, like next time I pass new port no it must get updated.
Using sed:
s="define('NO_OF_PORTS',10);"
n=25
sed "s/\('NO_OF_PORTS',\)[0-9]*/\1$n/" <<< "$s"
define('NO_OF_PORTS',25);
To change inline in the file use:
sed -i.bak "s/\('NO_OF_PORTS',\)[0-9]*/\1$n/" file
You can use sed in the script to edit the file
sed -i s/NO_OF_PORTS\',[0-9]*/NO_OF_PORTS\',$1/ $2
1.txt has
define('NO_OF_PORTS',19)
shell script
#!/bin/sh
echo $1
sed -i -r '/NO_OF_PORTS/ s/'[0-9]+'/'$1'/g' 1.txt
run
linux:/home/test # ./replace_port.sh 78
linux:/home/test # cat 1.txt
define('NO_OF_PORTS',78)

Pattern matching with grep [duplicate]

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

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