Biostar 시작하기 학습 노트 (1): Some basic but useful code
http://swcarpentry.github.io/shell-novice/
cd ../..
cd ~ #user home "/home/user_name/""
cd / #to root home "/""
">"redirect the command's output to a file Be careful when using ">", it will override with new contents and the previous contents will be lost.
touch length.txt
wc -lwc *.pdb > length.txt
sort -n length.txt > sorted.length.txt
head -n 6 sorted.length.txt
Difference between ">"& ">>"
">"redirect the command's output to a file ">>"append the command's output to a file
echo hello > testfile01.txt
echo hello > testfile01.txt
echo hello >> testfile02.txt
echo hello >> testfile02.txt
cat testfile01.txt
cat testfile02.txt
"|"between two commands is called a pipe. Use the output of the command on the left as the input to the command on the right.
wc -l *.pdb | sort -n
wc -l *.pdb | sort # alphabetical order
wc -l *.pdb | sort -n | head -n 5
wc -l *.pdb | sort -n | tail -n 5
*[AB].txt
*matches any number of characters; the expression
*[AB] matches either an ‘A’ or a ‘B’,
"uniq"Only Remove Adjacent Duplicates
the file data-shell/data/salmon.txt contains:
cat samon.txt
# output:
coho
coho
steelhead
coho
steelhead
steelhead
uniq salmon.txt
# output:
coho
steelhead
coho
steelhead
sort salmon.txt | uniq
# output:
coho
steelhead
# show the total cout of each type of animals in the file
cut -d, -f 2 animals.txt | sort | uniq -c
# output:
1 bear
2 deer
1 fox
3 rabbit
1 raccoon
Appending Data
touch animalsUpd.txt
head -3 animals.txt > animalsUpd.txt
tail -2 animals.txt >> animalsUpd.txt
Loops
for filename in *.dat
do
echo $filename
head -n 100 $filename | tail -n 20
done
rename multiple files
$ for filename in *.dat
> do
> cp $filename original-$filename
> done
# use "echo" for debugging
$ for filename in *.dat
> do
> echo cp $filename original-$filename
> done
# output:
cp basilisk.dat original-basilisk.dat
cp unicorn.dat original-unicorn.dat
$ cd north-pacific-gyre/2012-07-03
$ for datafile in NENE*[AB].txt
> do
> echo $datafile
> done
History Commands
$ history
Ctrl-R
!$
less !$
Nested loops
for species in cubane ethane methane
do
for temperature in 25 30 37 40
do
mkdir $species-$temperature
done
done
Inside a shell script, $1 means “the first filename (or other argument) on the command line”, $2 the filename or argument. ("$@"is equivalent to "$1""$2"…) Create middle.sh and enter the following code:
# Select lines from middle of a file (line 11 to line 20)
# Useage: bash middle.sh filename end_line num_lines
head -n 15 "$1" | tail -n 5
head -n "$2" "$1" | tail -n "$3"
$ bash middle.sh octane.pdb
$ bash middle.sh octane.pdb 10 5
Write a shell script called species.sh that takes any number of filenames as command-line arguments, and uses cut, sort, and uniq to print a list of the unique species appearing in each of those files separately.
# Script to find unique species in csv files where species is the second data field
# This script accepts any number of file names as command line arguments
# Loop over all files
for file in $@
do
echo "Unique species in $file:"
# Extract species names
cut -d , -f 2 $file | sort | uniq
done
find
find .
# find all directories
find . -type -d
# find all files
find . -type -f
# find all
find . -name filename.extension
find . -name 'filenane.extension'
command1 $(command2) When the shell executes this command, the first thing it does is run whatever is inside the $(). It then replaces the $() expression with that command’s output.
$ wc -l $(find . -name '*.txt')
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.