0

I have a shell script to print appimage filenames inside a folder like this

#! /bin/bash

Dir="$HOME/Applications/"
Dir2="$HOME/Downloads/"

cd -P "$Dir"
for f in *.AppImage; do
z=$(echo $f | head -n1 | awk '{print $1;}')
  echo $z
done

now the output is like this :

Altus-4.8.5-x86_64.AppImage
GitHubDesktop-linux-3.2.0-linux1.AppImage
webamp-desktop-0.3.0-x86_64.AppImage
YTDownloader.AppImage

I want the output to be like following:

Altus
GitHubDesktop-linux
webamp-desktop
YTDownloader

How can i achieve this?

3
  • Biggest problem here is defining exactly what a "word" is. On a quick look, I'd have expected any non-letter to terminate the word, but based on webamp-desktop-0.3.0 -> webamp-desktop, that's not enough. What should happen if the input is something like thingy3-desktop-1.2.3.AppImage?
    – ilkkachu
    Mar 12 at 16:30
  • @ilkkachu I meant by "word" the lingual meaning of it " some letters but together and have a meaning" so webamp-desktop-0.3.0 are consisting of two words "webamp"&"desktop" and some numbers Mar 12 at 16:49
  • yes, but that doesn't fly with a computer :) You need to be specific. Note that your description contradicts what you asked earlier, since if webamp-desktop-0.3.0 is two words and some numbers, then the output containing just the first word should likely be just webamp. Of course if looking for -0 (with any digit) works, then that's fine.
    – ilkkachu
    Mar 12 at 16:57

4 Answers 4

2

Using % to remove suffixes:

for f in *.AppImage; do
  f=${f%.AppImage}
  echo "${f%%-[0-9]*}"
done
0
1

Using sed (and it's assumed no newline character in the file's name):

printf '%s\n' "$fileName" |
sed -E 's/(-[0-9].*)?\.AppImage//' 
0
0

Using GNU grep:

<INTPUT> | grep -oP '^\D+(?=-\d|\.)' 

Using Perl:

<INPUT> | perl -nE 'say $& if /^\D+(?=-\d|\.)/' 

Output

Altus
GitHubDesktop-linux
webamp-desktop
YTDownloader

The regular expression matches as follows:

Regex Description
^\D+ Match 1 or more non-digit characters at the beginning of the regex
(?=-\d|\.) Perform a lookahead, attempting to match a hyphen and a number, or a period, but do not include it in the selection.
0

Assuming none of your file names contain newlines then using any awk:

printf '%s\n' *.AppImage | awk -F'[.]|-[0-9]' '{print $1}'

If they can contain newlines then using GNU awk or any other version that can use NUL for the RS:

printf '%s\0' *.AppImage | awk F'[.]|-[0-9]' 'BEGIN{RS=ORS="\0"} {print $1}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .