Reformating ascii file with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reformating ascii file with awk
# 1  
Old 11-11-2009
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=
Code:
1 
1 
1 
1 
1 
2 
2 
2 
2 
3 
3 
3 
3 
3 
3 
3 
3 
4 
4

[...]

What I'd like to do is to assign the value "1" for the first record of a consistant block ( a block is defined by the same $5 number), "2" for middle records and "3" for the last one. Then another "1" for the next block, etc. :

In my exemple, the file may become :

$5=
Code:
1 
2 
2 
2 
3 
1 
2 
2 
3 
1 
2 
2 
2 
2 
2 
2 
3 
1 
3

[...]


I've tested some stuff with awk but I always failed...

If you've got some ideas to help me... you're welcome.

Nico.


ps : Please excuse my poor English, I'm from France Smilie

---------- Post updated at 04:23 PM ---------- Previous update was at 04:04 PM ----------

I've tryed this...

Code:
awk '{l[NR]=$1};

   	{for(i=1;i<=NR;i++)

	{if(l[i]!=l[i-1])print "1";

	if(l[i]==l[i-1] && l[i]==l[i+1]) print "2";

	if(l[i]!=l[i+1])print "3"}}' toto

but it does not work :/

Last edited by radoulov; 11-12-2009 at 03:45 AM.. Reason: added code tags
# 2  
Old 11-12-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'BEGIN { ARGV[ARGC++] = ARGV[ARGC-1] }
FNR == NR { _[$5]++ ? max[$5] = NR : min[$5] = NR; next }
{ $5 = FNR == min[$5] ? 1 : (FNR == max[$5] ? 3 : 2) } 5
' infile

Consecutive default FS characters will be squeezed because of the ricompilation of the record,
let me know if this could be a problem.
With no default FS an OFS should be explicitly specified.

Last edited by radoulov; 11-12-2009 at 04:39 AM..
# 3  
Old 11-12-2009
Thanks a lot radoulov..

It perfectly works...

I've tested my ancient script in an Exceed emulated unix... It also works... (whereas it doesn't works on cygwin...) But your solution keeps all the others columns. It's perfect for me.

For the moment I don't understand all the syntax of your script... but I'll try to study a little bit the problem. =)

Caribou.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

2. Shell Programming and Scripting

Removal Extended ASCII using awk

Hi All, I am trying to remove (SELECTIVE - passed as argument) Extended ASCII using Awk based on adhoc basis. Can you please let me know how to do it. I have to implement this using awk only. Thanks & Regads (14 Replies)
Discussion started by: tostay2003
14 Replies

3. 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

4. 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

5. 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

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

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 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

10. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: Martian
4 Replies
Login or Register to Ask a Question