perplexity-cli
🧠 A simple command-line client for the Perplexity API. Ask questions and receive answers directly from the terminal! 🚀🚀🚀
A collection of handy Bash One-Liners and terminal tricks for data processing and Linux system maintenance.
git clone https://github.com/onceupon/Bash-Oneliner.gitonceupon/Bash-OnelinerI am glad that you are here! I was working on bioinformatics a few years ago and was amazed by those single-word bash commands which are much faster than my dull scripts, time saved through learning command-line shortcuts and scripting. Recent years I am working on cloud computing and I keep recording those useful commands here. Not all of them is oneliner, but i put effort on making them brief and swift. I am mainly using Ubuntu, Amazon Linux, RedHat, Linux Mint, Mac and CentOS, sorry if the commands don't work on your system.
This blog will focus on simple bash commands for parsing data and Linux system maintenance that i acquired from work and LPIC exam. I apologize that there are no detailed citation for all the commands, but they are probably from dear search engine and Stack Overflow.
English and bash are not my first language, please correct me anytime, thank you. If you know other cool commands, please teach me!
Here's a more stylish version of Bash-Oneliner~
Ctrl + a : move to the beginning of line.
Ctrl + d : if you've type something, Ctrl + d deletes the character under the cursor, else, it escapes the current shell.
Ctrl + e : move to the end of line.
Ctrl + k : delete all text from the cursor to the end of line.
Ctrl + l : equivalent to clear.
Ctrl + n : same as Down arrow.
Ctrl + p : same as Up arrow.
Ctrl + q : to resume output to terminal after Ctrl + s.
Ctrl + r : begins a backward search through command history.(keep pressing Ctrl + r to move backward)
Ctrl + s : to stop output to terminal.
Ctrl + t : transpose the character before the cursor with the one under the cursor, press Esc + t to transposes the two words before the cursor.
Ctrl + u : cut the line before the cursor; then Ctrl + y paste it
Ctrl + w : cut the word before the cursor; then Ctrl + y paste it
Ctrl + x + backspace : delete all text from the beginning of line to the cursor.
Ctrl + x + Ctrl + e : launch editor defined by $EDITOR to input your command. Useful for multi-line commands.
Ctrl + z : stop current running process and keep it in background. You can use `fg` to continue the process in the foreground, or `bg` to continue the process in the background.
Ctrl + _ : undo typing.
Esc + u # converts text from cursor to the end of the word to uppercase. Esc + l # converts text from cursor to the end of the word to lowercase. Esc + c # converts letter under the cursor to uppercase, rest of the word to lowercase.
!53
!! # run the previous command using sudo sudo !!
# last command: echo 'aaa' ^aaa^bbb #echo 'bbb' #bbb # Notice that only the first aaa will be replaced, if you want to replace all 'aaa', use ':&' to repeat it: ^aaa^bbb^:& # or !!:gs/aaa/bbb/
!cat # or !c # run cat filename again
# '*' serves as a "wild card" for filename expansion.
/etc/pa*wd #/etc/passwd
# '?' serves as a single-character "wild card" for filename expansion.
/b?n/?at #/bin/cat
# '[]' serves to match the character from a range.
ls -l [a-z]* #list all files with alphabet in its filename.
# '{}' can be used to match filenames with more than one patterns
ls *.{sh,py} #list all .sh and .py files
$0 :name of shell or shell script.
$1, $2, $3, ... :positional parameters.
$# :number of positional parameters.
$? :most recent foreground pipeline exit status.
$- :current options set for the shell.
$$ :pid of the current shell (not subshell).
$! :is the PID of the most recent background command.
$_ :last argument of the previously executed command, or the path of the bash script.
$DESKTOP_SESSION current display manager
$EDITOR preferred text editor.
$LANG current language.
$PATH list of directories to search for executable files (i.e. ready-to-run programs)
$PWD current directory
$SHELL current shell
$USER current username
$HOSTNAME current hostname
set -o vi # change bash shell to vi mode # then hit the Esc key to change to vi edit mode (when `set -o vi` is set) k # in vi edit mode - previous command j # in vi edit mode - next command 0 # in vi edit mode - beginning of the command R # in vi edit mode - replace current characters of command 2w # in vi edit mode - next to 2nd word b # in vi edit mode - previous word i # in vi edit mode - go to insert mode v # in vi edit mode - edit current command in vi man 3 readline # man page for complete readline mapping
# foo=bar echo $foo # bar echo "$foo" # bar # single quotes cause variables to not be expanded echo '$foo' # $foo # single quotes within double quotes will not cancel expansion and will be part of the output echo "'$foo'" # 'bar' # doubled single quotes act as if there are no quotes at all echo ''$foo'' # bar
var="some string"
echo ${#var}
# 11
var=string
echo "${var:0:1}"
#s
# or
echo ${var%%"${var#?}"}
var="some string"
echo ${var:2}
#me string
var="0050"
echo ${var[@]#0}
#050
{var/a/,}
{var//a/,}
export NAME="Alice"
envsubst < template.txt > output.txt
# template.txt: Hello ${NAME}, your order is shipped.
# output.txt: Hello Alice, your order is shipped.
# or
export USER="Bob" && echo 'Hi $USER, welcome!' | envsubst
# Hi Bob, welcome!
# with grep
test="stringA stringB stringC"
grep ${test// /\\\|} file.txt
# turning the space into 'or' (\|) in grep
var=HelloWorld
echo ${var,,}
helloworld
cmd="bar=foo" eval "$cmd" echo "$bar" # foo
# https://github.com/asciinema/asciinema asciinema rec demo.cast
echo $(( 10 + 5 )) #15 x=1 echo $(( x++ )) #1 , notice that it is still 1, since it's post-increment echo $(( x++ )) #2 echo $(( ++x )) #4 , notice that it is not 3 since it's pre-increment echo $(( x-- )) #4 echo $(( x-- )) #3 echo $(( --x )) #1 x=2 y=3 echo $(( x ** y )) #8
factor 50 # 50: 2 5 5
seq 10|paste -sd+|bc
awk '{s+=$1} END {print s}' filename
cat file| awk -F '\t' 'BEGIN {SUM=0}{SUM+=$3-$2}END{print SUM}'
expr 10+20 #30 expr 10\*20 #600 expr 30 \> 20 #1 (true)
# Number of decimal digit/ significant figure echo "scale=2;2/3" | bc # .66 # Exponent operator echo "10^2" | bc # 100 # Using variables echo "var=5;--var"| bc # 4
grep = grep -G # Basic Regular Expression (BRE) fgrep = grep -F # fixed text, ignoring meta-characters egrep = grep -E # Extended Regular Expression (ERE) rgrep = grep -r # recursive grep -P # Perl Compatible Regular Expressions (PCRE)
grep -c "^$"
grep -o '[0-9]*' # or grep -oP '\d*'
grep '[0-9]\{3\}'
# or
grep -E '[0-9]{3}'
# or
grep -P '\d{3}'
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
# or
grep -Po '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
grep -w 'target' # or using RE grep '\btarget\b'
# return also 3 lines after match grep -A 3 'bbo' # return also 3 lines before match grep -B 3 'bbo' # return also 3 lines before and after match grep -C 3 'bbo'
grep -o 'S.*'
grep -o -P '(?<=w1).*(?=w2)'
grep -v bbo filename
grep -v '^#' file.txt
grep "$myvar" filename # remember to quote the variable!
grep -m 1 bbo filename
grep -c bbo filename
grep -o bbo filename |wc -l
grep -i "bbo" filename
grep --color bbo filename
grep -R bbo /path/to/directory # or grep -r bbo /path/to/directory
grep -rh bbo /path/to/directory
grep -rl bbo /path/to/directory
grep 'A\|B\|C\|D'
grep 'A.*B'
grep 'A.B'
grep 'colou\?r'
grep -f fileA fileB
grep $'\t'
$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 -oP '\(\K[^\)]+'
grep -o -w "\w\{10\}\-R\w\{1\}"
# \w word character [0-9a-zA-Z_] \W not word character
grep -d skip 'bbo' /path/to/files/*
sed 1d filename
sed 1,100d filename
sed "/bbo/d" filename # case insensitive: sed "/bbo/Id" filename
sed -E '/^.{5}[^2]/d'
#aaaa2aaa (you can stay)
#aaaa1aaa (delete!)
sed -i "/bbo/d" filename
# e.g. add >$i to the first line (to make a bioinformatics FASTA file) 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
# Use backslash for end-of-line $ pattern, and double quotes for expressing the variable sed -e "\$s/\$/\n+--$3-----+/"
sed '/^\s*$/d' # or sed '/^$/d'
sed '$d'
sed -i '$ s/.$//' filename
sed -i '1s/^/[/' filename
sed -e '1isomething' -e '3isomething'
sed '$s/$/]/' filename
sed '$a\'
sed -e 's/^/bbo/' filename
sed -e 's/$/\}\]/' filename
sed 's/.\{4\}/&\n/g'
sed '/hello*/a world' filename # hello # world
sed -s '$a,' *.json > all.json
sed 's/A/B/g' filename
sed "s/aaa=.*/aaa=\/my\/new\/path/g"
sed -n '/^@S/p'
sed '/bbo/d' filename
sed -n 500,5000p filename
sed -n '0~3p' filename # catch 0: start; 3: step
sed -n '1~2p'
sed -n '1p;0~3p'
sed -e 's/^[ \t]*//' # Notice a whitespace before '\t'!!
sed 's/ *//' # notice a whitespace before '*'!!
sed 's/,$//g'
sed "s/$/\t$i/" # $i is the valuable you want to add # To add the filename to every last column of the file for i in $(ls); do sed -i "s/$/\t$i/" $i; done
for i in T000086_1.02.n T000086_1.02.p; do sed "s/$/\t${i/*./}/" $i; done >T000086_1.02.np
sed ':a;N;$!ba;s/\n//g'
sed -n -e '123p'
sed -n '10,33p' <filename
sed 's=/=\\/=g'
sed 's/A-.*-e//g' filename
sed '$ s/.$//'
sed -r -e 's/^.{3}/&#/' filename
awk -F $'\t'
awk -v OFS='\t'
a=bbo;b=obb; awk -v a="$a" -v b="$b" "$1==a && $10=b" filename
awk '{print NR,length($0);}' filename
awk '{print NF}'
awk '{print $2, $1}'
awk '$1~/,/ {print}'
awk '{split($2, a,",");for (i in a) print $1"\t"a[i]}' filename
awk -v N=7 '{print}/bbo/&& --N<=0 {exit}'
ls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' filename
awk 'BEGIN{OFS="\t"}$3="chr"$3'
awk '!/bbo/' filename
awk 'NF{NF-=1};1' filename
# For example there are two files: # fileA: # a # b # c # fileB: # d # e awk 'print FILENAME, NR,FNR,$0}' fileA fileB # fileA 1 1 a # fileA 2 2 b # fileA 3 3 c # fileB 4 1 d # fileB 5 2 e
# For example there are two files:
# fileA:
# 1 0
# 2 1
# 3 1
# 4 0
# fileB:
# 1 0
# 2 1
# 3 0
# 4 1
awk -v OFS='\t' 'NR=FNR{a[$1]=$2;next} NF {print $1,((a[$1]=$2)? $2:"0")}' fileA fileB
# 1 0
# 2 1
# 3 0
# 4 0
awk '{while (match($0, /[0-9]+\[0-9]+/)){
\printf "%s%.2f", substr($0,0,RSTART-1),substr($0,RSTART,RLENGTH)
\$0=substr($0, RSTART+RLENGTH)
\}
\print
\}'
awk '{printf("%s\t%s\n",NR,$0)}'
# For example, separate the following content:
# David cat,dog
# into
# David cat
# David dog
awk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' filename
# Detail here: http://stackoverflow.com/questions/33408762/bash-turning-single-comma-separated-column-into-multi-line-string
awk '{s+=$1}END{print s/NR}'
awk '$1 ~ /^Linux/'
awk ' {split( $0, a, "\t" ); asort( a ); for( i = 1; i <= length(a); i++ ) printf( "%s\t", a[i] ); printf( "\n" ); }'
awk '{$6 = $4 - prev5; prev5 = $5; print;}'
xargs -d\t
ls|xargs -L1 -p head
echo 1 2 3 4 5 6| xargs -n 3 # 1 2 3 # 4 5 6
echo a b c |xargs -p -n 3
xargs -t abcd # bin/echo abcd # abcd
find . -name "*.html"|xargs rm # when using a backtick rm `find . -name "*.html"`
find . -name "*.c
more like this
🧠 A simple command-line client for the Perplexity API. Ask questions and receive answers directly from the terminal! 🚀🚀🚀
search projects, people, and tags