The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Redirect to a Variable? Or is it cmd cap? oxoxo UNIX for Dummies Questions & Answers 2 11-22-2008 08:59 AM
redirect cat to variable shailesh_arya Shell Programming and Scripting 7 07-10-2008 05:34 AM
Redirect to variable MrAd UNIX for Dummies Questions & Answers 2 05-07-2007 05:18 PM
Redirect from Variable to command line?? ugh UNIX for Dummies Questions & Answers 8 03-28-2006 11:31 AM
redirect command output to variable hugow UNIX for Dummies Questions & Answers 1 06-22-2005 07:43 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-26-2009
dhibbit dhibbit is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
variable redirect messing up a sed command.

Not sure if that title is decent, but here we go. I'm running this string of commands:


Code:
qstat -f $1 | sed 's/^[ \t]*//' | sed -n -e ":a" -e "$ s/\n//gp;N;b a" | sed 's/\\,/,/' | awk -F"PBS_O_WORKDIR=" '{print $2}' | awk -F",PBS_O_SYSTEM" '{print $1}'

In case you're curious is takes the output of a PBS queue, removes all the leading spaces, removes all the linebreaks, removes the backslash escape character in front of any commas, then grabs the output between two strings, effectively extracting a variable from a large mess of output from PBS.

When I run that, it works just fine, all steps work perfectly. However, when I direct the output into a variable, the sed command to remove the backslash fails.

So, I simplified the problem:

echo "Hello\,World" | sed 's/\\,/,/' --> "Hello,World"
var=`echo "Hello\,World" | sed 's/\\,/,/'`; echo $var --> "Hello\,World"

any thoughts?
  #2 (permalink)  
Old 05-26-2009
JerryHone JerryHone is offline
Registered User
  
 

Join Date: Nov 2006
Location: UK
Posts: 178
"Cascading backslashes" a.k.a "Leaning toothpicks"!


Code:
var=`echo "Hello\,World" | sed 's/\\\,/,/'`; echo $var --> "Hello,World"

seems to work!

It's because the backticks are spawning another subshell, but the original backslashes have already been interpreted by the first one.
  #3 (permalink)  
Old 05-27-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,418
this simple tr will do that

Code:
echo "Hello\,World"|tr -d '\\'

  #4 (permalink)  
Old 05-27-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Or


Code:
var=$(echo "Hello\,world" | sed 's/\\,/,/g'); echo $var


-Devaraj Takhellambam
Closed Thread

Bookmarks

Tags
backquotes, backslashes, redirect, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0