How to get a part of a file name in shell script?

Asked

Viewed 2,044 times

0

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

Obs.: The versions change, but the names and extension are the same. Thank you!

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 called name1-Nome2-0.0.0.0.War, executed a grep using the following regex '[\.0-9]+', the parameter o and w, were necessary to match only what the regex contemplated.

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

#!/bin/bash

for i in $(ls *.war|egrep  -wo '[\.0-9]+')
do
    mkdir $i
done

0

ls *.war | awk -F '-' {'print $3'} | awk -F '.war' {'print $1'}
while read versao
do
    mkdir $versao 
done
  • 1

    This response was flagged as low quality due to size or content. Please, if possible and if this is a solution to the problem, explain better how you solved it. The code may be trivial to you, but to other users it may not be.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.