Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split the string into variables/parameters to pass to another script?

I've got a list of files (with full paths) that I need to split into tokens to be able to pass to another script/command. Looking at the style of the strings, I figure awk is the right tool to use, but I just can't seem to figure out how to do this given that the number of tokens vary by line.

Given a filename ./some/path/to/artifact_name/v1.2.3/filename.jar, I need to be able to extract the following:

  • filename - the string following the final / : filename.jar
  • version - the string following the / preceeding the filename : v1.2.3
  • artifact name - the string following the / preceeding the version : artifact_name
  • group name - the rest of the path, with all / replaced by . : some.path.to

For example, given:

./com/eric/ics/BillP/3.5.11/BillP-3.5.11.jar
- filename: BillP-3.5.11.jar
-version: 3.5.11
-artifact: BillP
-group: com.eric.ics

My biggest complication becomes that the number of folders representing the group can change. For ex: ./com/eric/some/other/pkg/BillP/3.5.11/BillP-3.5.11.jar would be just as valid, except that the group would then be com.eric.some.other.pkg.

My goal is to pass these 4 params to a separate script once I've managed to extract them, but I cannot seem to figure out the easiest way to do this. Is awk the right tool for this? Is there something better/easier to use?

like image 662
Eric B. Avatar asked Apr 28 '26 14:04

Eric B.


2 Answers

You can use awk or perl to print all the components and pipe that into read to assign them to different variable names, or use the less elegant commands dirname and basename several times to get the components one by one, storing them immediately into variables that you can use as parameters to call other scripts. Depends on what your like.

#!/bin/bash

p="$1"
file="$(basename "$p")"
p="$(dirname "$p")"
version="$(basename "$p")"
p="$(dirname "$p")"
artifact="$(basename "$p")"
group="$(dirname "$p" | tr / . | sed 's+\.*++')"

echo file=$file version=$version artifact=$artifact group=$group

Now you have these 4 parameters in 4 variables which you can pass to whatever you like.

like image 147
Hans Klünder Avatar answered Apr 30 '26 06:04

Hans Klünder


Using gnu-awk:

awk -F/ -v OFS=. '{f=$NF;v=$(NF-1);a=$(NF-2); NF-=3; sub(/^[^[:alnum:]]+/, ""); 
  printf "-filename: %s\n-version: %s\n-artifact: %s\n-group: %s\n\n", f, v, a, $0 }' file
-filename: BillP-3.5.11.jar
-version: 3.5.11
-artifact: BillP
-group: com.eric.ics

-filename: BillP-3.5.11.jar
-version: 3.5.11
-artifact: BillP
-group: com.eric.some.other.pkg

cat file
./com/eric/ics/BillP/3.5.11/BillP-3.5.11.jar
./com/eric/some/other/pkg/BillP/3.5.11/BillP-3.5.11.jar

EDIT: To call a secondary script using these params:

awk -F/ -v OFS=. '{f=$NF;v=$(NF-1);a=$(NF-2); NF-=3; sub(/^[^[:alnum:]]+/, ""); 
  system("./script.sh " f " " v " " a " " $0) }' file
like image 41
anubhava Avatar answered Apr 30 '26 07:04

anubhava