How can I set a variable to be the output of cat command? [duplicate] - bash

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 have a file named "myout" in my current directory.
I wish to set the variable x to be the output of that line:
cat myout | cut -d" " -f1 | cut -d"/" -f1
I searched all over the site and couldn't find an answer,
any help would be appreciated!

x=$(cut -d" " -f1 ./myout | cut -d "/" -f1)
Updated as per Cyrus's comment.

Related

Explode a string into two parts using Bash [duplicate]

This question already has answers here:
How do I split a string on a delimiter in Bash?
(37 answers)
Closed 1 year ago.
I'm building a small script to automate something I do pretty regularly.
The script takes in a single parameter, for example: vendor/package. I'd like to take that and split/explode it into two separate strings: vendor and package.
I've looked at solutions on here and various other places but can't get anything to work. I'm sure it's really simple but I'm not great at bash so some help would be greatly appreciated. Thanks!
You can use cut to achieve this
[🐉 vaebhav#Vaibhavs-MacBook-Air:/usr/local/lib - 06:12 PM 🐉]$ echo "value/package" | cut -d"/" -f1
value
[🐉 vaebhav#Vaibhavs-MacBook-Air:/usr/local/lib - 06:12 PM 🐉]$ echo "value/package" | cut -d"/" -f2
package
[🐉 vaebhav#Vaibhavs-MacBook-Air:/usr/local/lib - 06:12 PM 🐉]$
in terms of its implementation in a script -
part1=`echo $1 | cut -d"/" -f1`
part2=`echo $1 | cut -d"/" -f2`
If using bash:
#!/bin/bash
echo ${1#*/}
echo ${1%/*}
Test:
$ bash foo.bash vendor/package
package
vendor

Can't assign linux command output into a 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 3 years ago.
I've tried to write the following code in bash but I am not able to get the output to be echoed.
part1="blkid | grep -P 'CENTOS 7' | cut -c1-9"
echo "$part1"
Try this
part1=$(blkid | grep -P 'CENTOS 7' | cut -c1-9)
echo "$part1"

Removing word from grep output [duplicate]

This question already has answers here:
Parsing variables from config file in Bash
(7 answers)
Closed 4 years ago.
I have the following bash command:
cat setup.py | grep name=`
This returns the line
name='SOME_PROJECTNAME',
How would I pipe this output from grep to just retrieve SOME_PROJECTNAME?
I have tried
cat setup.py | grep name= | tr -d 'name=','
but this removes characters in SOME_PROJECTNAME.
Use grep lookahead.
$ grep -oP "(?<=name=').*(?=')" setup.py
#bad cat setup.py | grep name= | cut -d= -f2-
cat setup.py | grep name= | cut -d' -f2
Sed may be helpful here.
sed -ne "s/name='\(.*\)'/\1/p" setup.py
The option -n makes sed not print lines by default. Then we replace the entire property line (name='SOME_PROJECTNAME') with the value only (SOME_PROJECTNAME). The p flag in the s/// command makes sed print the line only if the replacement its executed. So, the only line to be printed are the ones where the replacement was done, with the replaced value.

How to iterate through line and check needed part? [duplicate]

This question already has an answer here:
How can I retrieve an entry from /etc/passwd for a given username?
(1 answer)
Closed 5 years ago.
I have this line
Username:x:120:101:somethingsomething
and I need to get the '101' part after the third ':', how can I do that?
do I use grep or sed?
cut -d':' -f4 /etc/passwd
awk, only with string:
mstr="Username:x:120:101:somethingsomething"; awk -F: '{print $4}' <<< "$mstr"

How to extract path to filename [duplicate]

This question already has answers here:
How to extract directory path from file path?
(9 answers)
Closed 6 years ago.
In bash programming If VAR="/home/somestuff/work/OutExample/Air",
How to get "workOutExampleAir" ?
Thanks
cut -f4- -d/ --output-delimiter "" <<< "$var"
cut utility takes field from 4 with delimiter /
in bash
name=$(cut -f4- -d/ --output-delimiter "" <<< "$var")
echo "$name"

Resources