Why does awk always print full lines? [duplicate] - bash

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

Related

Bash unable to assign value [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 1 year ago.
Got a bit of a problem. I'm new to bash and I'm trying my hardest, but I can't figure out how to assign the desired output to the variable. Running this command in the prompt
wc -l data.txt | awk '{ print $1 }'
yields the result 12, which is desired. However if I put it in the test.sh file, it won't work. I've tried different quotations, but all I've managed to get is the entire line as a string...
Test.sh
#! /bin/bash
# Count lines for data.txt files
data1=wc -l data.txt | awk '{ print $1 }'
echo "Lines in data.txt: $data1"
exit
I think you want:
data1="$(wc -l data.txt | awk '{ print $1 }')"
The $() syntax causes bash to execute that expression and replace it with the results.
Actually, powershell does allow you to do a straight = assignment like you did...

How can I delete empty line from my ouput by grep? [duplicate]

This question already has answers here:
Remove empty lines in a text file via grep
(11 answers)
Closed 4 years ago.
Exists way to remove empty lines with cat myfile | grep -w #something ?
I looking for simple way for remove empty lines from my output like in the way the presented above.
This really belongs on the codegolfing stackexchange because it's not related to how anyone would ever write a script. However, you can do it like this:
cat myfile | grep -w '.*..*'
It's equivalent to the more canonical grep ., but adds explicit .*s on either side so that it will always match the complete line, thereby satisfying the word boundary conditions imposed by -w
You can pipe your output to awk to easily remove empty lines
cat myfile | grep -w #something | awk NF
EDIT: so... you just want cat myfile | awk NF?
if you have to use grep, you can do grep myfile -v '^[[:blank:]]*$'

Bash script sometimes not working [duplicate]

This question already has answers here:
How can I use a file in a command and redirect output to the same file without truncating it?
(14 answers)
Closed 5 years ago.
Okay, so i have strange problem with the following piece of code:
who > tmp
cat tmp | awk '{print $1}' | sort | uniq > tmp
ps aux | grep -Fvf tmp
It is supposed to list processes of all users not logged in at the moment. Problem is it sometimes works, and sometimes doesn't and I have no idea what causes it. I can enter exactly same commands and I get 2 different results. I've narrowed the problem to 2nd line > tmp redirect, where it saves proper user list or wipes the file completely and I have no idea why it happens.
PS. I know it may not be proper solution for the task, but it's what I came up with during limited time I was given.
It's probably a timing issue: you're reading from and truncating the file in the same pipeline.
The simple solution is to not use temp files at all:
ps aux | grep -Fvf <(who | awk '{print $1}' | sort -u)

Get the extension of file [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 7 years ago.
I have files with "multiple" extension , for better manipulation I would like to create new folder for each last extension but first I need to retrieve the last extension.
Just for example lets assume i have file called info.tar.tbz2 how could I get "tbz2" ?
One way that comes to my mind is using cut -d "." but in this case I would need to specify -f parameter of the last column, which I don't know how to achieve.
What is the fastest way to do it?
You may use awk,
awk -F. '{print $NF}' file
or
sed,
$ echo 'info.tar.tbz2' | awk -F. '{print $NF}'
tbz2
$ echo 'info.tar.tbz2' | sed 's/.*\.//'
tbz2

How to pass variable to awk [duplicate]

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.

Resources