linuxsedawkvikingsoft

How to extract file name from file list?


Now I have several files, their names include version number, I want to extract their file name only and strip the version number,

ES File Explorer_3.0.5.5.apk
ES Task Manager_1.4.1.apk
Facebook_3.7.apk
Gmail_4.6 (836823).apk
Google+_4.1.2.51968121.apk
Google Play Books_2.9.21.apk
Google Search_2.8.8.849369.arm.apk
Hangouts_1.2.018 (849105-30).apk
Instagram_4.1.2.apk
Kingsoft Office_5.7.3.apk
Maps_7.2.0.apk
Skype_4.4.0.31369.apk
Translate_2.8.apk
Twitter_4.1.8L.apk
WhatsApp_2.11.93.apk
YouTube_5.1.10.apk

How to do? using vi, sed or awk? Such as extract YouTube or YouTube.apk from YouTube_5.1.10.apk.


Solution

  • Using awk

    awk -F_ '{print $1 ".apk"}' file
    ES File Explorer.apk
    ES Task Manager.apk
    Facebook.apk
    Gmail.apk
    Google+.apk
    Google Play Books.apk
    Google Search.apk
    Hangouts.apk
    Instagram.apk
    Kingsoft Office.apk
    Maps.apk
    Skype.apk
    Translate.apk
    Twitter.apk
    WhatsApp.apk
    

    A version to fix cn.msn.messenger.1370207212755.apk

    awk '{gsub(/[0-9].*|_/,x);gsub(/\.$/,x)} {print $0 ".apk"}' file