前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux学习-文件列太多,很难识别想要的信息在哪列;别焦急,看这里。

Linux学习-文件列太多,很难识别想要的信息在哪列;别焦急,看这里。

作者头像
生信宝典
发布2018-02-05 11:26:40
1.4K0
发布2018-02-05 11:26:40
举报
文章被收录于专栏:生信宝典生信宝典

经常会碰到列数特别多的文件,而屏幕又不足以放下这么多列;即便能放下,也不容易清晰的辨别出想提取的信息在第几列。

根据我们前面的学习,可以用一行命令或简单的写一个bash脚本来处理这个问题。

命令如下,命令的解释见 Linux学习-文件排序和FASTA文件操作

```bash

ct@ehbio:~$ vim test

ct@ehbio:~$ cat test

sample A B C D E F G H

ID1 1 2 3 4 5 6 7 8

ID2 1 2 3 4 5 6 7 8

ID3 1 2 3 4 5 6 7 8

ct@ehbio:~$ sed -n '1 s/\t/\n/g; 1p' test

sample

A

B

C

D

E

F

G

H

ct@ehbio:~$ sed -n '1 s/\t/\n/g; 1p' test | sed '=' | sed 'N;s/\n/\t/'

1 sample

2 A

3 B

4 C

5 D

6 E

7 F

8 G

9 H

```

完整脚本 `checkCol.sh` (查看如何像运行一个系统命令一样运行脚本: Linux学习-环境变量和可执行属性)。

```bash

#!/bin/bash

#set -x

set -e

#set -u

usage()

{

cat <<EOF

${txtcyn}

Usage:

$0 options${txtrst}

${bldblu}Function${txtrst}:

This script is used to check the number of columns in given line and

will output the column number before each column item.

${txtbld}OPTIONS${txtrst}:

-f Data file (- represents STDIN)${bldred}[NECESSARY]${txtrst}

-l lineNumber[${txtred}Default 1 meaning the first line${txtrst}]

EOF

}

file=

lineno=1

while getopts "hf:l:" OPTION

do

case $OPTION in

h)

usage

exit 1

;;

f)

file=$OPTARG

;;

l)

lineno=$OPTARG

;;

?)

usage

exit 1

;;

esac

done

if [ -z $file ]; then

usage

exit 1

fi

if read -t 0; then

sed -n "${lineno} s/\t/\n/g; ${lineno}p" | \

sed '=' | sed 'N;s/\n/\t/'

else

sed -n "${lineno} s/\t/\n/g; ${lineno}p" ${file} | \

sed '=' | sed 'N;s/\n/\t/'

fi

```

脚本使用

```

ct@ehbio:~$ checkCol.sh -f test

1 sample

2 A

3 B

4 C

5 D

6 E

7 F

8 G

9 H

ct@ehbio:~$ checkCol.sh -f test -l 2

1 ID1

2 1

3 2

4 3

5 4

6 5

7 6

8 7

9 8

```

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-07-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信宝典 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档