Why is my linux command with awk not working - linux

i am trying to let this command work but it won't let me do anything
awk -F: ‘{if($3>'1000') print$1}’ passwd | sort > users.txt
I get an error which is saying:
bash: syntaxfout nabij onverwacht symbool '('
Can someone help me out?

You're using ‘ instead of '. And then, you should replace ' with " in the awk program (or just leave them out):
awk -F: '{if ($3 > 1000) print $1}' passw | ...

You're using backticks instead of single quotes. Try:
awk -F: '{if($3>1000) print $1} passwd | sort > users.txt
or just
awk -F: '$3>1000 {print $1}' passwd | sort > users.txt

Related

Script : check 8th field of /etc/shadow

I want to check if the 8th field of /etc/shadow of a username has no entry.
This is my code:
#!/bin/bash
for i in $(cat < "users.txt")
do
sudo grep -w $i /etc/shadow | awk -F: "$8 == ' '" | cut -d: -f1 ;
done
But this is the error that i get when i execute the script
awk: line 1: syntax error at or near ==
awk syntax for this purpose would be:
awk -F 'delim' '$n1 == "text" {print $n2}'
sudo grep -w $i /etc/shadow | awk -F':' '$8 == " " {print $0}'
FYI: /etc/shadow does not contain spaces between colons. so if cat shows
bin:*:1:1:::::::
you should run $8 == ""
note that there is no space.
If n2 is 0, you'd return the entire row. Hope this helps!
Your approach can be greatly simplified, typically using grep and awk on one line indicates that you're overthinking things. Invoking one command instead of three ...
#!/bin/bash
for i in $(cat < "users.txt")
do
sudo awk -F: -v user=$i '$1==user && $8==""{print $1}' /etc/shadow
done

"awk" is not processing shell variables as expected

I am working on filtering Specific text from a log file. Problem here is that awk is not processing on shell variable. But working fine on filename.
I am storing a new log entry that comes in log file in a shell variable using "new_log=tail -n5 alerts.log" in a loop whenever a new log comes, then ,
Level_no=`awk '{FS="Rule: "}{print $2}' "$new_log" | sed '/^$/d' | awk '{FS=" "}{print $3}' | sed 's/)//g'
Output:
awk: fatal: cannot open file `** Alert 1564460779.1380: mail - ossec,syscheck
* New Log starts from ** Alert 1564460779.1380: mail - ossec,syscheck *`
Above mentioned command works well when I run it in terminal using filename instead of shell variable as follows:
awk '{FS="Rule: "}{print $2}' logs_mining | sed '/^$/d' | awk '{FS=" "}{print $3}' | sed 's/)//g'
But its performance issue if I store new log entry in another file and process from there.
So I researched more and more and came to know about awk variables...here is my shell script..
Level_no=`awk -v var="$new_log" '{FS="Rule: "}{print $2}' var | sed '/^$/d' | awk '{FS=" "}{print $3}' | sed 's/)//g'
Then output says
awk: fatal: cannot open file `var' for reading (No such file or directory)
Actual Result should be Successful execution of awk script.
If new_log contains the data you want to process, not a filename, you need to pipe it to awk. You can do this with a here-string.
Level_no=`awk '{FS="Rule: "}{print $2}' <<<"$new_log" | sed '/^$/d' | awk '{FS=" "}{print $3}' | sed 's/)//g'`
It's also not necessary to pipe the output to sed and another awk, you can do it all in the first script.
Level_no=$(awk -F'Rule: ' '$2 != "" {split($2, a, " "); gsub(/)/, "", a[3]); print a[3]}' <<<"$new_log")
You probably don't need the variable, though, just pipe the output of the command to awk:
Level_no=$(tail -n5 alerts.log | awk -F'Rule: ' '$2 != "" {split($2, a, " "); gsub(/)/, "", a[3]); print a[3]}')

How to separate a field ussing awk by letter or end character

i have a 2 fields in my DB
ID25333,1429291340lNormPUC-AP_MEX_UFM-GOL_44PUC-AP_VEX_UFM-ROL_55PUCAP_MEX_UFM-DOJ_49
ID55555,1429291340lNormPUC-AP_PPP_UFM-HOL_44PUC-AF_GEX_UJM-SOL_45PUCAP_MEX_UFM-DOJ_59
and i need separate like this
ID25333,PUC-AP_MEX_UFM-GOL_44
ID25333,PUC-AP_VEX_UFM-ROL_55
ID25333,PUCAP_MEX_UFM-DOJ_49
ID55555,PUC-AP_PPP_UFM-HOL_44
ID55555,PUC-AF_GEX_UJM-SOL_45
ID55555,PUCAP_MEX_UFM-DOJ_59
with the same numbre ID
i using the AWK or grep
awk 'BEGIN{FS="PUC"}{for(i=1;i<=NF;i++)print $(i)}'
any suggestions
thanks!
With GNU awk:
$ awk -F, '{gsub(/PUC/, ","); for (i=3;i<=NF;i++)print $1",PUC"$i}' file.db
ID25333,PUC-AP_MEX_UFM-GOL_44
ID25333,PUC-AP_VEX_UFM-ROL_55
ID25333,PUCAP_MEX_UFM-DOJ_49
ID55555,PUC-AP_PPP_UFM-HOL_44
ID55555,PUC-AF_GEX_UJM-SOL_45
ID55555,PUCAP_MEX_UFM-DOJ_59
Or:
$ awk -F'(,|PUC)' '{for (i=3;i<=NF;i++)print $1",PUC"$i}' file.db
ID25333,PUC-AP_MEX_UFM-GOL_44
ID25333,PUC-AP_VEX_UFM-ROL_55
ID25333,PUCAP_MEX_UFM-DOJ_49
ID55555,PUC-AP_PPP_UFM-HOL_44
ID55555,PUC-AF_GEX_UJM-SOL_45
ID55555,PUCAP_MEX_UFM-DOJ_59
If you like awk
awk -F, ' -v OFS=','
{
id=$1
split($2,line,"PUC")
for(i=2;i<=length(line);i++)
print id,"PUC" line[i]
}'
With GNU awk for fixed width fields:
$ awk -v FIELDWIDTHS="8 15 21 21 21" '{for (i=3;i<=NF;i++) print $1 $i}' file
ID25333,PUC-AP_MEX_UFM-GOL_44
ID25333,PUC-AP_VEX_UFM-ROL_55
ID25333,PUCAP_MEX_UFM-DOJ_49
ID55555,PUC-AP_PPP_UFM-HOL_44
ID55555,PUC-AF_GEX_UJM-SOL_45
ID55555,PUCAP_MEX_UFM-DOJ_59

cut command to delimit the output and add them

I am new in bash
I wrote a bash script and it gives me an output like this:
3387 /test/file1
23688 /test/file2
5813 /test/file3
10415 /test/file4
1304 /test/file5
46 /test/file6
8 /test/file7
138 /test/file8
I can delimit them by
wc -l /path/to/$dir/test | cut -d" " -f1
how can I add numbers to eachother and caculate them?
can I do:
output=`wc -l /path/to/$dir/test | cut -d" " -f1`
Is it possible to use "while" or "for" loop and add those numbers?
how?
thank you in advance
You want awk here to avoid explicit loops. If your output was in the file data.txt you could use:
$ awk '{sum += $1} END {print sum}' data.txt
44799
In your case, pipe the output of your script to awk:
$ your_script.sh | awk '{sum += $1} END {print sum}'
Since the output you gave in your question was the output of wc -l, try:
$ wc -l /path/to/$dir/test | awk '{sum += $1} END {print sum}'
(Aside for anyone else landing on this page: wc -l, when given wildcards, will also give you a total, but it's great to use awk in this case because you can deal directly with the total line count and pipe just that to another process.)

plus sign in sed

I am trying to do the following:
echo "test++abc" | awk -v bar="test++" 'BEGIN {FS=bar} {print $2}'
I am expecting to see abc as the output, because I used bar="test++" as the separator. But I am getting ++abc instead.
Can anyone please explain this behavior and how to fix it?
Thanks a lot!
echo "test++abc" | awk -v bar='test[+][+]' 'BEGIN {FS=bar} {print $2}'
Try:
echo "test++abc" | awk -v bar="test\\+\\+" 'BEGIN {FS=bar} {print $2}'
And in sed:
echo "test++abc" | sed -e 's/test++//'

Resources