Sending Sed/Echo output to Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sending Sed/Echo output to Variable
# 1  
Old 08-28-2012
Sending Sed/Echo output to Variable

I have a variable $WORDS that contains a string
Quote:
one two @three !four
Then i want to use sed to break it up.
Code:
echo $WORDS | sed 's/[!-/:-@[-`{-~]/ /g'

I tried setting this as a variable by doing
Code:
WORDS2=`echo $WORDS | sed 's/[!-/:-@[-`{-~]/ /g'`

But when i do this it does not return me to the prompt properly
ie.
Code:
jmpprd-v1>
jmpprd-v1> set WORDS2=`echo $WORDS | sed 's/[!-/:-@[-`{-~]/ /g'`
>
>

Then i have to ctrl+c to exit and it does not set it.
What am i missing here?
# 2  
Old 08-28-2012
Hi
Adding a backslash

Code:
WORDS2=`echo $WORDS | sed 's/[!-/:-@[-\`{-~]/ /g'`

Guru.

Last edited by guruprasadpr; 08-28-2012 at 02:48 AM.. Reason: added text
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 08-28-2012
In the line:
Code:
set WORD2=`echo $WORDS | sed 's/[!-/:-@[-`{-~]/ /g'`

you have an odd number of back-ticks (highlighted in red). Hence, the shell is waiting for another back-tick to complete the command line and displaying the secondary prompt.

Use what guruprasadpr suggested or use:
Code:
typeset WORDS2=$(echo $WORDS | sed 's/[!-/:-@[-`{-~]/ /g')

Also, I think that set should be typeset. Or are you using C shell?

Last edited by elixir_sinari; 09-27-2012 at 08:16 AM..
# 4  
Old 08-28-2012
Alternatively you may use tr too:
Code:
echo $WORDS | tr -d '!-/:-@[-\`{-~'

# 5  
Old 08-28-2012
thanks for all of the suggestions/help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Echo awk output from its variable

Stumped with the formatting of the awk output when used with variables, e.g.: awk -F, 'BEGIN {OFS=","} print {$2,$3,$4}' $infile1 produces the desired output (with rows), but when echoing the variable below, the output is one continuous line var1=$(awk -F, 'BEGIN {OFS=","} print... (4 Replies)
Discussion started by: ux4me
4 Replies

2. Shell Programming and Scripting

Trim sed output & assign to variable

Hi, I have the following command that parses an xml file to read a node <port>'s value. Hoever the output comes with spaces. My requirement is to trim the spaces around the value and assign to a variable. sed -n 's|<port>\(.*\)</port>|\1|p' ../cfg.xml How do I go about it? (6 Replies)
Discussion started by: sai2013
6 Replies

3. Shell Programming and Scripting

Sending sqlplus output to a shell variable

I am trying to import a sqlplus output into a shell variable but it doesnt seem to be working. set -x export DEPENDENT_CR_NO=`sqlplus -s /nolog <<EOF conn username/passwd set heading off select dependency from custom_patches where patch_name='PATCH.zip'; exit; EOF` echo $DEPENDENT_CR_NO ... (2 Replies)
Discussion started by: beginer314
2 Replies

4. Shell Programming and Scripting

cannot pass a echo output to a variable in bash

Hi, I have a problem with passing a echo output into a variable in bash file='1990.tar' NAME='echo $file | cut -d '.' -f1'; echo $NAME the result is echo $file | cut -d . -f1 however with this one,#!/bin/bash file='1990.tar' echo $file | cut -d '.' -f1 the result is what I... (2 Replies)
Discussion started by: 1988PF
2 Replies

5. Shell Programming and Scripting

Problem with variable ECHO $((SED...

Hi, I'm new here so I want to say hello to everyone first! I searched google and this forum for a similar problem, but wasn't successful #! /bin/bash I'm trying to output (echo) n lines of a text file to the screen (later into another file). But I have problem with the sed command, it won't... (1 Reply)
Discussion started by: studiologe
1 Replies

6. UNIX for Dummies Questions & Answers

Appending sed output to variable

I want to append matched output and cat the results into an variable. but I've been running into problems. sed is printing result on to screen instead of appending the output to $CAPTURE. I'm stumped...how should i fix this? contents of $TEST 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 expected... (5 Replies)
Discussion started by: jazzaddict
5 Replies

7. Shell Programming and Scripting

Echo - Sending mail to multiple addresses

Hi, If I want my script to send a mail to multiple recipients I can do the following: if then echo $err_string1 | mailx -s "UAT CPU ALERT" 1@email.com echo $err_string1 | mailx -s "UAT CPU ALERT" 2@email.com fi Can this also be done something like: ... (1 Reply)
Discussion started by: runnerpaul
1 Replies

8. Shell Programming and Scripting

storing output from echo & cut into variable

Hi All, Hope someone can advise here as I have been struggling to find a syntax that works here. I have tried a stack of combination I have seed in the forums but I think because I have needed to use "" and `` in the statments another method is found. I am reading in lines with the following... (1 Reply)
Discussion started by: nkwilliams
1 Replies

9. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

10. Shell Programming and Scripting

Passing output of sed/echo to a variable

I understand how to use a variable in a sed command, but for the life of me I can't get the output into a variable. I'm making a general function to replace part of a filename with a different string, so: >>myscript this that would change: this_file001.txt to that_file001.txt and... (11 Replies)
Discussion started by: donflamenco
11 Replies
Login or Register to Ask a Question