0

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.

Dmitry Pavliv
  • 35,333
  • 13
  • 79
  • 80
Victor S
  • 4,021
  • 15
  • 43
  • 54

3 Answers3

3

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
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • Is there any other solution to deal with cn.msn.messenger.1370207212755.apk? – Victor S Oct 28 '13 at 08:10
  • It would be difficult since there are different field separators. We could say here that last filed separated by `.` should be removed, but will not work here: `WhatsApp_2.11.93.apk` – Jotne Oct 28 '13 at 08:12
2
cat file | sed 's/\([^_]*\)\(.*apk\)/\1.apk/'
Niklas
  • 85
  • 5
1
sed "s/_[^_]*.apk$/.apk/" YourFile

assuming the last "_" is the delimiter and is not included

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43