Get the extension of file [duplicate] - bash

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 7 years ago.
I have files with "multiple" extension , for better manipulation I would like to create new folder for each last extension but first I need to retrieve the last extension.
Just for example lets assume i have file called info.tar.tbz2 how could I get "tbz2" ?
One way that comes to my mind is using cut -d "." but in this case I would need to specify -f parameter of the last column, which I don't know how to achieve.
What is the fastest way to do it?

You may use awk,
awk -F. '{print $NF}' file
or
sed,
$ echo 'info.tar.tbz2' | awk -F. '{print $NF}'
tbz2
$ echo 'info.tar.tbz2' | sed 's/.*\.//'
tbz2

Related

how to pass variable to awk in bash for parsing value [duplicate]

This question already has an answer here:
awk: fatal: cannot open file `' for reading (No such file or directory)
(1 answer)
Closed 2 years ago.
Using awk when I try to parse the last folder I get error "No such file or directory". How do I pass variable to awk
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName=$(awk -F/ '{print $NF}' $CacheLocation)
awk does not work on strings, it expects the input as file or stdin
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$(awk -F/ '{print $NF}' <<<"$CacheLocation" )"
In this special case, it might be easier to use basename
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$( basename "$CacheLocation" )"

How can I delete empty line from my ouput by grep? [duplicate]

This question already has answers here:
Remove empty lines in a text file via grep
(11 answers)
Closed 4 years ago.
Exists way to remove empty lines with cat myfile | grep -w #something ?
I looking for simple way for remove empty lines from my output like in the way the presented above.
This really belongs on the codegolfing stackexchange because it's not related to how anyone would ever write a script. However, you can do it like this:
cat myfile | grep -w '.*..*'
It's equivalent to the more canonical grep ., but adds explicit .*s on either side so that it will always match the complete line, thereby satisfying the word boundary conditions imposed by -w
You can pipe your output to awk to easily remove empty lines
cat myfile | grep -w #something | awk NF
EDIT: so... you just want cat myfile | awk NF?
if you have to use grep, you can do grep myfile -v '^[[:blank:]]*$'

How to use grep command to extract number in a text file [duplicate]

This question already has answers here:
Extract number from a line with awk/sed
(2 answers)
Closed 7 years ago.
I have a cookie file,I just want to extract the number before the .jpg extension using grep command ..how can i do it
USERSTAFFPHOTO 09480106177557.jpg
I want to copy 09480106177557
echo "USERSTAFFPHOTO 09480106177557.jpg" | grep -oE '[0-9]{1,}'
You should chain 2 grep commands, the first one to get the file name along with the .jpg extension and the second one to get only the number from the file name:
grep -oE '[0-9]+\.jpg' | grep -oE '[0-9]+'
Otherwise, you would get all the numbers that exist in the file with or without the .jpg extension.
Why restrict yourself to a particular tool? Do you want the number, or do you want to use grep? If the format is fixed, I would do this with tr:
echo USERSTAFFPHOTO 09480106177557.jpg | tr -cd [0-9]
If this is one line in a file and you need to filter it out, try awk:
awk '/USERSTAFFPHOTO/{print $2}' FS=' |\\.' input

Unique entry set in the first column of all csv files under directory [duplicate]

This question already has answers here:
Is there a way to 'uniq' by column?
(8 answers)
Closed 7 years ago.
I have a list of comma separated files under the directory. There are no headers, and unfortunately they are not even the same length for each row.
I want to find the unique entry in the first column across all files.
What's the quickest way of doing it in shell programming?
awk -F "," '{print $1}' *.txt | uniq
seems to only get uniq entries of each files. I want all files.
Shortest is still using awk (this will print the row)
awk -F, '!a[$1]++' *.txt
to get just the first field
awk -F, '!a[$1]++ {print $1}' *.txt

Extract Information From File Name in Bash [duplicate]

This question already has answers here:
How to split a string into an array in Bash?
(24 answers)
Closed 7 years ago.
Suppose I have a file with a name ABC_DE_FGHI_10_JK_LMN.csv. I want to extract the ID from the file-name i.e. 10 with the help of ID position and file-name separator. I have following two inputs
File-name_ID_Position=4; [since 10 is at fourth position in file-name]
File-name_Delimiter="_";
Here ID can be numeric or alpha-numeric. So how extract the 10 from above file with the help of above two inputs. How to achieve this in bash?
Instead of writing a regex in bash, I would do it with awk:
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F_ -v pos=4 '{print $pos}'
or if you want the dot to also be a delimiter (requires GNU awk):
echo 'ABC_DE_FGHI_10_JK_LMN.csv' | awk -F'[_.]' -v pos=4 '{print $pos}'

Resources