LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Read a word in a string with bash (https://www.linuxquestions.org/questions/programming-9/read-a-word-in-a-string-with-bash-331178/)

orgazmo 06-07-2005 07:09 AM

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

theYinYeti 06-07-2005 07:14 AM

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

Yves.

orgazmo 06-07-2005 07:24 AM

Ok thank you Yves.

but where do i specify the string to be used?

theYinYeti 06-07-2005 07:30 AM

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.

orgazmo 06-07-2005 07:34 AM

Ok but i can use the pipe (other command | awk ... ) in my script, it makes no difference, does it?

theYinYeti 06-07-2005 09:54 AM

Pipe is OK.

orgazmo 06-07-2005 10:19 AM

thanks, it work just as i excpected!!!


All times are GMT -5. The time now is 12:11 AM.