From b820acd34acb4d6037d556aff83d4be225715e4b Mon Sep 17 00:00:00 2001 From: onceupon Date: Tue, 7 Feb 2017 15:13:47 +0800 Subject: [PATCH] Update index.md --- index.md | 2946 ++++++++++++++++++++++++------------------------------ 1 file changed, 1300 insertions(+), 1646 deletions(-) diff --git a/index.md b/index.md index 91f963a..56b6130 100644 --- a/index.md +++ b/index.md @@ -1,1768 +1,1422 @@ -##Handy Bash oneliner commands for tsv file editing - -- [Grep](#grep) -- [Sed](#sed) -- [Awk](#awk) -- [Xargs](#xargs) -- [Find](#find) -- [Loops](#loops) -- [Download](#download) -- [Random](#random) -- [Others](#others) -- [System](#system) - -##Grep -#####extract text bewteen words (e.g. w1,w2) - -```bash -grep -o -P '(?<=w1).*(?=w2)' -``` - -#####grep lines without word (e.g. bbo) - -```bash -grep -v bbo filename -``` - -#####grep only one/first match (e.g. bbo) - -```bash -grep -m 1 bbo filename -``` - -#####grep and count (e.g. bbo) - -```bash -grep -c bbo filename -``` - -#####insensitive grep (e.g. bbo/BBO/Bbo) - -```bash -grep -i "bbo" filename -``` - -#####count occurrence (e.g. three times a line count three times) - -```bash -grep -o bbo filename -``` - -#####COLOR the match (e.g. bbo)! - -```bash -grep --color bbo filename -``` - -#####grep search all files in a directory(e.g. bbo) - -```bash -grep -R bbo /path/to/directory -``` - -or - -```bash -grep -r bbo /path/to/directory -``` -#####search all files in directory, only output file names with matches(e.g. bbo) - -```bash -grep -Rh bbo /path/to/directory -``` -or -```bash -grep -rh bbo /path/to/directory -``` - -#####grep OR (e.g. A or B or C or D) - -``` -grep 'A\|B\|C\|D' -``` - -#####grep AND (e.g. A and B) - -```bash -grep 'A.*B' -``` - -#####grep all content of a fileA from fileB - -```bash -grep -f fileA fileB -``` - -#####grep a tab - -```bash -grep $'\t' -``` - -#####grep variable from variable -```bash -$echo "$long_str"|grep -q "$short_str" -if [ $? -eq 0 ]; then echo 'found'; fi -``` -//grep -q will output 0 if match found -//remember to add space between []! - -#####grep strings between a bracket() -```bash -grep -oP '\(\K[^\)]+' -``` - -#####grep number of characters with known strings in between(e.g. AAEL000001-RA) -```bash -grep -o -w "\w\{10\}\-R\w\{1\}" -``` -// \w word character [0-9a-zA-Z_] \W not word character - - -#####a lot examples here -http://www.cyberciti.biz/faq/grep-regular-expressions/ - - - -##Sed -[[back to top](#handy-bash-oneliner-commands-for-tsv-file-editing)] - -#####remove lines with word (e.g. bbo) - -```bash -sed "/bbo/d" filename -``` - -#####edit infile (edit and save) - -```bash -sed -i "/bbo/d" filename -``` -#####when using variable (e.g. $i), use double quotes " " -e.g. add >$i to the first line (to make a FASTA file) - -```bash -sed "1i >$i" -``` -//notice the double quotes! in other examples, you can use a single quote, but here, no way! -//'1i' means insert to first line - - -#####delete empty lines - -```bash -sed '/^\s*$/d' -``` -or - -```bash -sed 's/^$/d' -``` -#####delete last line - -```bash -sed '$d' -``` - -#####delete last character from end of file -```bash -sed -i '$ s/.$//' filename -``` - -#####add string to end of file (e.g. "]") - -```bash -sed '$s/$/]/' filename -``` - -#####add string to beginning of every line (e.g. bbo) - -```bash -sed -e 's/^/bbo/' file -``` - -#####add string to end of each line (e.g. "}") -```bash -sed -e 's/$/\}\]/' filename -``` - -#####add \n every nth character (e.g. every 4th character) - -```bash -sed 's/.\{4\}/&\n/g' -``` - -#####concatenate/combine/join files with a seperator and next line (e.g seperate by ",") - -```bash -sed -s '$a,' *.json > all.json -``` - -#####substitution (e.g. replace A by B) - -```bash -sed 's/A/B/g' filename -``` -#####select lines start with string (e.g. bbo) - -```bash -sed -n '/^@S/p' -``` -#####delete lines with string (e.g. bbo) - -```bash -sed '/bbo/d' filename -``` -#####print every nth lines - -```bash -sed -n '0~3p' filename -``` -//catch 0: start; 3: step - - -#####print every odd # lines - -```bash -sed -n '1~2p' -``` -#####print every third line including the first line - -```bash -sed -n '1p;0~3p' -``` -#####remove leading whitespace and tabs - -```bash -sed -e 's/^[ \t]*//' -``` -//notice a whitespace before '\t'!! - - -#####remove only leading whitespace - -```bash -sed 's/ *//' -``` -//notice a whitespace before '*'!! - - -#####remove ending commas - -```bash -sed 's/,$//g' -``` -#####add a column to the end - -```bash -sed "s/$/\t$i/" -``` -//$i is the valuable you want to add -e.g. add the filename to every last column of the file - -```bash -for i in $(ls);do sed -i "s/$/\t$i/" $i;done -``` -#####add extension of filename to last column - -```bash -for i in T000086_1.02.n T000086_1.02.p;do sed "s/$/\t${i/*./}/" $i;done >T000086_1.02.np -``` - -#####remove newline\ nextline - -```bash -sed ':a;N;$!ba;s/\n//g' -``` - -#####print a number of lines (e.g. line 10th to line 33 rd) - -```bash -sed -n '10,33p'