mirror of
https://github.com/onceupon/Bash-Oneliner.git
synced 2024-11-26 06:17:01 +00:00
Update README.md
This commit is contained in:
parent
17c9f93e06
commit
911bd9bc43
43
README.md
43
README.md
@ -93,6 +93,7 @@ e.g. add >$i to the first line (to make a FASTA file)
|
|||||||
//notice the double quotes! in other examples, you can use a single quote, but here, no way!
|
//notice the double quotes! in other examples, you can use a single quote, but here, no way!
|
||||||
//'1i' means insert to first line
|
//'1i' means insert to first line
|
||||||
|
|
||||||
|
|
||||||
delete empty lines
|
delete empty lines
|
||||||
|
|
||||||
sed '/^\s*$/d'
|
sed '/^\s*$/d'
|
||||||
@ -126,6 +127,7 @@ print every nth lines
|
|||||||
sed -n '0~3p' filename
|
sed -n '0~3p' filename
|
||||||
//catch 0: start; 3: step
|
//catch 0: start; 3: step
|
||||||
|
|
||||||
|
|
||||||
print every odd # lines
|
print every odd # lines
|
||||||
|
|
||||||
sed -n '1~2p'
|
sed -n '1~2p'
|
||||||
@ -139,11 +141,13 @@ remove leading whitespace and tabs
|
|||||||
sed -e 's/^[ \t]*//'
|
sed -e 's/^[ \t]*//'
|
||||||
//notice a whitespace before '\t'!!
|
//notice a whitespace before '\t'!!
|
||||||
|
|
||||||
|
|
||||||
remove only leading whitespace
|
remove only leading whitespace
|
||||||
|
|
||||||
sed 's/ *//'
|
sed 's/ *//'
|
||||||
//notice a whitespace before '*'!!
|
//notice a whitespace before '*'!!
|
||||||
|
|
||||||
|
|
||||||
remove ending commas
|
remove ending commas
|
||||||
|
|
||||||
sed 's/,$//g'
|
sed 's/,$//g'
|
||||||
@ -231,22 +235,35 @@ fileB 4 1 d
|
|||||||
fileB 5 2 e
|
fileB 5 2 e
|
||||||
|
|
||||||
and gate
|
and gate
|
||||||
|
|
||||||
e.g.
|
e.g.
|
||||||
fileA:
|
fileA:
|
||||||
1 0
|
1 0
|
||||||
|
|
||||||
2 1
|
2 1
|
||||||
|
|
||||||
3 1
|
3 1
|
||||||
|
|
||||||
4 0
|
4 0
|
||||||
|
|
||||||
fileB:
|
fileB:
|
||||||
|
|
||||||
1 0
|
1 0
|
||||||
|
|
||||||
2 1
|
2 1
|
||||||
|
|
||||||
3 0
|
3 0
|
||||||
|
|
||||||
4 1
|
4 1
|
||||||
|
|
||||||
awk -v OFS='\t' 'NR=FNR{a[$1]=$2;next} NF {print $1,((a[$1]=$2)? $2:"0")}' fileA fileB
|
awk -v OFS='\t' 'NR=FNR{a[$1]=$2;next} NF {print $1,((a[$1]=$2)? $2:"0")}' fileA fileB
|
||||||
|
|
||||||
1 0
|
1 0
|
||||||
|
|
||||||
2 1
|
2 1
|
||||||
|
|
||||||
3 0
|
3 0
|
||||||
|
|
||||||
4 0
|
4 0
|
||||||
|
|
||||||
round all numbers of file (e.g. 2 significant figure)
|
round all numbers of file (e.g. 2 significant figure)
|
||||||
@ -274,6 +291,7 @@ display 3 items per line
|
|||||||
//1 2 3
|
//1 2 3
|
||||||
4 5 6
|
4 5 6
|
||||||
|
|
||||||
|
|
||||||
prompt before execution
|
prompt before execution
|
||||||
|
|
||||||
echo a b c |xargs -p -n 3
|
echo a b c |xargs -p -n 3
|
||||||
@ -284,6 +302,7 @@ print command along with output
|
|||||||
///bin/echo abcd
|
///bin/echo abcd
|
||||||
//abcd
|
//abcd
|
||||||
|
|
||||||
|
|
||||||
with find and rm
|
with find and rm
|
||||||
|
|
||||||
find . -name "*.html"|xargs rm -rf
|
find . -name "*.html"|xargs rm -rf
|
||||||
@ -320,6 +339,7 @@ copy all files from A to B
|
|||||||
//v: verbose|
|
//v: verbose|
|
||||||
//p: keep detail (e.g. owner)
|
//p: keep detail (e.g. owner)
|
||||||
|
|
||||||
|
|
||||||
with sed
|
with sed
|
||||||
|
|
||||||
ls |xargs -n1 -I file sed -i '/^Pos/d' filename
|
ls |xargs -n1 -I file sed -i '/^Pos/d' filename
|
||||||
@ -341,6 +361,7 @@ count files within directories
|
|||||||
echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' --
|
echo mso{1..8}|xargs -n1 bash -c 'echo -n "$1:"; ls -la "$1"| grep -w 74 |wc -l' --
|
||||||
// "--" signals the end of options and display further option processing
|
// "--" signals the end of options and display further option processing
|
||||||
|
|
||||||
|
|
||||||
download dependencies files and install (e.g. requirements.txt)
|
download dependencies files and install (e.g. requirements.txt)
|
||||||
|
|
||||||
cat requirements.txt| xargs -n1 sudo pip install
|
cat requirements.txt| xargs -n1 sudo pip install
|
||||||
@ -373,6 +394,7 @@ if no subdirectory
|
|||||||
replace "www" "w" -- *
|
replace "www" "w" -- *
|
||||||
//a space before *
|
//a space before *
|
||||||
|
|
||||||
|
|
||||||
find and output only filename (e.g. "mso")
|
find and output only filename (e.g. "mso")
|
||||||
|
|
||||||
find mso*/ -name M* -printf "%f\n"
|
find mso*/ -name M* -printf "%f\n"
|
||||||
@ -403,17 +425,24 @@ or
|
|||||||
sdiff fileA fileB
|
sdiff fileA fileB
|
||||||
//side-to-side merge of file differences
|
//side-to-side merge of file differences
|
||||||
|
|
||||||
>number a file (e.g. fileA)
|
|
||||||
|
number a file (e.g. fileA)
|
||||||
|
|
||||||
nl fileA
|
nl fileA
|
||||||
|
|
||||||
or
|
or
|
||||||
|
|
||||||
nl -nrz fileA
|
nl -nrz fileA
|
||||||
|
|
||||||
//add leading zeros
|
//add leading zeros
|
||||||
|
|
||||||
|
|
||||||
combine/ paste two files (e.g. fileA, fileB)
|
combine/ paste two files (e.g. fileA, fileB)
|
||||||
|
|
||||||
paste fileA fileB
|
paste fileA fileB
|
||||||
//default tab seperated
|
//default tab seperated
|
||||||
|
|
||||||
|
|
||||||
reverse string
|
reverse string
|
||||||
|
|
||||||
echo 12345| rev
|
echo 12345| rev
|
||||||
@ -439,11 +468,13 @@ or
|
|||||||
(command here) 2>&1 >>outfile
|
(command here) 2>&1 >>outfile
|
||||||
//0: standard input; 1: standard output; 2: standard error
|
//0: standard input; 1: standard output; 2: standard error
|
||||||
|
|
||||||
|
|
||||||
send mail
|
send mail
|
||||||
|
|
||||||
echo 'heres the content'| mail -A 'file.txt' -s 'mail.subject' me@gmail.com
|
echo 'heres the content'| mail -A 'file.txt' -s 'mail.subject' me@gmail.com
|
||||||
//use -a flag to set send from (-a "From: some@mail.tld")
|
//use -a flag to set send from (-a "From: some@mail.tld")
|
||||||
|
|
||||||
|
|
||||||
.xls to csv
|
.xls to csv
|
||||||
|
|
||||||
xls2csv filename
|
xls2csv filename
|
||||||
@ -501,6 +532,7 @@ or
|
|||||||
!c
|
!c
|
||||||
//run cat filename again
|
//run cat filename again
|
||||||
|
|
||||||
|
|
||||||
extract .xf
|
extract .xf
|
||||||
|
|
||||||
1.unxz filename.tar.xz
|
1.unxz filename.tar.xz
|
||||||
@ -548,11 +580,13 @@ or
|
|||||||
Alt+Shift+#
|
Alt+Shift+#
|
||||||
//to make it to history
|
//to make it to history
|
||||||
|
|
||||||
|
|
||||||
add things to history (e.g. "addmetohistory")
|
add things to history (e.g. "addmetohistory")
|
||||||
|
|
||||||
#addmetodistory
|
#addmetodistory
|
||||||
//just add a "#" before~~
|
//just add a "#" before~~
|
||||||
|
|
||||||
|
|
||||||
sleep awhile or wait for a moment or schedule a job
|
sleep awhile or wait for a moment or schedule a job
|
||||||
|
|
||||||
sleep 5;echo hi
|
sleep 5;echo hi
|
||||||
@ -569,12 +603,14 @@ backup with rsync
|
|||||||
rsync -av --update directory directory.bak
|
rsync -av --update directory directory.bak
|
||||||
//skip files that are newer on receiver (i prefer this one!)
|
//skip files that are newer on receiver (i prefer this one!)
|
||||||
|
|
||||||
|
|
||||||
make all directories at one time!
|
make all directories at one time!
|
||||||
|
|
||||||
mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat}
|
mkdir -p project/{lib/ext,bin,src,doc/{html,info,pdf},demo/stat}
|
||||||
//-p: make parent directory
|
//-p: make parent directory
|
||||||
//this will create project/doc/html/; project/doc/info; project/lib/ext ,etc
|
//this will create project/doc/html/; project/doc/info; project/lib/ext ,etc
|
||||||
|
|
||||||
|
|
||||||
run command only if another command returns zero exit status (well done)
|
run command only if another command returns zero exit status (well done)
|
||||||
|
|
||||||
cd tmp/ && tar xvf ~/a.tar
|
cd tmp/ && tar xvf ~/a.tar
|
||||||
@ -594,17 +630,22 @@ use backslash "\" to break long command
|
|||||||
>mkdir -p tmp/a/b/c
|
>mkdir -p tmp/a/b/c
|
||||||
|
|
||||||
get pwd
|
get pwd
|
||||||
|
|
||||||
VAR=$PWD; cd ~; tar xvf -C $VAR file.tar
|
VAR=$PWD; cd ~; tar xvf -C $VAR file.tar
|
||||||
//PWD need to be capital letter
|
//PWD need to be capital letter
|
||||||
|
|
||||||
list file type of file (e.g. /tmp/)
|
list file type of file (e.g. /tmp/)
|
||||||
|
|
||||||
file /tmp/
|
file /tmp/
|
||||||
//tmp/: directory
|
//tmp/: directory
|
||||||
|
|
||||||
|
|
||||||
bash script
|
bash script
|
||||||
|
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
file=${1#*.}
|
file=${1#*.}
|
||||||
//remove string before a "."
|
//remove string before a "."
|
||||||
|
|
||||||
file=${1%.*}
|
file=${1%.*}
|
||||||
//remove string after a "."
|
//remove string after a "."
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user