what is "|" in Ubuntu or Linux in general [duplicate] - linux

This question already has answers here:
What does "|" mean in a terminal command line? [closed]
(3 answers)
Closed 4 years ago.
example :
diskFreeSpace="$(df | grep -v -E 'Filesystem|udev|tmpfs|Home|/dev/sr1|/dev/sr0' | awk '{ print $5-1+1}')"
and hope if you understand what is this command u help me

the symbol is called a "pipe".
The command df | grep is "piping" (redirecting) the standard output of the df command into the standard input of the grep command. In this example the grep command filters the output of the df command. Then the awk commands is for display/format purposes.
Inside the regular expression it is specific to the grep syntax since it is surrounded by quotes it is only considered as an argument passed to grep

Related

Execute agrep within script | grep terminal output [duplicate]

This question already has answers here:
Assign output to variable in Bash [duplicate]
(2 answers)
Closed 2 years ago.
Can I execute a grep in my script below?
echo 5
echo 4
result='çat output.txt | grep flag'
echo $result
The scipt gets used like
./script | ./program > output.txt
The script is used as input for the program, and the output of the program gets put into output, which I want to be able to grep for instantly. At the moment, it seems to finish without doing the grep command
I have the impression you are using single quotes while it should be backtics. Luckily there is something easier to use
result=$(cat output.txt | grep "flag")
The $(some_command) is used for getting the results of some_command.

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.

how to print text between two specific words using awk, sed? [duplicate]

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 4 years ago.
how to print text between two specific words using awk, sed ?
$ ofed_info | awk '/MLNX_OFED_LINUX/{print}'
MLNX_OFED_LINUX-4.1-1.0.2.0 (OFED-4.1-1.0.2):
$
Output required:-
4.1-1.0.2.0
Following awk may help you here.(considering that your input to awk will be same as shown sample only)
your_command | awk '{sub(/[^-]*/,"");sub(/ .*/,"");sub(/-/,"");print}'
Solution 2nd: With sed solution now.
your_command | sed 's/\([^-]*\)-\([^ ]*\).*/\2/'
Solution 3rd: Using awk's match utility:
your_command | awk 'match($0,/[0-9]+\.[0-9]+\-[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/){print substr($0,RSTART,RLENGTH)}'
You may use this sed:
echo 'MLNX_OFED_LINUX-4.1-1.0.2.0 (OFED-4.1-1.0.2):' |
sed -E 's/^[^-]*-| .*//g'
4.1-1.0.2.0
This sed command removes text till first hyphen from start or text starting with space towards end.
Try this:
ofed_info | sed -n 's/^MLNX_OFED_LINUX-\([^ ]\+\).*/\1/p'
The sed command only selects lines starting with the keyword and prints the version attached to it.

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

Resources