结合变量分为庆典code线 [英] Incorporate variables into bash code line
问题描述
好吧,也许这是不是最好的称号;但很难传达我的本意只是在简短的标题。
Well maybe this is not the best title; but it's hard to convey my intention only in short title.
我在这里已经一行:
2 118610455 P2_PM_2_5034 T <DUP:TANDEM> 40 . END=118610566;SVLEN=110;SVTYPE=TDUP;CIPOS=-100,55;CIEND=-56,100;IMPRECISE;DBVARID=esv7540;VALIDATED;VALMETHOD=CGH;SVMETHOD=RP
基本上我想将其转换为:
Basically I would like to convert it into:
2 118610455 118610566
所以,主要的问题是到grep这个 118610566
从第8列。
我知道如何使用grep这个数字:
I know how to grep this number:
$c=`cat line|awk '{print $8}'|sed 's/;/\t/g'|awk '{print $1}'|sed 's/\END=//g'`
但我的问题是,那么我怎么可以将这个变量到另一个bash的行:
but my question is then how I can incorporate this variable into another bash line:
what_i_want=`cat line|awk '{print $1"\t"$2"\t"$c}'`
THX
推荐答案
随着一点点的字符串操作可以在一个去得到它。
With a little string manipulation you can get it in one go.
what_i_want=$(awk '{sub(/^END=/,"",$8); sub(/;.*$/,"",$8); print $1,$2,$8}' line)
一些解释:
分(A,B,C)
的模式搜索 A
变量Ç
和替换它b
,保存修改后的字符串返回到 C
。模式是在 //
写的。
sub(a,b,c)
searches for pattern a
in variable c
and replaces it with b
, storing the modified string back into c
. Patterns are written within //
.
^
是字符串的开头, $
是结束,。
是什么, *
表示零个或多个preceding格局。因此,在我们的例子:
^
is the beginning of the string, $
is the end, .
is anything, and *
means zero or more of the preceding pattern. So in our case:
子(/ ^ END = /,,$ 8);
匹配 END =
开头( ^
)的字符串和,
,什么都没有,基本上是删除它替换它。
sub(/^END=/,"",$8);
matches END=
at the beginning (^
) of the string and replaces it with ""
, nothing, essentially deleting it.
子(/;.*$/,\"\",$ 8);
需要的一切( *
)。从;
来结束( $
),并删除它。请注意,在awk中,与大多数正则表达式引擎, *
是贪婪的,这意味着它需要它可以得到最长的匹配,所以我们知道这将获得的第一个;
。
sub(/;.*$/,"",$8);
takes everything (.*
) from ;
to the end ($
) and deletes it. Note that in awk, as with most regex engines, *
is greedy, which means it takes the longest match it can get, so we know this will get the first ;
.
和所有我们留下的是你想要的号码。
And all we are left with is the number you want.
这篇关于结合变量分为庆典code线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!