Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

Picking out the right file when ls | greping

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
6 posts • Page 1 of 1
Author
Message
XST1
Apprentice
Apprentice
Posts: 163
Joined: Tue Jan 27, 2004 12:11 am

Picking out the right file when ls | greping

  • Quote

Post by XST1 » Tue Feb 15, 2005 11:44 pm

I'm writing a script where I have to use ls | grep and search for a file in the current directory. I am assigning the name of this file to a variable so it needs to be precise. My only problem is that the file that I am grepping for has a brother which happens to have a different extension. I want to pick out the right file and send it to the variable.

Heres an example just to clarify:

I am in /home/m/temp

in the script I say:

a=`ls | grep myFile`

the value now stored in a is: myFile.exe myfFile.dll

I only want to store myFile.exe

How would I do this? Thanks.
Top
hollerith
Apprentice
Apprentice
User avatar
Posts: 204
Joined: Fri May 21, 2004 10:56 am

script tools

  • Quote

Post by hollerith » Tue Feb 15, 2005 11:55 pm

use 'sed' and 'cut'
Top
oberyno
Guru
Guru
Posts: 467
Joined: Sun Feb 15, 2004 10:39 am
Location: /bin/zsh

  • Quote

Post by oberyno » Wed Feb 16, 2005 12:10 am

Is there any reason why you can't do a test? The following would test if both myfile.dll and myfile.exe exist. If both exist, then set $a to myfile.exe.

Code: Select all

[[ -e myfile.dll && -e myfile.exe ]] && a=myfile.exe
Top
BitJam
Advocate
Advocate
Posts: 2513
Joined: Tue Aug 12, 2003 4:15 pm
Location: Silver City, NM

  • Quote

Post by BitJam » Wed Feb 16, 2005 12:12 am

I think you need to provide more information in order to precisely define the problem that you want solved. Are you always looking for a specific extension or always trying to ignore a specific extension? Also, it is not clear if you want to script the strip off the extension or leave it on. Let's say you are looking for a file named "dum.exe" and want to end up with the name "dum". This code should work:

Code: Select all

file=`ls *.exe | grep dum`
echo file is ${file%%.exe}
Top
XST1
Apprentice
Apprentice
Posts: 163
Joined: Tue Jan 27, 2004 12:11 am

  • Quote

Post by XST1 » Wed Feb 16, 2005 1:14 am

say I had "myfile.exe" stored in $a. How would I delete only the ".exe" part of it (without the quotes)?
Top
lookinin
Guru
Guru
Posts: 486
Joined: Fri Jan 21, 2005 2:15 pm

  • Quote

Post by lookinin » Wed Feb 16, 2005 1:43 am

This is basically the same as what BitJam said, but doesn't care what the extention actually is.

Code: Select all

echo ${a%%.*}
Additionally, if you $a contains "myfile.exe.exe", this would return "myfile.exe", as it only works on the last ".":

Code: Select all

echo ${a%.*}
Top
Post Reply

6 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic