LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-01-2009, 05:04 AM   #1
jasper25
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
find a string within another with bash script


Hello,

I would like to search for the existence of a string within another. I would like to check if a particular string is within a filename:

#!/bin/sh
search=123
for file in *.gz;
do
case $search in
$file) echo "Already renamed!";;

*)
name="$search""_""$file";
echo mv "$file" "$name";;

esac
done

This script does only work if the search matches the filename exactly
does anyone know how I can search for a part of the filename?

Thanx,
Jasper
 
Old 02-01-2009, 05:32 AM   #2
dorian33
Member
 
Registered: Jan 2003
Location: Poland, Warsaw
Distribution: LFS, Gentoo
Posts: 591

Rep: Reputation: 32
http://tldp.org/LDP/abs/html/refcards.html#AEN21400 may help.
 
Old 02-01-2009, 08:06 AM   #3
pcunix
Member
 
Registered: Dec 2004
Location: MA
Distribution: Various
Posts: 149

Rep: Reputation: 23
Quote:
Originally Posted by dorian33 View Post
That's probably the better answer because it will teach him more about Bash pattern matching, but for the specific case:

#!/bin/sh
search=123
for file in *$search*;
do
echo $file matches
done

teaches him something else he should learn (and notice what happens when "search" doesn't match at all).
 
Old 02-01-2009, 08:08 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You want something like grep.

Example (not tested):

Code:
for file in *.gz;
do
echo $(grep $search $file)
done
Get comfortable with how this works, then add other constructs until you get what you want.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
Bash Script to get the first characters of a string onesikgypo Programming 12 11-14-2008 09:57 AM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
String manipulation with BASH script King V Programming 9 04-21-2006 03:15 PM
looking for string on particular line - bash script tara Linux - General 9 12-14-2005 05:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration