Help on awk.. reformating a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on awk.. reformating a file
# 1  
Old 08-06-2007
Help on awk.. reformating a file

Hello, I am having a trouble with awk attempting to reformat a two columns file , 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

What I need to do is 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


I have tried with the arithmetic and incremental operators but I did not get good results. I will deeply appreciate any help

Thanks!
# 2  
Old 08-06-2007
Do you have to use awk? Something like this works just as well:

Code:
#!/usr/bin/bash

changenumber=0
prevcol=""
while read col1 col2; do if [ "$prevcol" != "$col1" ]; then 
   prevcol=$col1
   changenumber=$(($changenumber+1))
fi
echo $col1 $col2 $changenumber
done < /path/to/file

# 3  
Old 08-06-2007
Code:
awk 'BEGIN{ prev = -1}{ if ( prev != $1 ) { cnt++; prev=$1 } $3=cnt; print }' filename

# 4  
Old 08-06-2007
In awk:
Code:
awk '{if ($1 != tmp ){tmp=$1;c++};print $0,c}' file

Regards
# 5  
Old 08-06-2007
Bug Re: Help on awk..

Thanks for the input blowtorch and matrixmadhan..
The awk script worked pretty straightforward! I really appreciate your input, I am sure that I could not have done it without this serious help!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

2. UNIX for Dummies Questions & Answers

Reformating unix data

Hi i have a unix date in file a file like this '1313675999' in oracle i would do it like this select TO_CHAR ( TO_DATE ('01011970', 'DDMMYYYY')+ 1 / 24 / 60 / 60 * 1313675999,'YYYYMMDD') from dual how to achive the same in unix ? (8 Replies)
Discussion started by: phpsnook
8 Replies

3. Shell Programming and Scripting

Splitting & reformating a single file

I have a bif text file with the following format: d1_03 fr:23 d1_03 fr:56 d1_03 fr:67 d1_03 fr:78 d1_01 fr:35 d1_01 fr:29 d1_01 fr:45 d2_09 fr:34 d2_09 fr:78 d3_98 fr:90 d3_98 fr:104 d3_98 fr:360 I have like thousands of such lines I want to reformat this file based on column 1... (3 Replies)
Discussion started by: Lucky Ali
3 Replies

4. Shell Programming and Scripting

reformating non-uniform strings

I have a set of free-form phone numbers that are not uniform and I want to reformat them into a standard uniform string. These are embedded at the end of a colon seperated file built by a large nawk + tr piping like such: XXXXX:YYYYY:ZZZZZ:(333)333-3333x33333 XXXXX:YYYYY:ZZZZZ:x44444... (9 Replies)
Discussion started by: lordsmiter
9 Replies

5. Shell Programming and Scripting

Parse file using awk and work in awk output

hi guys, i want to parse a file using public function, the file contain raw data in the below format i want to get the output like this to load it to Oracle DB MARWA1,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 MARWA2,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 this the file raw format: Number of... (6 Replies)
Discussion started by: dagigg
6 Replies

6. Shell Programming and Scripting

Rows to columns transposing and reformating.

----File attached. Input file =========== COL_1 <IP Add 1> COL_2 <Service1> COL_3 <ABCDEFG> COL_4 <IP ADD:PORT> COL_4 <IP ADD:PORT> COL_1 <IP Add 2> COL_2 <Service2> COL_2 <Service3> COL_2 <Service4> COL_3 <AAAABBB> COL_4 <IP ADD:PORT> COL_4 <IP ADD:PORT> COL_4 <IP... (27 Replies)
Discussion started by: bluethunder
27 Replies

7. Shell Programming and Scripting

Reformating ascii file with awk

Hello, I've a lot of ascii files that I would like to reformat : One of files's column (for exemple $5) contains increasing numbers (see exemple) : $5= 1 1 1 1 1 2 2 2 2 3 3 (2 Replies)
Discussion started by: Caribou
2 Replies

8. UNIX for Advanced & Expert Users

Selectively Reformating a file using AWK

Dear users, I am new to AWK and have been battling with this one for close to a week now. Some of you did offer some help last week but I think I may not have explained myself very well. So I am trying again. I have a dataset that has the following format where the datasets repeat every... (5 Replies)
Discussion started by: sda_rr
5 Replies

9. Shell Programming and Scripting

awk - reformating rows into columns

looking to do the following... What the data looks like server1 02/01/2008 groups 10 server1 03/01/2008 groups 15 server1 04/01/2008 groups 20 server2 02/01/2008 users 50 server2 03/01/2008 users 75 server2 04/01/2008 users 100 server2 04/01/2008 users 125 What I would like the... (1 Reply)
Discussion started by: jmd2004
1 Replies
Login or Register to Ask a Question