Merge pull request #29 from siansiansu/formatExampleCode

format bash commands
This commit is contained in:
Bonnie I-Man Ng 2023-03-11 16:40:42 +08:00 committed by GitHub
commit 16197f576e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -617,12 +617,12 @@ 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 $(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
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
@ -1055,7 +1055,7 @@ if [[ $age -gt 21 ]]; then echo -e "forever 21!!" ; fi
##### For loop
```bash
# Echo the file name under the current directory
for i in $(ls); do echo file $i;done
for i in $(ls); do echo file $i; done
#or
for i in *; do echo file $i; done
@ -1063,7 +1063,7 @@ for i in *; do echo file $i; done
for dir in $(<myfile); do mkdir $dir; done
# Press any key to continue each loop
for i in $(cat tpc_stats_0925.log |grep failed|grep -o '\query\w\{1,2\}');do cat ${i}.log; read -rsp $'Press any key to continue...\n' -n1 key;done
for i in $(cat tpc_stats_0925.log |grep failed|grep -o '\query\w\{1,2\}'); do cat ${i}.log; read -rsp $'Press any key to continue...\n' -n1 key; done
# Print a file line by line when a key is pressed,
oifs="$IFS"; IFS=$'\n'; for line in $(cat myfile); do ...; done
@ -1073,21 +1073,21 @@ while read -r line; do ...; done <myfile
for line in $(cat myfile); do echo $line; read -n1; done
#Loop through an array
for i in "${arrayName[@]}"; do echo $i;done
for i in "${arrayName[@]}"; do echo $i; done
```
##### While loop,
```bash
# Column subtraction of a file (e.g. a 3 columns file)
while read a b c; do echo $(($c-$b));done < <(head filename)
while read a b c; do echo $(($c-$b)); done < <(head filename)
#there is a space between the two '<'s
# Sum up column subtraction
i=0; while read a b c; do ((i+=$c-$b)); echo $i; done < <(head filename)
# Keep checking a running process (e.g. perl) and start another new process (e.g. python) immediately after it. (BETTER use the wait command! Ctrl+F 'wait')
while [[ $(pidof perl) ]];do echo f;sleep 10;done && python timetorunpython.py
while [[ $(pidof perl) ]]; do echo f; sleep 10; done && python timetorunpython.py
```
##### switch (case in bash)
@ -2746,8 +2746,8 @@ echo {1,2}{1,2}
```bash
set = {A,T,C,G}
group= 5
for ((i=0; i<$group; i++));do
repetition=$set$repetition;done
for ((i=0; i<$group; i++)); do
repetition=$set$repetition; done
bash -c "echo "$repetition""
```
@ -2925,7 +2925,7 @@ tac filename
##### Reverse the result from `uniq -c`
```bash
while read a b; do yes $b |head -n $a ;done <test.txt
while read a b; do yes $b |head -n $a ; done <test.txt
```