AlwynIsPat
AlwynIsPat

Reputation: 719

How to extract name of file?

In a dir i have some files which looks something like;

org.coy.application_0.1-2_arm.deb

com.cpo.app2_1.2.1_arm.deb

sg.team.works.app3a_1.33_arm.deb

com.share.name4.deb

com.sha-re.app5.deb

com.sha.re.anything.deb

I only require the bolded names.

here's what i have so far;

for file in *.deb; do
 name=$(echo "$file" | sed 's/^.*\.\([^.][^.]*\)\.deb$/\1/')
 echo $name
done

Upvotes: 0

Views: 142

Answers (5)

potong
potong

Reputation: 58430

This might work for you:

for file in *.deb; do
    name=$(echo "$file" |  sed 's/.*\.\([a-zA-Z][^_.]*\).*\.deb/\1/')
    echo $name
done

Upvotes: 0

kev
kev

Reputation: 161694

for i in *.deb
do
    name=${i%.deb}      #<-- remove extension      (.deb)
    name=${name%%_*}    #<-- remove version        (_x.y.z_arm)
    name=${name##*.}    #<-- remove namespace      (comp.x.y.z)
    echo $name
done

output

app2
anything
app5
name4
application
app3a

Upvotes: 2

Birei
Birei

Reputation: 36262

One way using perl:

perl -e '
    do { 
        printf qq[%s\n], $+{my} 
            if $ARGV[0] =~ m/(?(?=.*_)\.(?<my>[^._]+)_\d|.*\.(?<my>[^.]+)\.deb\Z)/ 
    } while shift && @ARGV
' *.deb

Explanation of the regexp:

(?                          # Conditional expression.
(?=.*_)                     # Positive look-ahead to check if exits '_' in the string.
\.(?<my>[^._]+)_\d          # If previous look-ahead succeed, match string from a '.' until
                            # first '_' followed by a number.
|                           # Second alternative when look-ahead failed.
.*\.(?<my>[^.]+)\.deb\Z     # Match from '.' until end of string in '.deb'

As I'm using named captures, perl 5.10 or above is needed.

Output:

app2
anything
app5
name4
application
app3a

Upvotes: 0

sureshvv
sureshvv

Reputation: 4422

you can use the basename command to make things a bit easier

for file in *.deb; do
 name=`basename $file | sed -e 's/.*\.//' -e 's/_.*//'`
 echo $name
done

Upvotes: 0

tuxuday
tuxuday

Reputation: 3037

The best solution would be to use dpkg-query with appropriate options. Check For more information

Upvotes: 0

Related Questions