Skip to content

Instantly share code, notes, and snippets.

@wisicn
Last active May 21, 2022 12:20
Show Gist options
  • Save wisicn/5006617 to your computer and use it in GitHub Desktop.
Save wisicn/5006617 to your computer and use it in GitHub Desktop.
my linux swiss army knife tools sed/grep/awk collection
#Deleting comment lines starting with a special symbol
sed '/^\#/d' myFile > output.txt
#delete empty lines with SED
sed '/^$/d' myFile > output.txt
#mv empty lines and comments lines together
sed '/^\#/d' myFile | sed '/^$/d' > output.txt
#To remove all leading whitespace (including tabs) from left to first word
sed -e 's/^[ \t]*//' myFile > output.txt
sed -e 's/^\s*//' myFile > output.txt
#To delete trailing whitespace from end of each line
sed 's/[ \t]*$//' myFile > output.txt
#Better remove all leading and trailing whitespace from end of each line:
sed 's/^[ \t]*//;s/[ \t]*$//' myFile > output.txt
#replace ctrl-M with \n, to get ^M type CTRL+V followed by CTRL+M
sed -e 's/^M/\n/g' myFile > output.txt
#in Mac OS X, the sed did not reconginze \t\n\s\w
cat myFile | tr '\r' '\n' > output.txt
#print previous lines when match some patten, in this case it is the previous 4 lines(-B4)
grep -B4 "RTSP/1.0 404 Not Found" /var/log/rtsp.log
#remove all # leading lines and all blank lines
sed -e '/^$/d;/^#/d' myile > output.txt
#remove all ending ^M, to get ^M type CTRL+V followed by CTRL+M
sed -e 's/^M//g' input > output
#replace commoa with newline
echo "a,b" | sed -e $'s/,/\\\n/g'
#read line from a file and execute command
while read line; do echo $line; done < yourname.txt
#read line from a file and execute two command
while read line; do echo $line && sleep 5; done < yourname.txt
#find the old file more than 30 days and list or delete them
find /path/to/my/directory -type f -mtime +180 | xargs ls -la
find /path/to/my/directory -type f -mtime +180 | xargs rm -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment