- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想找到这样的模式:当前行中的第 2 列是“C”,下一行中的第 2 列是“G”。文件的第 4 列是“CG”。我想比较第 1 与第 2、第 3 与第 4、第 5 与第 6,依此类推。然后打印几行当前行和下一行。“C”可以出现在偶数行和奇数行中。
输入如下:
chr1 C 10467 CHH CT 0.0 0 1
chr1 C 10469 CG CG 0.0 0 1
chr1 G 10470 CG CG 0.0 0 8
chr1 C 10471 CG CG 0.0 0 1
chr1 G 10472 CG CG 1.0 8 8
预期输出是,用制表符分隔符分隔:
chr1 C 10469 CG CG 0.0 0 1
chr1 G 10470 CG CG 0.0 0 8
chr1 C 10471 CG CG 0.0 0 1
chr1 G 10472 CG CG 1.0 8 8
我的代码是:
awk '{a=$2; c=$4; d=$0; e=NR; getline; f=$2; g=$4} {if (a == "C" && f == "G" && c == "CG" && g == "CG") {print d,e,"\n",$0,NR}}' input_file
我使用 getline 并检查下一行是否有“G”。问题是,如果我这样做,awk 将直接转到第三行,并且会错过一些行。例如,输入的第 2 列是:
Line 1: G
Line 2: C
Line 3: G
Line 4: C
预期的输出是第2行和第3行。但是,awk直接从第一行转到第三行,而不是逐行。因此,输出为无。
亲切的问候!
最佳答案
编辑(要将每一行与其下一行进行比较,请使用此行):现在使用 OP 的新示例添加此解决方案。
awk '
FNR>1{
if(secCol=="C" && $2=="G" && fourthCol=="CG" && $4=="CG"){
print prevLine ORS $0
}
}
{
secCol=$2
fourthCol=$4
prevLine=$0
}
' Input_file
说明:为上述内容添加详细说明。
awk '
##Starting awk program from here.
FNR>1{
##Checking condition if current line number is more than 1 then do following.
if(secCol=="C" && $2=="G" && fourthCol=="CG" && $4=="CG"){
##Checking condition if secCol is C AND 2nd column is G AND fourthCol is CG and 4th column is CG then do following.
print prevLine ORS $0
##Printing prevLine ORS and current line.
}
}
{
secCol=$2
##Creating secCol with 2nd column of current line.
fourthCol=$4
##Creating fourthCol with 4th column of current line.
prevLine=$0
##Setting prevLine to current line value.
}
' Input_file ##Mentioning Input_file name here.
初始解决方案(比较每个奇数行和偶数行):(OP 的示例在编辑后变得更加清晰,但也将这个解决方案保留在这里,以供将来的读者使用,以防它有帮助)您能否尝试以下操作,仅按照所示示例编写。这会检查上一行是否有第四列(fourthCol)也是 CG
,以防万一您不需要它,然后从以下内容中删除 && foruthCol=="CG"
.
awk '
FNR%2==0{
if(secCol=="C" && $2=="G" && fourthCol=="CG" && $4=="CG"){
print prevLine ORS $0
}
prevLine=secCol=fourthCol=""
next
}
{
secCol=$2
fourthCol=$4
prevLine=$0
}
' Input_file
输出如下。
chr1 C 10469 CG CG 0.0 0 1
chr1 G 10470 CG CG 0.0 0 8
chr1 C 10471 CG CG 0.0 0 1
chr1 G 10472 CG CG 1.0 8 8
说明:为上述内容添加详细说明。
awk ' ##Starting awk program from here.
FNR%2==0{ ##Checking condition if line number is divided by 2 or not.
if(secCol=="C" && $2=="G" && fourthCol=="CG" && $4=="CG"){
##Checking condition if secCol is C AND 2nd column is G AND fourthCol is CG and 4th column is CG then do following.
print prevLine ORS $0 ##Printing prevLine ORS and current line.
}
prevLine=secCol=fourthCol="" ##Nullifying prevLone, secCol, fourthCol here.
next ##next will skip all further statements from here.
}
{
secCol=$2 ##Creating secCol with 2nd column of current line.
fourthCol=$4 ##Creating fourthCol with 4th column of current line.
prevLine=$0 ##Setting prevLine to current line value.
}
' Input_file ##Mentioning Input_file name here.
关于awk - 比较 awk 中的当前行和下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65360525/
是否可以从 awk 文件执行另一个 awk 文件?使用 awk 文件我需要执行当前文件夹中的所有 awk 文件。是否可以在 awk 中进行此类操作? 最佳答案 是的你可以。您需要使用 system()
这是一个 awk 脚本,它尝试根据第一列设置两个文件的差异: BEGIN{ OFS=FS="\t" file = ARGV[1] while (getline < file)
awk 逐行处理文件。假设每一行操作不依赖于其他行,有没有办法让 awk 一次并行处理多行? 是否有任何其他文本处理工具可以自动利用并行性并更快地处理数据? 最佳答案 唯一试图提供 awk 并行实现的
我有文件: 结果.txt Apple fruits 10 20 30 Car vehicle 40 50 60 Book study 70 80 90 假设这里第 2 列是特征,第 3 列是最小值
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我对 awk 的行为感到惊讶表演时浮点数 计算。它导致我对表格数据进行错误计算。 $ awk 'BEGIN {print 2.3/0.1}' 23 0.1}' )。 那么我应该如何执行大于 (
为什么我在下面的例子中得到分隔符前后的空格? awk -F'^' '{print $1,":",$2}' SERVER_2012-02-29-12-15-00 3969 : 1272 3969 :
我有一个文件,其中每四行是这样的: HISEQ15:454:D27KKACXX:6:2316:16241:100283 1:N:0:GTTTCG (对于那些感兴趣的人,此文件包含DNA序列) 我需
你能帮我按 $2 列中的坐标合并行吗?有一系列坐标以一个为单位增长。我想输出 f.e. :第 1 行合并到第 4 行 9079811-9079814,之后没有系列,因此将其合并到另一行等。对于输入中的
大家好,我是 awk 的新手,我可以问一下我有这样的输入文件吗: # ABC DEFG value1 GH value2 GH value3 GH # BCF SQW value4 GH value5
大家好,我想问一下,我对awk中的括号{}感到非常困惑,就像我写了一段代码 { FNR == 3 { print $1 " age is " $2 } } 但它在外括号上给了我错误但没有在打印语
我想知道如何在 awk 中使用多行注释。到目前为止,我一直在使用 # 来评论一行。有人可以就此指导我。谢谢你。 最佳答案 AWK 中没有多行注释,但如果需要,您可以伪造它。这是一种至少适用于 GNU
关于AND逻辑运算符的一个基本问题。我试图根据第1列和第2列的值提取数据文件niveles.csv中的某些字段。我想写一个awk语句,说“当field1 = date和field2 = area然后打
以下命令按预期工作。 # some command | awk '/(\|\|\)/,/;/' create table todel1 (id int) max_rows=2 /*!*/; alter
我有一个日志文件,需要在服务器上“重播”。 它包含这样的条目: Request: query: EXEC prc_insert_customer @param0: 11
如何从制表符分隔的字符串中选择第一列? # echo "LOAD_SETTLED LOAD_INIT 2011-01-13 03:50:01" | awk -F'\t' '{prin
我正在尝试在目录中的多个文件的内容中执行一些 grep 并将我的 grep 匹配附加到单个文件中,在我的输出中我还想要一个包含文件名的列,以了解哪些文件条目已被拾取。我试图使用 awk 来实现相同的目
我想选择文件中第9列的绝对值小于500的行。列有时为正,有时为负。 awk -F'\t' '{ if ($9 output.bam 到目前为止这不起作用..互联网上的一轮告诉我,要使用绝对值,我们应
例如,假设我运行以下命令: gawk -f AppendMapping.awk Reference.tsv TrueInput.tsv 假设文件名会改变。在遍历第一个文件时,我想创建一个映射。 map
我正在使用这个命令; awk -v regex1='new[[:blank:]]+File\(' 'BEGIN{print "Regex1 =", regex1}' 这警告我; awk: warnin
我是一名优秀的程序员,十分优秀!