LinuxQuestions.org
Visit Jeremy's Blog.
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 06-07-2005, 07:09 AM   #1
orgazmo
Member
 
Registered: May 2005
Location: Paris, france
Posts: 53

Rep: Reputation: 15
Read a word in a string with bash


Hi all!!!

Today i've got a new problem.

I'm Trying to extract a word from a string that look like that:

"695856 5859 66:55:66 99:77:55"

In fact i want a command that would allow me to extract, for example, the third word (here 66:55:66)

Thanks
 
Old 06-07-2005, 07:14 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
awk seems just right for that, eg:
awk '{print $3}'

Yves.
 
Old 06-07-2005, 07:24 AM   #3
orgazmo
Member
 
Registered: May 2005
Location: Paris, france
Posts: 53

Original Poster
Rep: Reputation: 15
Ok thank you Yves.

but where do i specify the string to be used?
 
Old 06-07-2005, 07:30 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
awk reads from standard input or from a file (much like sed). So if your text is in a file, then this will do:
Code:
awk '...' a_file
If your text is produced by other commands, do this:
Code:
other_commands | awk '...'
Finally, if your text is built inside a script, bash will tell you how to "build a custom" standard input. Two most common solutions are:
- Long text:
Code:
awk '...' <<-FIN
Ligne 1 ${objet} coute \$${prix} !
Lighe 2 ...
FIN
- Short text:
Code:
awk '...' <<<"text with ${var} if you want"
Yves.
 
Old 06-07-2005, 07:34 AM   #5
orgazmo
Member
 
Registered: May 2005
Location: Paris, france
Posts: 53

Original Poster
Rep: Reputation: 15
Ok but i can use the pipe (other command | awk ... ) in my script, it makes no difference, does it?
 
Old 06-07-2005, 09:54 AM   #6
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Pipe is OK.
 
Old 06-07-2005, 10:19 AM   #7
orgazmo
Member
 
Registered: May 2005
Location: Paris, france
Posts: 53

Original Poster
Rep: Reputation: 15
thanks, it work just as i excpected!!!
 
  


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
bash scripting capitalizing first letter or each word in a string hypexr Programming 6 06-16-2011 09:58 AM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
please read it i dont know the exact word to use to describe wat i wanna say pranith Slackware 5 05-30-2005 07:27 AM
[BASH] How to filter characters away? !{param#word} {param%word}! Dark Carnival Programming 8 03-17-2005 01:49 PM
read word on terminal? khucinx Programming 1 06-09-2004 04:04 AM


All times are GMT -5. The time now is 04:53 AM.

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