How do I get a part of a filename in shell script?

0

I have a file that has the format nome1-nome2-0.0.0.0.war , and through the shell I wanted to get the version (0.0.0.0) and save it in a variable and then use it to create a directory .

Note: Versions change, but the names and extension are the same. Thank you!

    
asked by anonymous 21.03.2017 / 20:37

2 answers

1

You can do it in some ways, one of them is:

ls nome1-nome2-0.0.0.0.war| egrep  -wo '[\.0-9]+'

The output of this will be: 0.0.0.0

Explanation:

I created a file named name1-name2-0.0.0.0.war , I executed a grep using the following regex '[\.0-9]+' , the parameter o and w , it was necessary to match only what the regex contemplated.

A script that will scan a folder and create other folders based on the versions of your app, could be:

#!/bin/bash

for i in $(ls *.war|egrep  -wo '[\.0-9]+')
do
    mkdir $i
done
    
21.03.2017 / 20:49
0
ls *.war | awk -F '-' {'print $3'} | awk -F '.war' {'print $1'}
while read versao
do
    mkdir $versao 
done
    
07.07.2017 / 20:31