Selectively Reformating a file using AWK


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Selectively Reformating a file using AWK
# 1  
Old 03-23-2009
Computer 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 three lines. So the headings come first and then the packets of data are written out in the file in a similar manner.

col1 col2 col3
col4 col5 col6
col7 col8 col9
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27


and I need to change it to the format below:

col1 col2 col3 col4 col5 col6 col7 col8 col9
1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26 27

Now if that weren't bad enough. Say I only wanted the data from col2, col5 and col8. Or I wanted to select the data from any other column and only that column to be printed out? How would I do this please? The data pattern is regular and repeats itself and I could find out how many lines the file has in total if need be.

Thanks in advance.

Regards,


sda_rr
# 2  
Old 03-23-2009
here's the first part:
Code:
nawk 'ORS=FNR%3?FS:RS' myFile

# 3  
Old 03-23-2009
# default fields '2 5 8'
nawk -f sda.awk myFile

# specified fields: '1 3 9'
nawk -v fld='1 3 9' -f sda.awk myFile

sda.awk:
Code:
BEGIN {
  fld=(fld=="")? "2 5 8":fld
  n=split(fld, a, FS)
}
{
     rec=(rec=="")?$0:rec OFS $0
}
!(FNR%3){
     rN=split(rec, recA, OFS)
     rec=""
     for(i=1; i<=n; i++)
       printf("%s%c", recA[a[i]], (i==n) ? ORS : OFS)
}

# 4  
Old 03-23-2009
Thanks very much but you lost me here!!

What do you mean exactly by default and specified fields? If you specify the fields, does it oiverride the default?

Thanks.

# default fields '2 5 8'
nawk -f sda.awk myFile

# specified fields: '1 3 9'
nawk -v fld='1 3 9' -f sda.awk myFile
# 5  
Old 03-23-2009
Quote:
Originally Posted by sda_rr
Thanks very much but you lost me here!!

What do you mean exactly by default and specified fields? If you specify the fields, does it oiverride the default?

Thanks.

# default fields '2 5 8'
nawk -f sda.awk myFile

# specified fields: '1 3 9'
nawk -v fld='1 3 9' -f sda.awk myFile
yes, that's right - try it and see.
In the future please use BB Code tags when giving data and/or code samples - you'll be increasing your chances of your questions being answered.
# 6  
Old 03-23-2009
Thanks very much for your help. I think I can run with that.

Best Regards,

sda_rr
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Selectively extracting entries from FASTA file

I would like to extract all entries containing the following patterns: ccccta & ccccccccc from the following infile: >P39PT-1224_Freq_900 cccctacgacggcattggtaatggctcccgcaagccatctctcttcagccaagg >P39PT-784_Freq_2 cccctacgacggcattggtaatggcacccgcaagccatctctcttccccccccc >P39PT-678_Freq_5... (4 Replies)
Discussion started by: Xterra
4 Replies

2. Shell Programming and Scripting

How to remove spaces from a file selectively?

Hi i have a file in which i am doing some processing. The code is as follows: #!/bin/ksh grep DATA File1.txt >> File2.txt sed 's/DATA//' File2.txt | tr -d ‘ ‘ >> File4.xls As you can see my output is going in a xl file.The output consist of four columns/feilds out of which the first... (20 Replies)
Discussion started by: Sharma331
20 Replies

3. Shell Programming and Scripting

Awk or Perl - to selectively merge two files.

I have two files, these have to be selectively merged into two other files. In addition there will require to be a edit to the last field, where the date format is changed. The first file is a csv file with around 300k lines the data is now nearly 20 years old and I have been asked to move this... (7 Replies)
Discussion started by: gull04
7 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

Selectively Find/Replace in a file?

I have a file that is HTML encoded. Each line has something like this on each line.. <href=http://link.com/username.aspx>username </a> more info.. <a href=http://link.com/info1.aspx>info1</a> more code... <a href=http://link.com/info2.aspx>info2</a> I have one goal really.. to clean up the... (2 Replies)
Discussion started by: dragin33
2 Replies

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

7. UNIX for Dummies Questions & Answers

Removing selectively the last character from a file

Dear Members, Problem is suppose i have 50 lines in a file, 40 lines last character is "\" and the remaining 10 lines are good(i mean these 10 lines do not have "\" character) How can i remove this character from the file. Thanks (1 Reply)
Discussion started by: sandeep_1105
1 Replies

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

9. Shell Programming and Scripting

Selectively splitting a file with C-shell?

I have a rather long csh script that works, but it's terribly ungraceful and takes a while from various loops. I only know enough code to get myself into trouble, so I'm looking for some guidance. I have a large file that is separated at intervals by the same line, like this: ... (2 Replies)
Discussion started by: fusi0n
2 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