This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using awk with variables
The following command is wrong, the point is I want to use $curLineNumber in awk, how can I do it? Any solution?
curLineNumber = 3
curTime=`ls -l | awk 'NR==$curLineNumber {print $NF}'`
Thanks
curTime=$(ls -l | awk -v line=$curLineNumber 'NR == line { print $NF }'
The -v option is used to specify variables initialized on the command line. I chose the name line for the awk variable.
Related
This question already has an answer here:
awk: fatal: cannot open file `' for reading (No such file or directory)
(1 answer)
Closed 2 years ago.
Using awk when I try to parse the last folder I get error "No such file or directory". How do I pass variable to awk
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName=$(awk -F/ '{print $NF}' $CacheLocation)
awk does not work on strings, it expects the input as file or stdin
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$(awk -F/ '{print $NF}' <<<"$CacheLocation" )"
In this special case, it might be easier to use basename
CacheLocation="/home/dir1/tempdl/abc-cache"
cacheFolderName="$( basename "$CacheLocation" )"
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 3 years ago.
When attempting to use awk to get the process ID from the output of ps aux like so:
ps aux | awk "{ print $2 }"
No matter what number of row I attempt to print, awk always outputs the full line. I've never managed to get it to work properly. I'm using macOS which apparently uses a different type/version of awk, but I can't find an alternative syntax which might work.
Any advice would be greatly appreciated!
ps aux | awk '{ print $2 }'
Try that one
This question already has an answer here:
How can I retrieve an entry from /etc/passwd for a given username?
(1 answer)
Closed 5 years ago.
I have this line
Username:x:120:101:somethingsomething
and I need to get the '101' part after the third ':', how can I do that?
do I use grep or sed?
cut -d':' -f4 /etc/passwd
awk, only with string:
mstr="Username:x:120:101:somethingsomething"; awk -F: '{print $4}' <<< "$mstr"
This question already has answers here:
How do I use shell variables in an awk script?
(7 answers)
Closed 7 years ago.
echo boy:foo:cheese | awk -F":" '{print $1}'
Result: boy
echo boy:foo:cheese | awk -F":" '{print $2}'
Result: foo
i=1
echo boy:foo:cheese | awk -F":" '{print $($i)}'
Result: boy:foo:cheese
Also,
i=1
echo boy:foo:cheese | awk -F":" '{print $i}'
Result: boy:foo:cheese
I want to be able to print the ith item. The only possible reason this happens is that awk doesn't support variables for its print values? My end goal here is to loop through the string and get:
boy
foo
cheese
Figured it out!
echo "cat:test:cheese" | awk -F":" '{print $'$i'}'
cat
You appear to have noticed that shell variables and awk positional/field variables share a syntax $# but failed to consider that that means awk can't expand both of them.
Either the shell expands the variable or the shell does, not both.
The single quotes around the awk script mean the shell doesn't so awk does. So '{print $2}' tells awk to print the second field. Similarly for $1 telling awk to print the first field. Whereas, '{print $($i)}' tells use the awk variable i and then use that value as a positional/field variable ($<i>) and then use that as yet another positional/field variable $(<$<i>>).
Variables in awk default to 0 when uninitialized so that sequence is:
$($i)
$($0)
$(input line)
``
which then causes awk to dutifully print nothing.
The correct way to use a shell variable is to pass it to awk using -v var="$1" and then using var in awk. Non-positional/non-field variables in awk do not use the $ sigil.
This question already has answers here:
Using awk with variables
(3 answers)
Closed 8 years ago.
As a part of my bash, I want to pass some constant from command line to awk. For example, I want to subtract constant1 from column 1 and constant2 from column 5
$ sh bash.sh infile 0.54 0.32
#!/bin/bash
#infile = $1
#constant1 = $2
#constant2 = $3
cat $1 | awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6}'
thank you very much for your help
As awk is it's own language, by default it does not share the same variables as Bash. To use Bash variables in an awk command, you should pass the variables to awk using the -v option.
#!/bin/bash
awk -v constant1=$2 -v constant2=$3 '{print($1-constant1),($5-constant2)}' $1
You'll notice I removed cat as there is no need to pipe cat into awk since awk can read from files.
you need to remove gaps when defining vaariables:
#!/bin/bash
infile=$1
constant1=$2
constant2=$3
cat $1 | awk '{print $1 $2 $3 $4 $5 $6}'