Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I just want to ask if, is there an easier way to parse string?
say for example: ant-1.3.5-1 is the string. i want to parse this variable into this:
artifactId=ant version=1.3.5-1
Note: The rule in this parser is that the string may change its length and value because it is located in a directory wherein there are also a lot of other artifacts in the directory. So the first thing that will come up to your mind is that you should create a looping statement wherein all artifacts will be placed in an array. Then, inside the loop is the parsing and assignment of the correct values to the correct fields.
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
#extract the version of the jar from the artifactId
args=$(echo $artifactId | perl -lne '$c++ while /-/g; END {print $c; }')
while [ $ctr -le $(expr $args + 1) ]
do
temp=$(echo $artifactId | cut -d'-' -f $ctr)
numseries=$(echo $temp | sed -e 's/^[0-9]//')
if [ -z $numseries ]
then
echo "null numseries"
else
if [ $temp != $numseries ]
then
echo "$temp is a number!"
else
tempartifact[$ctr]=$temp
echo "artifact: ${tempartifact[$ctr]}"
fi
fi
ctr=$(expr $ctr + 1)
done
Hmm, not homework yet you quote some explaining explaing the sort of thought process you should adopt when tackling it?? It's a pretty simple one liner in many forms, just a single bash substitution potentially - http://tldp.org/LDP/abs/html/string-manipulation.html Not sure if that will fit in with what the teacher wants though.
Hmm, not homework yet you quote some explaining explaing the sort of thought process you should adopt when tackling it?? It's a pretty simple one liner in many forms, just a single bash substitution potentially - http://tldp.org/LDP/abs/html/string-manipulation.html Not sure if that will fit in with what the teacher wants though.
Sir, I appreciate your help but I know how to manipulate strings. The problem is that the strings in each artifact is not fixed in just one artifact id. so i cannot assume that the delimiter should be a dash. example: commons-logging-1.1.8
and the versions would also appear like this: jslt-13.4.5-84.1
Well ... as long as the "word" part of your artifactid
doesn't contain numbers, and the versions have no alpha
components it's still trivial.
Code:
[tink:~]$ echo -e "commons-logging-1.1.8\njslt-13.4.5-84.1" | sed -r 's/^([-a-z\.]+)-.+/\1/'
commons-logging
jslt
[tink:~]$ echo -e "commons-logging-1.1.8\njslt-13.4.5-84.1" | sed -r 's/^[-a-z\.]+-(.+)/\1/'
1.1.8
13.4.5-84.1
If the conditions above DON'T apply I'd say you're
screwed unless you have a dictionary of your IDs,
since a parsing by lexicographic rules w/o language
knowledge is impossible.
In other words, both atoms can contain dashes inside of them. Then you are going to have to filter by contents. For anything that's not basic string mangling you are going to have to use something more advanced, like awk or sed.
Well ... as long as the "word" part of your artifactid
doesn't contain numbers, and the versions have no alpha
components it's still trivial.
Code:
[tink:~]$ echo -e "commons-logging-1.1.8\njslt-13.4.5-84.1" | sed -r 's/^([-a-z\.]+)-.+/\1/'
commons-logging
jslt
[tink:~]$ echo -e "commons-logging-1.1.8\njslt-13.4.5-84.1" | sed -r 's/^[-a-z\.]+-(.+)/\1/'
1.1.8
13.4.5-84.1
If the conditions above DON'T apply I'd say you're
screwed unless you have a dictionary of your IDs,
since a parsing by lexicographic rules w/o language
knowledge is impossible.
cheers,
Tink
Sir, one last question. how about getting the version from the given artifact? what should i need to configure with the reg ex?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.