How to extract path to filename [duplicate] - bash

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"

Related

grep access_token in bash script from the URL [duplicate]

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
Wants to get the access_token from the url below in bash script.
http://localhost:4200/loginoauth2#access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiI4ZTAxNTM1MWfddfRmMzEyMzczMTk4NTUxNjYyODRiMWI1MCIsImV4cCI6MTU5NTI1NzYwMiwic3ViIjoiZm9nsddssbWFuIn0.Sr3FT4EdssdsbddnBUR4VgKfPdAhaqvdGKKCJKV8gWLa3xhJwcfg_I3pjnHuYdsfsdfsdfsdfsd0zVD_MYNqqmLMqNRbdeeDTXgtveWmlErQ&expires_in=3600&scope=all&state=3d417058-50a5-49&token_type=Bearer
With sed:
echo 'http://localhost:4200/loginoauth2#access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiI4ZTAxNTM1MWfddfRmMzEyMzczMTk4NTUxNjYyODRiMWI1MCIsImV4cCI6MTU5NTI1NzYwMiwic3ViIjoiZm9nsddssbWFuIn0.Sr3FT4EdssdsbddnBUR4VgKfPdAhaqvdGKKCJKV8gWLa3xhJwcfg_I3pjnHuYdsfsdfsdfsdfsd0zVD_MYNqqmLMqNRbdeeDTXgtveWmlErQ' |
sed 's/.*access_token=\([^&]*\).*/\1/'
This should do it
URL="http://localhost:4200/loginoauth2#access_token=eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiI4ZTAxNTM1MWRmMzEyMzczMTk4NTUxNjYyODRiMWI1MCIsImV4cCI6MTU5NTI1NzYwMiwic3ViIjoiZm9nbWFuIn0.Sr3FT4EnBUR4VgKfPdAhaqvdGKKCJKV8gWLa3xhJwcfg_I3pjnHuY0zVD_MYNqqmLMqNRbdeeDTXgtveWmlErQ&expires_in=3600&scope=all&state=3d417058-50a5-49&token_type=Bearer"
token=$(echo $URL | cut -d '=' -f 2 | cut -d '&' -f 1)
echo $token

How can I set a variable to be the output of cat command? [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 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.

Get file extension with bash script [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
i' m trying to extract the file extension in bash without using regex.
i' ve tried the following
extension = $(echo $1 | cut -f 2 -d '.')
extension is the variable
$1 contains something like: file.txt or file.pdf etc.
this code is outputting:
./prova.sh: 3: ./prova.sh: extension: not found
extension=$(echo sample.txt | cut -f 2 -d '.')
echo $extension
Above command will give you the output as expected txt

Get substring of string in korn shell [duplicate]

This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 6 years ago.
Have a string as xxxxxxx.txt
Need to get the only xxxxxxx string without extension.
How to get this using korn shell?
s=abcdef.txt
s_base=${s%.txt}
...will assign abcdef to s_base.
STRING=xxxxxxxxxx.txt
echo $STRING | rev | cut -f 2- -d "." | rev

Matching only the first three numbers of a string in ksh [duplicate]

This question already has answers here:
KSH check if string starts with substring
(4 answers)
Closed 8 years ago.
VARIABLE=`grep PortNumber` testfile.txt | awk -F'"' '{print $2}'`
echo $VARIABLE
33111
I want to do a check to ensure the first 2 numbers of the variable are the digit '3' only.
How can I do this using a standard ksh script?
EDIT:
I think I have it in the following, does this look correct?
echo $VARIABLE | egrep -q '^[3]{2}'
You can do :
echo $VARIABLE | grep -E "^3+{2}"

Resources