LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-03-2011, 10:41 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,067

Rep: Reputation: 45
Sed: assigning its output to a variable.


Kernel 2.6.21.5, GNU/Linux (Slackware 12.0).

Hi:
In this script,
Code:
semoi@darkstar:~$ cat rename4.sh
#!/bin/bash

INPUT="k3b_audio_0_04"
echo $INPUT | sed s/k3b_audio_0_// > OUTPUT
echo "Input=  $INPUT"
echo "Output= $OUTPUT"
semoi@darkstar:~$
I would like the output to be
Code:
$ Input=  k3b_audio_0_04
$ Output= 04
But how do I send sed's output into OUTPUT? Regards.

EDIT: I found it:
OUTPUT=$(echo $INPUT | sed s/k3b_audio_0_//)

Last edited by stf92; 02-03-2011 at 10:50 PM.
 
Old 02-03-2011, 10:52 PM   #2
lucmove
Member
 
Registered: Aug 2005
Location: Brazil
Distribution: Lubuntu, Slackware
Posts: 575

Rep: Reputation: 64
Code:
semoi@darkstar:~$ cat rename4.sh
#!/bin/bash

INPUT="k3b_audio_0_04"
OUTPUT=$(echo $INPUT | sed s/k3b_audio_0_//)
echo "Input=  $INPUT"
echo "Output= $OUTPUT"
You can also do this:

Code:
OUTPUT=`echo $INPUT | sed s/k3b_audio_0_//`
But backticks make the code look kind of "dirty", are much harder to escape if the code inside them also contains backticks, and they are not a subshell. The $(CODE) format is a lot cleaner, embeddable and it creates a subshell.
 
1 members found this post helpful.
Old 02-04-2011, 12:04 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,067

Original Poster
Rep: Reputation: 45
Yes. As you can see, I had figured out a way out minutes before you pressed 'Submit Reply'. The second form I'll bear it in mind, and the morals behind it. Thanks a lot.
 
Old 02-04-2011, 12:38 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,432

Rep: Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878Reputation: 1878
Or in this case you could have just used parameter substitution:
Code:
OUTPUT=${INPUT##*_}
 
1 members found this post helpful.
  


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 output -> variable problem figo Programming 13 05-31-2009 09:26 AM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
in C, Assigning output of system() to a variable Miaire Programming 4 01-30-2005 12:40 PM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM
Assigning the output of one command to a variable (shell) guru_stew Programming 5 08-03-2003 06:12 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration