0

I work with bash script .I want get value 89 from this line

var_val --------- 89 (1 row)

Can anybody help me?

mindia
  • 6,991
  • 4
  • 22
  • 20
  • 1
    What do you want to do with this value? Print it out, assign it to a variable, ...? – Floris Jul 12 '13 at 21:19
  • Also - is this line the only line in a file, is it passed in to the script as a variable, is this the exact line or "a general form", etc. If you provide more information (and show an attempt to solve this yourself), the quality of the answers will improve (and people will stop downvoting your question). – Floris Jul 12 '13 at 21:25

2 Answers2

4

Assuming that the line is the only line in file inputFile (you did say "1 row"):

1) Assign to a variable

thevalue=`awk '{print $3}' < inputFile`

2) Echo to screen:

awk '{print $3}`

Of course I am making all kinds of assumptions about the general nature of the string - is it always "the third word" you want, is it always "the only line in the file", etc.

Floris
  • 45,857
  • 6
  • 70
  • 122
4

Cut command should work:

cut -d " " -f3 inFile

To assign this value to a shell variable:

val=$(cut -d " " -f3 inFile)
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    +1 for a perfectly good alternative to the one I gave... Nice to show the two ways to assign a value side by side in the two answers `$(command)` and `backtick command backtick` - (I know they are not identical, but both work in this instance). I never did figure out how to include backticks in a code-formatted comment... – Floris Jul 12 '13 at 21:56
  • @Floris: Your answer is also perfectly valid, both awk & cut can work. And even I haven't figured out how to use `\`backticks\`` in comments :P – anubhava Jul 12 '13 at 22:04
  • @Floris: I just figured out using backticks in comments :P Just escape them using backslash like `\`this\``. – anubhava Jul 12 '13 at 22:05
  • That's `\`cool`\`! Thanks for that. It's obvious in retrospect but it had been driving me nuts. "Why didn't I think of that myself". – Floris Jul 12 '13 at 22:15