0

I have some zip files in directory when I need to unzip each in specific dir I used

ls | awk '{ print "unzip " $1 " -d " $1} '
unzip p21286665_121020_Linux-x86-64.zip -d p21286665_121020_Linux-x86-64.zip
unzip p21841318_121020_Linux-x86-64.zip -d p21841318_121020_Linux-x86-64.zip
unzip p22098146_121020_Linux-x86-64.zip -d p22098146_121020_Linux-x86-64.zip

But I need something like this.

unzip p21286665_121020_Linux-x86-64.zip -d p21286665
unzip p21841318_121020_Linux-x86-64.zip -d p21841318
unzip p22098146_121020_Linux-x86-64.zip -d p22098146
5

You could also use the -F argument to split the line on underscore and thus end up with something like this:

ls | awk -F_ '{print "unzip " $0 " -d " $1; }
0

try

ls | awk '{ print "unzip " $1 " -d " substr($1,1,index($1,"_")-1) } '

or in sed

ls | sed -e "s/\([^_]*\)_.*$/unzip & -d \1/"

where

  • [^_]* is a non _ string
  • \( .. \) catpure as \1
  • & is whole pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.