LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2005, 09:34 AM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Simple bash/awk/sed scripting question


I'm a little bit confused about the use of awk/sed in a script. I've skimmed through the guides to both and it seems to me like they only operate on files/stdin/stdout, is that right? What I am trying to do is write a simple expression to extract a substring from a bash variable and re-assign the variable to that substring. Specifically the variable consists of an executable's name (+ it's path) and it's inputs. So something like this:


Code:
exec_target="/usr/bin/gcc -c my_program my_program.c"
# And here I want to write an expression to extract "gcc" and reassign it to exec_target

I know there are multiple ways of doing this and I could do it in the blink of an eye if this script I wrote was in Perl, but what's a fast, simple way of doing it in the Bash world and what tool (awk/sed) is best suited for this type of task? Thanks
 
Old 04-12-2005, 09:44 AM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
I think this will do what you want, but I may be barking up the wrong tree...
Code:
exec_target=$(echo $exec_target | sed -e 's/.*\///')
 
Old 04-12-2005, 10:10 AM   #3
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
Oh, so sed uses the same matching operator as perl does it? Well that's good to know.

Yeah that command yields this:

Code:
exec_target="/usr/bin/gcc -c my_program my_program.c"
exec_target=$(echo $exec_target | sed -e 's/.*\///')
echo $exec_target

> gcc -c my_program my_program.c
So it gets rid of the path, but not the whitespace/other args. I can do that myself though. Thanks!
 
Old 04-15-2005, 03:01 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
exec_target=`echo $exec_target|cut -d' ' -f1`
echo $exec_target
/usr/bin/gcc
exec_target=`basename $exec_target`
echo $exec_target
gcc
 
Old 04-16-2005, 02:55 AM   #5
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
Awk is such a lovely tool

Code:
[/home/soumen/tmp] $ exec_target="/usr/bin/gcc -c my_program my_program.c"
[/home/soumen/tmp] $ exec_target=`echo $exec_target |awk '{print a[split($1,a,"/")]}'`
[/home/soumen/tmp] $ echo $exec_target
gcc
HTH.
 
  


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
Forum on scripting, sed, awk, etc.?? pixellany Programming 3 12-01-2005 01:16 PM
Very simple BASH scripting question clickster Linux - Newbie 6 11-23-2005 04:28 PM
Simple question about sed or awk setianusa Programming 2 09-16-2005 03:57 PM
How to loop or sort in bash, awk or sed? j4r0d Programming 1 09-09-2004 03:22 AM
Bash Scripting--Simple Question mooreted Linux - General 4 05-10-2004 01:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 AM.

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