Ubuntu: Command not found error awk



Question:

I am getting this command not found error in this code. I want to print first word of a sentence

First code (result needed= 1)

abc="1 hello world"; L=$($abc|awk '{print $1}'); echo $L  1: command not found  

Second Code (result needed= mp4)

abc="mp4 hello world"; L=$($abc|awk '{print $1}'); echo $L  No command 'mp4' found, did you mean:   Command 'mpp' from package 'makepp' (universe)   Command 'mpy' from package 'yorick-mpy-mpich2' (universe)   Command 'mpy' from package 'yorick-mpy-openmpi' (universe)   Command 'mpc' from package 'mpc' (universe)   Command 'm4' from package 'm4' (main)   Command 'mp4h' from package 'mp4h' (universe)   Command 'mpv' from package 'mpv' (universe)   Command 'mpd' from package 'mpd' (universe)   Command 'mp' from package 'mp' (universe)   Command 'mpb' from package 'mpb' (universe)  


Solution:1

You need echo to pass the variable abc to STDOUT so that awk can use it as STDIN. Although you would get away in this case but always quote the variables (and command substitution) unless you have a very good reason not to (e.g. you want pathname expansion and word spitting to take place):

$ abc="1 hello world"; L="$(echo "$abc" | awk '{print $1}')"; echo "$L"  1    $ abc="mp4 hello world"; L="$(echo "$abc" | awk '{print $1}')"; echo "$L"  mp4  

You can also use here strings:

$ abc="1 hello world"; L="$(awk '{print $1}' <<<"$abc")"; echo "$L"  1    $ abc="mp4 hello world"; L="$(awk '{print $1}' <<<"$abc")"; echo "$L"  mp4  


Solution:2

NOTE: it is a good practice to put quotes around the variable you are working with so that it is the only variable you have. Also so that you're not introducing new variables in.

There are some other ways to achieve the same result.

I have some listed below.

abc="1 hello world"; echo "$abc" | awk '{print $1}'  

or

abc="mp4 hello world" && echo "$abc" | awk '{print $1}'  

or if you want a new variable, according to command substitution, both using backticks "`" and "$(command)" are still valid ways of achieving the results you are after. More information can be found here.

with backtick marks:

abc="mp4 hello world"; L=`echo "$abc" | awk '{print $1}'`; echo "$L"  

with "$(command)":

abc="1 hello world"; L="$(echo "$abc" | awk '{print $1}')"; echo "$L"  

both will produce a new variable "$L" that is assigned to what you have specified.


Solution:3

All you need is to add echo $abc instead of $abc, like so:

abc="1 hello world"; L=$(echo $abc|awk '{print $1}'); echo $L


Solution:4

The problem with the original code, as has been mentioned, is that it is missing an echo. However, though another poster mentioned that you should always quote variables, this is incorrect. Often quoting a variable will help resolve a possible ambiguous variable name, it often causes other problems by interfering with other quoting. The correct way to specify the variable, in such cases, would be the use of curly brackets around the variable name, not quote around the whole variable. For instance, the original code could be rewritten as: abc="1 hello world"; L=$(echo ${abc}|awk '{print $1}'); echo ${L}

Oh, @heemayl, you forgot to quote the $1 in your example, which is just as well as putting a quote around that variable doesn't work.


Solution:5

In your command ... $($abc|awk '{print $1}'); ..., the content of $abc is executed as command. Therefore the error.

You can test this in a terminal with:

$ 1 your text  1: command not found  

Correct your version:

abc="1 hello world"; L=$(echo "$abc"|awk '{print $1}'); echo $L  

or use a shorter version:

awk '{print $1}' <<< $(echo "1 your text")  

Example

% awk '{print $1}' <<< $(echo "1 hello world")  1    % awk '{print $1}' <<< $(echo "mp4 hello world")          mp4  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »