Mostrando postagens com marcador awk. Mostrar todas as postagens
Mostrando postagens com marcador awk. Mostrar todas as postagens

Shell get lines interval from a text file

 

cat file | head -n 16482 | tail -n 258

using sed:

sed -n '16224,16482p' in.sql > out.sql

using awk:

awk 'NR>=16224&&NR<=16482' in.sql > out.sql

AWK get text between two patterns




awk '/BEGIN/,/END/' info.txt 
 
***** BEGIN *****
BASH is awesome
BASH is awesome
***** END *****
 
 

Insert character at specified position


awk '{$2="#"$2;print $0}' your_file

This will add # to the start of the second (space-separated) column regardless of which position it occurs at.



sed -r -e 's/^.{15}/&#/' file

Insert '#' in position 15

Create ISO in pendrive Linux

Get pendrive address (/dev/sdX)
lsblk -S | awk '$NF ~ /usb$/ {print $1}'

Use dd to send ISO to pendrive
dd if=/image.iso of=/dev/sdb bs=4096

Get first and last line of file


bash way
(head -1; tail -1) < /etc/passwd

sed way
sed -n '1p; $p' /etc/passwd

awk way
awk 'NR==1; END { print }' /etc/passwd

Print specific line with awk


print first line with awk
ping -c 1 8.8.8.8 | awk 'NR==1'

print last line with awk
ping -c 1 8.8.8.8 | awk 'END { print }'

Merge output lines with awk

Use paste with comma, default is tab

paste file1.txt file2.txt -d ","


merge output lines in the same line with awk

echo -e 'a\nb\nc' | awk 'ORS=" " { print }'

Delete html tag using awk

delete html tag using awk
 
echo '<font>abc 123</font>' | awk -F'.?>' '{print $2}' | awk -F'</' '{ print $(NF-1) }'

Sort on linux

sorting by first field
echo -e 'x 2\nb 1\nm 3' | sort

sorting by second field
echo -e 'x 2\nb 1\nm 3' | sort -k2

sorting by second field using comma(csv) as field separator
echo -e 'x,2\nb,1\nm,3' | sort -t, -k2

sorting by fifth field with numeric order
df -h | sort -k5n

sorting by fifth field with reverse numeric order
df -h | sort -k5nr

count a chars in file order by last column
awk -F'a' '{print $0,NF-1}' /etc/passwd | rev | sort | rev

sort in numeric order
echo 1 2 10 | tr ' ' '\n' | sort -V

Failed ssh login attempts

On Debian/Ubuntu

awk '/sshd.*Failed/ { for (f=1; f<NF; f++) if ( $f ~ "from" ) print $2, $1, $(f-1), $(f+1) }' /var/log/auth.log*

Monitoring response time with curl

   curl -s -o /dev/null -w "Conecction: %{time_connect}s | Start transfer: %{time_starttransfer}s | Total time: %{time_total}s\n" ...

Mais vistos