sh/bash/dash/ksh/zsh等Shell脚本
-
蒙毅酋长
- 帖子: 136
- 注册时间: 2009-08-29 17:55
#1
帖子
由 蒙毅酋长 » 2013-11-15 20:33
这是awk的命令:
代码: 全选
awk '{if (index($8,"1")==4) {print $0 > "$prifx_chr1_tmp"}\
else if (index($8,"2")==4) {print $0 > "$prifx_chr2_tmp"}\
else if (index($8,"3")==4) {print $0 > "$prifx_chr3_tmp"}\
else if (index($8,"4")==4) {print $0 > "$prifx_chr4_tmp"}\
else if (index($8,"5")==4) {print $0 > "$prifx_chr5_tmp"}\
else if (index($8,"6")==4) {print $0 > "$prifx_chr6_tmp"}\
else if (index($8,"7")==4) {print $0 > "$prifx_chr7_tmp"}}' $1
其中
这个,生成的文件都是以$prifx开头的,原来是想按
这样来的。想不成结果却是另一个样子,不知道这其中的问题是在哪呢?
是指一个文本文件。
谢谢大家
-
lilydjwg
- 论坛版主
- 帖子: 4258
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
-
联系:
#2
帖子
由 lilydjwg » 2013-11-16 12:05
没太明白问题。不过,awk 里是不像 shell 那样扩展 $var 这种变量的。如果你的 shell 里设置了 prefix=09110 的话,在 awk 可以这么用:
[bash]
awk -vprefix=$prefix '{if (index($8,"1")==4) {print $0 > prefix"_chr1_tmp"}\
else if (index($8,"2")==4) {print $0 > prefix"_chr2_tmp"}\
else if (index($8,"3")==4) {print $0 > prefix"_chr3_tmp"}\
else if (index($8,"4")==4) {print $0 > prefix"_chr4_tmp"}\
else if (index($8,"5")==4) {print $0 > prefix"_chr5_tmp"}\
else if (index($8,"6")==4) {print $0 > prefix"_chr6_tmp"}\
else if (index($8,"7")==4) {print $0 > prefix"_chr7_tmp"}}' $1
[/bash]
不过感觉这个脚本写得好麻烦啊。
-
sk1418
- 帖子: 229
- 注册时间: 2007-07-01 17:36
- 系统: (En):System
- 来自: (En):address
-
联系:
#3
帖子
由 sk1418 » 2013-12-22 22:49
1, 看不到你的输入,不好多说。但是你这个脚本一定会有更简化的版本。
2. 如果要用shell 变量,要用-v 比如
代码: 全选
awk -v foo="$HOME" '{...code... ; print "my Home dir is:" foo}' input
---
regards,
Kent