开发者

Get N line from unzip -l

开发者 https://www.devze.com 2023-04-01 02:19 出处:网络
I have a jar file, i need to execute the files in it in Linux. So I need to get the result of the unzip -l command line by line.

I have a jar file, i need to execute the files in it in Linux. So I need to get the result of the unzip -l command line by line. I have managed to extract the files names with this command :

unzip -l package.jar | awk '{print $NF}' | grep com/tests/[A-Za-Z] | cut -d "/" -f3 ;

But i c开发者_高级运维an't figure out how to obtain the file names one after another to execute them. How can i do it please ?

Thanks a lot.


If all you need the first row in a column, add a pipe and get the first line using head -1

So your one liner will look like :

unzip -l package.jar | awk '{print $NF}' | grep com/tests/[A-Za-Z] | cut -d "/" -f3 |head -1;

That will give you first line

now, club head and tail to get second line.

unzip -l package.jar | awk '{print $NF}' | grep com/tests/[A-Za-Z] | cut -d "/" -f3 |head -2 | tail -1;

to get second line.

But from scripting piont of view this is not a good approach. What you need is a loop as below:

for class in `unzip -l el-api.jar | awk '{print $NF}' | grep javax/el/[A-Za-Z] | cut -d "/" -f3`; do echo $class; done;

you can replace echo $class with whatever command you wish - and use $class to get the current class name.

HTH


Here is my attempt, which also take into account Daddou's request to remove the .class extension:

unzip -l package.jar | \
awk -F'/' '/com\/tests\/[A-Za-z]/ {sub(/\.class/, "", $NF); print $NF}' | \
while read baseName
do
    echo "  $baseName"
done

Notes:

  • The awk command also handles the tasks of grep and cut
  • The awk command also handles the removal of the .class extension
  • The result of the awk command is piped into the while read... command
  • baseName represents the name of the class file, with the .class extension removed
  • Now, you can do something with that $baseName
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号