Why is the RX and TX values ​the same when executing the network packet statistics script on centos8? - shell

##test1 on rhel8 or centos8
$for i in 1 2;do cat /proc/net/dev | grep ens192 | awk '{print "RX:"$2"\n""TX:"$10}';done | awk '{print $0}' | tee 111
##result:
RX:2541598118
TX:1829843233
RX:2541598118
TX:1829843233

Related

Listing network interfaces and list of IP's attached to each of them CentOS7

I know how to list Network interfaces:
ip ntable | grep dev | sort | uniq | sed -e 's/^.*dev //;/^lo/d'
and how to list ip's :
hostname -i
But Can't manage to list them nice way
Desired output would be:
IPv4:
Interface_1 IP_1, IP2
Interface_2 IP_4
Interface_3 IP_5
IPv6:
Interface_1 IP1
A quick approach that gives you the output in format:
Interface_1
IPv4: xxxxxxxx IPv6: xxxxxxxx
Interface_2
IPv4: xxxxxxxx IPv6: xxxxxxxx
Would be:
for i in $(ip ntable | grep dev | sort -u | awk '{print $2}'); do echo $i; ifconfig $i | grep inet | sed -e 's/\<inet\>/IPv4:/g' | sed -e 's/\<inet6\>/IPv6:/g' | awk '{print $1$2}'; done
You can parse the output to have the format as you like.

Cleaning up IP output on command line [duplicate]

This question already has answers here:
How to clean up masscan output (-oL)
(4 answers)
Closed 6 years ago.
I have a problem with the output L options ("grep-able" output); for instance, it outputs this:
| 14.138.12.21:123 | unknown | disabled |
| 14.138.184.122:123 | unknown | disabled |
| 14.138.179.27:123 | unknown | disabled |
| 14.138.20.65:123 | unknown | disabled |
| 14.138.12.235:123 | unknown | disabled |
| 14.138.178.97:123 | unknown | disabled |
| 14.138.182.153:123 | unknown | disabled |
| 14.138.178.124:123 | unknown | disabled |
| 14.138.201.191:123 | unknown | disabled |
| 14.138.180.26:123 | unknown | disabled |
| 14.138.13.129:123 | unknown | disabled |
The above is neither very readable nor easy to understand.
How can I use Linux command-line utilities, e.g. sed, awk, or grep, to output something as follows, using the file above?
output
14.138.12.21
14.138.184.122
14.138.179.27
14.138.20.65
14.138.12.235
Using awk with field separator as space, and : and getting the second field:
awk -F '[ :]' '{print $2}' file.txt
Example:
% cat file.txt
| 14.138.12.21:123 | unknown | disabled |
| 14.138.184.122:123 | unknown | disabled |
| 14.138.179.27:123 | unknown | disabled |
| 14.138.20.65:123 | unknown | disabled |
| 14.138.12.235:123 | unknown | disabled |
| 14.138.178.97:123 | unknown | disabled |
| 14.138.182.153:123 | unknown | disabled |
| 14.138.178.124:123 | unknown | disabled |
| 14.138.201.191:123 | unknown | disabled |
| 14.138.180.26:123 | unknown | disabled |
| 14.138.13.129:123 | unknown | disabled |
% awk -F '[ :]' '{print $2}' file.txt
14.138.12.21
14.138.184.122
14.138.179.27
14.138.20.65
14.138.12.235
14.138.178.97
14.138.182.153
14.138.178.124
14.138.201.191
14.138.180.26
14.138.13.129
AWK is perfect for cases when you want to split the file by "columns", and you know exactly that the order of values/columns is constant. AWK splits the lines by a field separator (which can be a regular expression like '[: ]'). The column names are accessible by their positions from the left: $1, $2, $3, etc.:
awk -F '[ :]' '{print $2}' src.log
awk -F '[ :|]' '{print $3}' src.log
awk 'BEGIN {FS="[ :|]"} {print $3}' src.log
You can also filter the lines with a regular expression:
awk -F '[ :]' '/138\.179\./ {print $2}' src.log
However, it is impossible to capture substrings with the regular expression groups.
SED is more flexible in regard to regular expressions:
sed -r 's/^[^0-9]*([0-9\.]+)\:.*/\1/' src.log
However, it lacks many useful features of the Perl-like regular expressions we used to use in every day programming. For example, even the extended syntax (-r) fails to interpret \d as a number.
Perhaps, Perl is the most flexible tool for parsing files. You can opt to simple expressions:
perl -n -e '/^\D*([^:]+):/ and print "$1\n"' src.log
or make the matching as strict as you like:
perl -n -e '/^\D*((?:\d{1,3}\.){3}\d{1,3}):/ and print "$1\n"' src.log
using sed
sed -r 's/^ *[|] *([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+):[0-9]{3}.*/\1/

Use Shell script to retrieve max number of users' activity in a file

Given a /var/log/messages file as below that recording different users sending Emails records, for example:
20140912 chris sendingemails_id00012jjdi3x
20140912 chris sendingemails_id00012jjdiji
20140912 alen sendingemails_id00012jwciscl
20140914 chris sendingemails_id00012jjdiji
20140915 linda sendingemails_id042555jwciscl
20140915 chris sendingemails_id00012jjdiji
20140916 alen sendingemails_id005hhbxxsscl
20140917 chris sendingemails_id2221d2rwaaiji
20140917 linda sendingemails_id00012baseeqcl
20140918 chris sendingemails_id45677tehhwaaiji
Now, I need to retrieve the user who sends emails the most. How can I do it?
Thanks a lot.
This should help:
cat /var/log/messages | grep "sendingemails_" | awk '{print $2}' | sort | uniq -c
OR to get exactly the username:
cat /var/log/messaes | grep "sendingemails_" | awk '{print $2}' | sort | uniq -c | sort -r | head -1 | awk '{print $2}'

replace string in comma delimiter file using nawk

I need to implement the if condition in the below nawk command to process input file if the third column has more that three digit.Pls help with the command what i am doing wrong as it is not working.
inputfile.txt
123 | abc | 321456 | tre
213 | fbc | 342 | poi
outputfile.txt
123 | abc | 321### | tre
213 | fbc | 342 | poi
cat inputfile.txt | nawk 'BEGIN {FS="|"; OFS="|"} {if($3 > 3) $3=substr($3, 1, 3)"###" print}'
Try:
awk 'length($3) > 3 { $3=substr($3, 1, 3)"###" } 1 ' FS=\| OFS=\| test1.txt
This works with gawk:
awk -F '[[:blank:]]*\\\|[[:blank:]]*' -v OFS=' | ' '
$3 ~ /^[[:digit:]]{4,}/ {$3 = substr($3,1,3) "###"}
1
' inputfile.txt
It won't preserve the whitespace so you might want to pipe through column -t

Ploblem of This code(show pr0cess) [ps Linux]

This code show user's process load (%cpu)
ps aux | awk 'NR!=1{print $1}' | sort | uniq | awk '{print "ps aux | grep "$1}' | awk '{printf $0; printf " | awk"; printf "{sum +="; print "$3}" }' | sed "s/{/ '{/g" | sed "s/3}/3} END {print \$1,sum}'/g" > 0.out
chmod 755 0.out
bash 0.out
This Code show same user in some OS(UBUNTU) example:
root 11.5
root 0
root 0
root 1.8
root 1.3
root 0
root 1.1
but show different user(uniq) on some OS example2:
root 11.5
daemon 0
syslog 0 ....
How can i write for example2 only.i want diff3rent user's %cpu.
You can replace all that with:
ps ahx -o "%U %C" | awk '
{cpu[$1] += $2}
END {for (user in cpu) {print user, cpu[user]}}
'

Resources