Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

Well, hard to think of a title, this will probably only ever get read by me, but its super random search patters with grep/awk and who knows what else

grep -Eo '[0-9]+' file | sort -rn | head -n 1 = highest number
grep -o '^[^ ]*' = Grep up till first space
grep -o '[0-9]*' = grep #'s one per line
grep -o '"[^"]\+"' >> text.file = Output Grep in double Quotes "RESULTS"
cut -d' ' -f17,20 = Cut out everything except #'s in one line
percent=$(awk "BEGIN { pc=100*${var1}/${var2}; i=int(pc); print (pc-i<0.5)?i:i+1 }") # Divide 2 Numbers and set varible to percent

Rename All Files with UPPERCASE letters to lowercase - linux

for i in $( ls | grep [A-Z] ); do mv -i $i `echo $i | tr 'A-Z' 'a-z'`; done

Move all Files of one type recursively into one directory

find /var/www/html/wp-content/uploads/ -name '*.jpg' | xargs -I files mv files .

These are from installing Asterisk + FOP2 but I find them useful examples:

sed -i 's/\(^SELINUX=\).*/\SELINUX=disabled/' /etc/selinux/config
sed -i 's/\(^upload_max_filesize = \).*/\120M/' /etc/php.ini
sed -i 's/^\(User\|Group\).*/\1 asterisk/' /etc/httpd/conf/httpd.conf
sed -i 's/AllowOverride None/AllowOverride All/' /etc/httpd/conf/httpd.conf

Grep for 2 things in same line

grep 'word1.*word2' logs

Sed Find and Replace Global

sed -i -e 's/foo/bar/g' filename

Replace Comment Symbol

sed -i '/2001/s/^/#/g' file    (to ADD Comment #)
sed -i '/2001/s/^#//g' file    (to uncomment)

Expand Variable (Has to have Double Quotes outside entire string!)

sed -i -e "s/MASTER/$MASTER/g" /etc/mysql/my.cnf 

Insert into new line after greping for X

sed -i '/X/a TEST' file

Insert Multiple New Lines after Greping for X

sed -i '/X/r input.txt' file

Append to end of Line where X is first word in line

sed -i '/^ s/$/ ve_vminfo delaypurple=vm_info:75/' /etc/xymon/hosts.cfg

Echo Quotes

echo "\"$1\",\"$2\",\"$3\",\"$4\""


Grep for IP Address

grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b"
ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep -v | grep -v 255
ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

Grep only first result

cat foo | grep bar | head -1

Grep into Array and parse Array - Double (( )) vs "()"

IP=($(echo hello))
for i in "${IP[@]}"
echo $1

Exclude Grep 

cat foo | grep bar | grep -v excludedpattern

Grep for IP and DO when IP Found


while [ -z "$IP" ];do

IP="$(ifconfig | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" | grep -v | grep -v 255)"


if [ -n "$IP" ]; then

echo "$IP"


Extract Value Between Double Quotes:

awk -F'"' '$0=$2' 

Grep for 2 Strings:

netstat -tulpn | grep 'ntopng\|nprobe'

Sed - Change Variable after = Sign:

sed -i 's,^\(THISISMYVARIABLE[ ]*=\).*,\1'THISISMYVALUE',g' config.cfg

Rename a bunch of files removing part of the name:

rename 's/PART2REMOTE//g' *

Remove all () Brackets from filenames:

for i in *\(*\)*; do j=`echo $i |sed -e 's/[()]//g'`; mv "$i" "$j"; done

Remove all []Brackets from filenames:

for x in *[*; do mv -- "$x" "${x//[][]}"; done

Change Spaces to underscores (Directory or Files):

find -name "* *" -type d | rename 's/ /_/g'    # do the directories first
find -name "* *" -type f | rename 's/ /_/g'

Move all Files from Subdirectories to Current Directory

 find . -type f -print0 |  xargs -0 mv -t .

Echo text into file:

sudo sh -c 'echo "foo" >> /path/bar'

Remove Entire Line matching Variable with SED:

sed -i '/foo/d' /path/bar

Echo Timestamp to log:

echo  $(date -u) >> log

Grep for Version Number:

egrep -o "([0-9]{1,}\.)+[0-9]{1,}" file

Grep for PID from PS AUX:

ps aux | grep $proc| awk '{print $2}' | head -1

Pass Output As Argument via Pipe & XArgs:

command1 | xargs -I{} command2 {}
  • No labels