Extract Information From File Name in Bash [duplicate] - bash

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}'

Related

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 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

Get the extension of file [duplicate]

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

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}"

How to pass variable to awk [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using awk with variables
The following command is wrong, the point is I want to use $curLineNumber in awk, how can I do it? Any solution?
curLineNumber = 3
curTime=`ls -l | awk 'NR==$curLineNumber {print $NF}'`
Thanks
curTime=$(ls -l | awk -v line=$curLineNumber 'NR == line { print $NF }'
The -v option is used to specify variables initialized on the command line. I chose the name line for the awk variable.

Resources