Reputation: 24441
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:
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?
Upvotes: 3
Views: 124
Reputation: 185530
Using perl :
#!/usr/bin/env perl
use strict; use warnings;
while (<DATA>) {
chomp;
my @list = split /\//;
print map { $_ . "\t" . pop(@list) . "\n" }
qw/-filename: -version: -artifact:/;
print "-group:\t\t", join(".", @list[1..$#list]), "\n\n";
}
__DATA__
./com/eric/ics/ccc/BillP/3.5.11/BillP-3.5.11.jar
./com/eric/ics/BillP/3.5.11/BillP-3.5.11.jar
./com/eric/ics/xxx/yyy/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.ccc
-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.ics.xxx.yyy
Upvotes: 0
Reputation: 785481
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
Upvotes: 1
Reputation: 2292
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.
Upvotes: 1