LinuxQuestions.org
Help answer threads with 0 replies.
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-19-2008, 03:42 PM   #1
Gracie Belle
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: 0
Unhappy help! wildcard expansion in variables using sed???


Sed isn't parsing the '*' symbol to automatically expand a variable term

this is what my code looks like

infile=/data/*DTI*/somefile
outfile=/data/*DTI*/tempfile

sed 's/whatever/ever/' "$infile" > "$outfile"


the file paths are actually something like /data/3450_DTI_33452/somefile
if i do echo $infile it prints the path correctly,
however sed reads $infile literally as /data/*DTI*/somefile and thus cannot find the file.

anyone know anyway around this???

help!

Last edited by Gracie Belle; 02-19-2008 at 03:51 PM.
 
Old 02-20-2008, 08:09 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Remove the quotes from $infile and $outfile.

The "*" does expansion in BASH, but not in SED. The double quotes are telling BASH to not process any special character except the "$", and to pass the string literally to SED.
 
Old 02-20-2008, 08:22 AM   #3
Gracie Belle
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Cool

beautiful! thank you!
 
Old 02-20-2008, 08:43 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Quote:
Originally Posted by Gracie Belle View Post
Sed isn't parsing the '*' symbol to automatically expand a variable term

this is what my code looks like

infile=/data/*DTI*/somefile
outfile=/data/*DTI*/tempfile

sed 's/whatever/ever/' "$infile" > "$outfile"


the file paths are actually something like /data/3450_DTI_33452/somefile
if i do echo $infile it prints the path correctly,
however sed reads $infile literally as /data/*DTI*/somefile and thus cannot find the file.

anyone know anyway around this???

help!
If you have to matching infile directories:
3450_DTI_33452/somefile
3451_DTI_33452/somefile

The infile will become:
3450_DTI_33452/somefile 3451_DTI_33452/somefile

So the sed line would expand to

sed 's/whatever/ever/' 3450_DTI_33452/somefile 3451_DTI_33452/somefile > 3450_DTI_33452/tempfile 3451_DTI_33452/tempfile

Which isn't what you want. You could either edit them in place:
sed -i 's/whatever/ever/' "$infile"

or use a loop:
for infile in /data/*DTI*/somefile; do
sed 's/whatever/ever/' "$infile" > "${outfile/somefile/tmpfile}"
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Sed and variables oden Programming 9 07-29-2007 05:36 PM
Bash variables expansion olaola Linux - Newbie 4 10-16-2006 12:45 PM
bash in ubuntu: save the wildcard expansion beebop Programming 0 03-12-2006 02:19 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
passing variables to sed jjfate Programming 8 07-31-2003 05:15 AM


All times are GMT -5. The time now is 06:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration