-2

I am finding the files in specific location now I need to extract the file name which is after last slash from that path without its extension like *.war by using shell scripting.

I tried below to find out the data in the path:

find /data1/jenkins_devops/builds/develop/5bab159c1c40cfc44930262d30511ac7337805fa -mindepth 1 -type f -name '*.war'

Ex.- This folder "5bab159c1c40cfc44930262d30511ac7337805fa" contains multiple .war file like interview.war, auth.war so I am expecting output is interview war.

Can someone please help?

jww
  • 97,681
  • 90
  • 411
  • 885
Learner
  • 105
  • 1
  • 1
  • 7
  • 3
    What is stopping you? – B001ᛦ Aug 16 '19 at 08:59
  • 3
    What have you tried? What happened? Why didn't it work? Why is this tagged groovy? – tim_yates Aug 16 '19 at 09:03
  • @tim_yates I tried with this- echo "find /data1/jenkins_devops/builds/develop/ab7f302d157d839b4ac3d7917cfa2d550ba2e73e/auth.war" | awk -F'/' '{print $6}' but not getting the proper result. – Learner Aug 16 '19 at 09:42
  • @tim_yates Actually "ab7f302d157d839b4ac3d7917cfa2d550ba2e73e" is my folder name that contains multiple .war files and I need all the names to be printed of that war files. – Learner Aug 16 '19 at 09:45
  • Possible duplicate of [Extract filename and extension in Bash](https://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash) – Lety Aug 16 '19 at 10:05

2 Answers2

1

There are much elegant ways to achieve the objectve. The following use awk to achieve the objectiv:

find /data1/jenkins_devops/builds/develop/5bab159c1c40cfc44930262d30511ac7337805fa -mindepth 1 -type f -name *.war | awk -F "/" '{print $NF}' | awk -F "." '{print $1}'

Awk NF returns the number of fields and you can use that to print the last column. First you seperate the columns with / as field seperator in awk and use it to print last column. Then use . as seperator and print the first column to achieve the desired result. It is done in the above script.

j23
  • 3,139
  • 1
  • 6
  • 13
  • actually I am finding the files from specific location mentioned below and need to print last filename without .war find /data1/jenkins_devops/builds/develop/5bab159c1c40cfc44930262d30511ac7337805fa -mindepth 1 -type f -name '*.war – Learner Aug 16 '19 at 10:25
  • @uday pipe them to the awk. See the edited answer. – j23 Aug 16 '19 at 11:12
0

Just use basename:

the_path="/data1/jenkins_devops/builds/develop/ab7f302d157d839b4ac3d7917cfa2d550ba2e73e/auth.war"
basename "$the_path" .war
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • actually I am finding the files from specific location mentioned below and need to print last filename without .war find /data1/jenkins_devops/builds/develop/5bab159c1c40cfc44930262d30511ac7337805fa -mindepth 1 -type f -name '*.war' – Learner Aug 16 '19 at 10:21
  • So? So pipe them to basename via a while read loop or by xargs. – KamilCuk Aug 16 '19 at 10:26