-2

we can easily to capture the first field by awk as this syntax

 capture=` echo 1 2 3 | awk '{print $1}' `

we can easily to capture the sec field by awk as this syntax , and so on

 capture=` echo 1 2 3 | awk '{print $2}' `

but I prefer to avoid to use echo just to capture the requested field

what is the same results with bash ?

my goal is to do it more simple and if we can not use echo - then its better

jango
  • 423
  • 1
    What are you actually trying to do? Why not just write your script in Awk? Awk is a programming language, not just for one-liners. – Wildcard Jan 18 '18 at 09:04
  • I just not want to use echo , we are not talking here about awk , awk is just fine , but want to avoid using echo , ot any other approach with echo – jango Jan 18 '18 at 09:11
  • so hope that my question now is more clearly if not please ask any want to put on the table and I will promise I will answer on any question , but I just not understand why I got (-2) what is wrong with my question ? – jango Jan 18 '18 at 09:19
  • another thing - echo it command that take resource from the CPU , this is another point that need to avoid echo when need to make a fast scripts – jango Jan 18 '18 at 09:20
  • 2
    @jango, if you care about speed / resource use, to the level that a shell builtin command matters, stop using the shell. It's measurable slower than, say, awk or Perl, even for the tasks that can be done just in the shell, without forking off other processes. – ilkkachu Jan 18 '18 at 09:37
  • the shell is primarily a tool for running other programs (either individually or in a pipeline). text processing languages like awk or sed or perl are tools for processing text. modern shells have gained more and more basic text-processing features as built-ins, but they're never going to do as good a job as tools that specialise in that task, and they're going to be much slower at it. e.g. awk and perl (but not sed) can both easily split things into fields and work with the fields directly. bash can't - it "recently" acquired some sed-like capabilities for performing regexes on strings. – cas Jan 18 '18 at 09:46
  • 3
    Your first example can be replaced by capture=1, the second by capture=2. You see the problem? We need to know what the source of the data is to give the matching answer. Of course it's useless to echo a string just to modify it. So what do you really want to do? – Philippos Jan 18 '18 at 10:21
  • it's not useless to echo (or printf) a string just to modify it. that has been THE way to modify strings in sh/bash/ksh/etc since the dawn of time :) – cas Jan 18 '18 at 10:23
  • @cas, it's useless to echo a hardcoded string just to modify it, which was (obviously) what Philippos meant. :) – Wildcard Jan 18 '18 at 12:55
  • i assumed the 1 2 3 was just a placeholder example. the question is completely pointless otherwise. nitpicking about such trivia seems even more pointless. – cas Jan 18 '18 at 13:32

1 Answers1

2

Leaving aside whether the shell is the suitable tool here for a moment, you could replace it with a herestring as you say you’re using bash.

capture=$( awk ‘{ print $1 }’ <<< “1 2 3” )

More info https://unix.stackexchange.com/a/80372

Guy
  • 894