1

Count in the awk loop if anything else

 2 years ago
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

advertisements

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

Tags awk

Related Articles

How to increment the counter in the python loop?

I'm just trying to increment a simple counter in my for loop, but this function always returns 54. I'm guessing it does this because it sees the second counter as a local variable inside the for loop. How can I increment the counter?? I feel like thi

The counter in the foreach loop does not work

I have a foreach loop that I need numbered classes so I set up a counter: <?php $counter = 0; ?> <?php $html = ''; foreach ($json['feed']['entries'] as $entries){ $counter++; $html .= '<a href="' .$entries['link']. '"><span cla

Why does the "continue" statement ignore the increment of the loop counter in the 'while' loop, but not in 'for'?

Why does it tend to get into an infinite loop if I use continue in a while loop, but works fine in a for loop? The loop-counter increment i++ gets ignored in while loop if I use it after continue, but it works if it is in for loop. If continue ignore

JS: The loop counter inside the For loop is still undefined

I have a JS function to check if a file has a valid extension and return true or false to indicate file is acceptable or not. My hidden field with value in markup: <input id="hidAcceptedFormat" name="acceptedFileFormat" value="

Limit the number of characters counted for the getChar loop in C

I'm making a small program that takes input from the user and display's how many times each vowel occurs in the input (as well as the number of digits). The one thing I can't figure out how to do is limit it so that only the first 50 characters are c

Set a breakpoint for the counter in the java loop

I wrote a method in java that takes an array of ints as argument & retuns true if that array contains three consecutive adjacent numbers. Example 4, 5, 6, or 23, 24, 25. My method works with but with one exception. The problem is i counter increases

The increment of the counter in the Bash loop does not work

I have the following simple script where I am running a loop and want to maintain a COUNTER. I am unable to figure out why the counter is not updating. Is it due to subshell thats getting created? How can I potentially fix this? #!/bin/bash WFY_PATH=

Adding Counter to the Enhanced Loop

I have a code which takes name and number from user and save those in an arraylist as object. I am using this enhanced for loop to printout all name and number which is stored in that arraylist ... for(Objectclass p : Test) { System.out.println("Name

How to print the insert in the awk loop

How to print the file name in the loop? I want to print the file name and the average value of column 4 at same line: for i in `ls *cov` do awk '{sum +=$4;n++}END{print sum/n}' $i done I mean I want to awk '{sum +=$4;n++}END{print $i\t sum/n}' $i You

Display the message in the foreach loop with if else condition

I want to display error message if condition is false in foreach loop. Here is my code foreach($items as $item){ if($item['userid'] == logged_in()) { echo $item['title']; } else { echo 'You do not have permission to view items'; } } // other code her

Can you write the IF statement without anything else in javascript

I wrote some code to create an simple todo app in angular js. I have the following function code to remove the tasks from the list. Javascript code $scope.deleteTask = function(){ $scope.tasks.splice(this.$index, 1); if($scope.tasks.length < 1){ $sco

& Ldquo; Else if & rdquo; Declaration in the for loop in a larger statement if / else if / else

Is it possible to do something like this in JavaScript? if (name == 'foo') { exampleFunction('some_arg'); } else if (name == 'bar') { exampleFunction('another_arg'); } for (i in exampleObject) { else if (name == exampleObject[i].name) { exampleFuncti

Besides malloc / free, does a program need the operating system to provide anything else?

I'm working on designing the kernel (which I'm going to actually call the "core" just to be different, but its basically the same) for an OS I'm working on. The specifics of the OS itself are irrelevant if I can't get multi-tasking, memory manag

The button does not work until the current loop ends in python

In python 2.7 if i click a button when a loop is running IDLE stops working till python comes out of the loop. I've attached my entire code as I don't have any idea why would this be happening. import time import Tkinter as tk from Tkinter import Str

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK