473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Help on awk scripting!!

Hello, I am really having a trouble on something that at first glance seemed easy to do in awk...
I am attempting to reformat a file that has two colums, such as below:

201 84
201 370
201 544
201 600
213 99
213 250
213 431
220 65
220 129
220 338
220 408
220 501
220 550
231 101
231 350

The idea is to add a third column containing a numeric value (a sort of flag) that increments by a value of 1 (one) as the first column value changes (the value of the first column itself is irrelevant, the important thing is to track when the value changes..). The output should be something like this:

201 84 1
201 370 1
201 544 1
201 600 1
213 99 2
213 250 2
213 431 2
220 65 3
220 129 3
220 338 3
220 408 3
220 501 3
220 550 3
231 101 4
231 350 4

...and so on...

I have tried with the arithmetic and incremental operators but I did not get good results. I will deeply appreciate any help on this from any of the awk gurus..!

Thanks!
Aug 3 '07 #1
2 1736
Hello, I am really having a trouble on something that at first glance seemed easy to do in awk...
I am attempting to reformat a file that has two colums, such as below:

201 84
201 370
201 544
201 600
213 99
213 250
213 431
220 65
220 129
220 338
220 408
220 501
220 550
231 101
231 350

The idea is to add a third column containing a numeric value (a sort of flag) that increments by a value of 1 (one) as the first column value changes (the value of the first column itself is irrelevant, the important thing is to track when the value changes..). The output should be something like this:

201 84 1
201 370 1
201 544 1
201 600 1
213 99 2
213 250 2
213 431 2
220 65 3
220 129 3
220 338 3
220 408 3
220 501 3
220 550 3
231 101 4
231 350 4

...and so on...

I have tried with the arithmetic and incremental operators but I did not get good results. I will deeply appreciate any help on this from any of the awk gurus..!

Thanks!
Assume you have the columns above in a file call list.txt (no extra newline/CRLF at the EOF), and the following awk script in doit.awk.

BEGIN {
counter = 0
prevval = 0
}

{
if ($1 != preval){
counter += 1
preval = $1
}
printf "%d %d %d\n", $1, $2, counter
}

END {
printf "Hooha!"
}


Just run:

awk -f doit.awk list.txt > list2.txt

Desired output will be in list2.txt.

This script compares the first column to the previous known value. If they are different, the counter variable gets incremented, and is printed as an additional column.
Aug 20 '07 #2
Or:

Expand|Select|Wrap|Line Numbers
  1. awk '!x[$1]++{c++};$3=c' infile

Use nawk on Solaris.
Aug 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Rob Cowie | last post by:
Hi, This is my first post so go easy! I have been asked (as part of an MSc project) to create a server based planner for a research group at my uni. It will have a web interface to interact...
9
by: What-a-Tool | last post by:
Dim MyMsg Set MyMsg = server.createObject("Scripting.Dictionary") MyMsg.Add "KeyVal1", "My Message1" MyMsg.Add "KeyVal2", "My Message2" MyMsg.Add "KeyVal3", "My Message3" for i = 1 To...
7
by: Scott M. | last post by:
How can I disable the cross-site scripting check for one particular page of a site?
17
by: freemann | last post by:
Can anyone provide example code showing how to send form results to a results page, email and a comma delimited file? Notice that I need it going to all three locations. Details: I have forms...
0
by: liezer | last post by:
I am writing my first script to execute a SQL/Oracle procedure through a unix script. Could you tell me how do I display an ERROR MESSAGE when FAILED_LOGIN_ATTEMPTS is greater than 3.... this...
9
by: i | last post by:
what is scripting language?We can create websites by using HTML. But we are using scripting language with HTML to create forms and websites.Advantage of using scripting language instead of HTML?
5
WebMissy
by: WebMissy | last post by:
I have a forum all set up and everything works great. I decide to use IE 7 for the benefit of transparent PNG files. All is well with that but now my scripting for my colored scrollbars doesn't show...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.