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

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

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

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

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学习-环境变量和可执行属性)。

#!/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