Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder with the following files:

ondemand_kvm131_img.2
vm114_img.2
vm114_img.3
ondemand_vm205_img.3
label_ondemand_kvm131_img.1
vm117_img.1

I'd like to only display the "names", so in this case the output should be

kvm131
vm114
vm114
vm205
kvm131
vm117

How can this be done? By using sed? Grep? Thanks in advance.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Try this:

ls -1 | sed 's/_img.*//;s/.*_//'
share|improve this answer
    
Works excellent, thanks very much! –  Devator Sep 27 '12 at 1:16

There are a number of ways this can be done, depending on the precise formatting of the names and what you plan to do with it. The most simple would probably be a ls | sed:

$ ls | sed 's/^\(.*_\)*\([^_]*\)_img\.[0-9]/\2/'
kvm131
kvm131
vm205
vm114
vm114
vm117

If your file names are sane and you just want a visual look this will do fine, but if you're doing more with the names there is the potential for errors on odd file names. You can get them individually like so:

$ for file in *; do left=${file%_img.?}; base=${left##*_}; echo $base; done 
kvm131
kvm131
vm205
vm114
vm114
vm117

Replace the echo with your command of choice. Both of these can be altered to match the format more closely, if you have a different format overall.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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