Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8291635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:11:58+00:00 2026-06-08T13:11:58+00:00

I have an input file that looks like this (first column is a location

  • 0

I have an input file that looks like this (first column is a location number and the second is a count that should increase over time):

1       0
1       2
1       6
1       7
1       7
1       8
1       7
1       7
1       9
1       9
1       10
1       10
1       9
1       10
1       10
1       10
1       10
1       10
1       10
1       9
1       10
1       10
1       10
1       10
1       10
1       10

and I’d like to fix it look like this (substitute counts that decreased with the previous count):

1       0
1       2
1       6
1       7
1       7
1       8
1       8
1       8
1       9
1       9
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10
1       10

I’ve been trying to use awk for this, but am stumbling with getline since I can’t seem to figure out how to reset the line number (NR?) so it’ll read each line and it’s next line, not two lines at a time. This is the code I have so far, any ideas?

awk '{a=$1; b=$2; getline; c=$1; d=$2; if (a==c && b<=d) print a"\t"b; else print c"\t"d}' original.txt > fixed.txt

Also, this is the output I’m currently getting:

1       0
1       6
1       7
1       7
1       9
1       10
1       9
1       10
1       10
1       9
1       10
1       10
1       10
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:11:59+00:00Added an answer on June 8, 2026 at 1:11 pm

    Perhaps all you want is:

    awk '$2 < p { $2 = p } { p = $2 } 1' input-file
    

    This will fail on the first line if the value in the second column is negative, so do:

    awk 'NR > 1 && $2 < p ...'
    

    This simply sets the second column to the previous value if the current value is less, then stores the current value in the variable p, then prints the line.

    Note that this also slightly modifies the spacing of the output on lines that change. If your input is tab-separated, you might want to do:

    awk 'NR > 1 && $2 < p { $2 = p } { p = $2 } 1' OFS=\\t input-file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem I have a tab delimited input file that looks like so: Variable
I'm using getline to grab lines from an input data file that looks like
Hi, I have a couple of input type file elements like this : <input
Please help with the following: I have an input file that is quite heterogeneous
I have a php file that contains a form (which contains 2 input boxes
I have a program that reads input from a file. I am trying to
I have a perl program that takes input and output file arguments, and I'd
I have a linux utility that parses structured input ( a text file ),
If I have an input file like: US, P1, AgriZone, H, 1000, 1200, 1101,
I have an input CSV file with a column containing information similar to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.