473,420 Members | 1,588 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,420 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 1742
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.