Linux bash - extract value from grep output [duplicate] - bash

This question already has answers here:
Get string after character [duplicate]
(5 answers)
Closed 6 years ago.
I have variable $OUTPUT=abc PHYSIN=lalala ghi
How can I extract the value of PHYSIN into another new variable called VETH_NAME,
in other words, I'd like VETH_NAME to be lalala
How can I do so using bash commands?
Thanks

Assuming this is what you are saying :
OUTPUT="abc"
PHYSIN="lalala ghi"
VETH_NAME=$(echo "$PHYSIN" | cut -d" " -f1)
finally :
echo $VETH_NAME
lalala

Use parameter expansion with the %% operator, which drops the longest matching suffix from the expansion.
OUTPUT=abc
PHYSIN="lalala ghi"
VETH_NAME=${PHYSIN%% *}

Related

How should I sort one word using shell? [duplicate]

This question already has answers here:
How to sort characters in a string?
(5 answers)
Closed 4 years ago.
I can use every language to realize it, but I want to use shell.
Use shell command or shell script.
For example, I have one word: sport.
I want to get another word: oprst.
Now I want to use shell to bring about it, how to do? Thanks!
Use the following Shell Script:-
#!/bin/sh
sortedWord=`echo $1 | grep -o . | sort |tr -d "\n"`;
echo $sortedWord;

bash- Assigning a value to a string using a command (rev) [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I am trying to assign the value of a string to the reversed value of another string (via the 'rev' command). I know the rev command is used like this:
$ echo "hello" | rev
olleh
But what I am trying to do is something like this:
var="hello"
rav=${$var | rev}
I know this isn't correct syntax and it doesn't work, but I was wondering if there was a way to assign a variable using a command, and if so how is it done?
Try this, using command substitution:
var="hello"
rav=$(echo "$var" | rev)
echo $rav
Following may also help you on same.
var="hello"
rav=$(rev <<< "$var")
Output will be as follows:
echo $rav
olleh

awk command does not work properly when assigning output to variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
I'm trying to convert space to underscore in a file name, my script is
like below.
old_file=/home/somedir/otherdir/foobar 20170919.csv
new_file="$(basename "$old_file")" | awk 'gsub(" ","_")'
This script works fine when I use with echo command,
echo "$(basename "$old_file")" | awk 'gsub(" ","_")'
but when it comes to assigning the output to variables, it doesn't work...
Does anybody know the idea?
Actually no need of awk, please note below one replaces all space to underscore, not just filename, it can be path too
$ old_file="/home/somedir/otherdir/foobar 20170919.csv"
$ newfile="${old_file// /_}"
$ echo "$newfile"
/home/somedir/otherdir/foobar_20170919.csv

How assign rev command to a variable in shell [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 wanted to direct the output of rev command to a variable, I tried different methods and didn't work.
read -p "Enter the number: " n
echo $n | rev
echo "new n is: $p"
I want to assign the output of line 2 to p. How?
Thank you,
To store the output of a command in a variable use a $(...) command substitution:
p=$(echo $n | rev)
For further reference, you can check this link

Unix shell scripting to reverse a string without rev [duplicate]

This question already has answers here:
reverse the order of characters in a string
(14 answers)
Closed 9 years ago.
I want to write a shell script that accepts a string as a command line argument and prints out what is entered in the reverse order with out using the rev command but rather reversing the letters one by one. how would I do that?
so like
if Flower is entered it will print out rewolF
Thanks
A good subject for bash parameter expansion :
#!/bin/bash
for ((i=${#1}; i>=0; i--)); do printf "${1:$i:1}"; done; echo
Example :
./script.sh foobar
Output :
raboof
s1='Flower'
a1=($(echo $s1|fold -w1 ))
for (( i=${#a1[#]}-1;i>=0;i--));do echo "$i=>${a1[i]}"; done
5=>r
4=>e
3=>w
2=>o
1=>l
0=>F

Resources