Linux文件操作
- 统计当前文件夹下文件的个数,包括子文件夹里的:
ls -lR | grep "^-" | wc -l
- 统计当前文件夹下目录的个数:
ls -l | grep "^d" | wc -l
- 批量修改文件名:
for i in `ls`;do mv -f $i `echo $i | sed 's/\.txt//'`;done
rename _. . *png
- 给所有文件名前面加字符:
for file in *; do mv "$file" "ab$file"; done
- 删除文件中相同的行(每行有多列):
sort test.txt | awk '{if($0!=line)print; line=$0}' > test1.txt
- 按某种顺序输出指定列:
awk '{print $3"\t"$1}' test.txt > test1.txt
- 统计文件列数:
awk 'END{print NF}' test.txt
- 一行数据转为一列数据:
1
2
3
4
5sed 's/\t/\n/g' test.txt
cat test.txt | tr "\t" "\n"
cat test.txt | xargs -n 1
awk '{gsub("\t", "\n"); print $0}' test.txt
awk '{for(i = 1; i <= NF; i++) printf("%s\n", $i)}' test.txt - 删除最后n个字符:
sed -i 's/.\{n\}$//g' test.txt
- 筛选某列含有某个字符的行:
awk '{if($2 == "NA"){print $0}}' test.txt
- 获取含某个字符所在行:
sed -n -e "/chrX/=" cohort.vcf | less
- 对第一列去重:
sort input.txt | awk '!seen[$1]++'
- 表头不参与排序并输出:
head -n 1 test.txt > h1 && sed '1d' test.txt |sort |awk '!seen[$1]++' > c2 && cat h1 c2 > out3
- 对每行的缺失值补零:
awk -F '\t' '{for(i=1;i<=NF;i++) if($i=="") $i=0 ;print}' input.txt
- 对文件某列进行操作其他列不变:
awk '{$2=$2*10}1' test.txt > new.txt
- 矩阵转置:
awk -F "\t" '{for(i=1;i<=NF;i=i+1){a[NR,i]=$i}}END{for(j=1;j<=NF;j++){str=a[1,j];for(i=2;i<=NR;i++){str = str "," a[i,j]}print str}}' tpmEns.txt > tpmEnst.csv