

Count in the awk loop if anything else
source link: https://www.codesd.com/item/count-in-the-awk-loop-if-anything-else.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Count in the awk loop if anything else
can you explain to me why this simple onliner does not work? Thanks for your time.
awk 'BEGIN{i=1}{if($2 == i){print $0} else{print "0",i} i=i+1}' check
input text file with name "check":
a 1
b 2
c 3
e 5
f 6
g 7
desired output:
a 1
b 2
c 3
0 4
e 5
f 6
g 7
output received:
a 1
b 2
c 3
0 4
0 5
0 6
awk 'BEGIN{i=1}{ if($2 == i){print $0; } else{print "0",i++; print $0 } i++ }' check
- increment
i
one more time in the else (you are inserting a new line) - print the currentline in the else, too
- this works only if there is only one line missing between the present lines, otherwise you need a loop printing the missing lines
Or simplified:
awk 'BEGIN{i=1}{ if($2 != i){print "0",i++; } print $0; i++ }' check
Related Articles
How to increment the counter in the python loop?
The counter in the foreach loop does not work
Why does the "continue" statement ignore the increment of the loop counter in the 'while' loop, but not in 'for'?
JS: The loop counter inside the For loop is still undefined
Limit the number of characters counted for the getChar loop in C
Set a breakpoint for the counter in the java loop
The increment of the counter in the Bash loop does not work
Adding Counter to the Enhanced Loop
How to print the insert in the awk loop
Display the message in the foreach loop with if else condition
Can you write the IF statement without anything else in javascript
& Ldquo; Else if & rdquo; Declaration in the for loop in a larger statement if / else if / else
Besides malloc / free, does a program need the operating system to provide anything else?
The button does not work until the current loop ends in python
Recommend
-
46
强大的AWKAWK是一个优良的文本处理工具,Linux及Unix环境中现有的功能最强大的数据处理引擎之一。这种编程及数据操作语言(其名称得自于它的创始人阿尔佛雷德·艾侯、彼得·温伯格和布莱恩·柯林汉姓氏的首个字母)的最大功能取决于一个人所拥有的知识。awk经过改进生...
-
39
一、AWK介绍  Linux文本处理工具三剑客:grep、sed和AWK。其中grep是一种文本过滤工具,sed是文本行编辑器,而AWK是一种报表生成器,就是对文件进行格式化处理,这里的格式化不是文件系统的格式化,而是对文件内容进行各种“排版”,进而格式化显...
-
38
GoAWK: an AWK interpreter written in Go AWK is a fascinating text-processing language, and somehow after reading the delightfully-terse
-
8
variable to continue counting the count in the loop and printing a total at the end advertisements I'm new to scripting and I'm trying to make...
-
7
This site can’t be reached anything.else.other.than.vi’s server IP address could not be found. ...
-
5
How anything in the world is related to everything elseSystem is a free, open, and living public resource that aims to explain how anything in the world is related to everything else.Here is an overview:
-
5
Will Hare replace C? Or Rust? Or Zig? Or anything else? The Hare programming language...
-
6
miCountCount anything. Build Consistency.
-
8
In this Python tutorial, you will learn how to combine a for loop with if / if-else statements in Python. Table Of Contents Introduction As we know that a List Comprehension is used to cre...
-
6
Update to iOS 16.0.1 on your new iPhone 14 before y...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK