Get file extension with bash script [duplicate] - bash

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

Related

use grep and cut command to get output of each line in next line [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 months ago.
$ cat hello.txt
shreenivasa hi hello this is test mail
shreenivasa how are you man
if i run in terminal
$ cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-
its giving following output
hi hello this is test mail
how are you man
but if i write it as script in hello.sh like below
#!/bin/bash
hi=`cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-`
echo $hi
output for ./hello.sh is
hi hello this is test mail how are you man.
I want to print "how are you man" in next line
like
hi hello this is test mail
how are you man
tried $hi\n didn't work

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

bash: trouble getting file size from remote url [duplicate]

This question already has answers here:
How to get remote file size from a shell script?
(14 answers)
How to remove a newline from a string in Bash
(11 answers)
Closed 4 years ago.
I am trying to get file size of remote url in the following manner:
I am trying the below command:
curl -sI $url | grep -i content-length | cut -d ' ' -f 2
Its giving 17857797\C-M
I dont understand why \C-M is adding
Append | dos2unix or | tr -d '\r' to remove carriage-return.

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

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