Format columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format columns
# 1  
Old 09-13-2010
Format columns

I have this format of columns

Quote
Code:
2 Points:
  np           x               y             z
   0        767203.9         2120710     917.2959
   1        767071.6         2120658     2793.661

Surface Polyline
Color: (0.0400229 1 0.845803)
2 Points:
  np           x               y             Depth
   0        767216.5         2120580     917.2959
   1        766846.8         2120435     2779.101

Surface Polyline
Color: (0.0400229 1 0.845803)
3 Points:
  np           x               y             Depth
   0        767160.6         2120424     909.7954
   1        766825.5         2120292      2767.58
   2        766825.5         2120292     2793.502

............................................................
Unquote

I would like to convert the values into

Code:
   x                   y        group  z
  767203.9         2120710  0   917.2959
  767071.6         2120658  0   2793.661
  767216.5         2120580  1   917.2959
  766846.8         2120435  1   2779.101
  767160.6         2120424  2   909.7954
  766825.5         2120292  2   2767.58
  766825.5         2120292  2   2793.502

The x and y followed by the no of points as a group (e.g 2 points in first 2 rows become group 0, 2 points in 2nd row as group 1, 3 points in 3rd row so group name 3.....etc).

I could successfully put x y and z values but while looping thru np values I am stuck.

any idea?

Last edited by Franklin52; 09-13-2010 at 03:23 AM.. Reason: Please use code tags!
# 2  
Old 09-13-2010
Don't forget the code tag.

Code:
 awk 'BEGIN{i=0;print "x\t\ty\tgroup\tz"}/^$/{i++} $1~/^[0-9]/&&NF>3 {print $2,$3,i,$4 }' OFS="\t" infile


Last edited by rdcwayx; 09-13-2010 at 03:46 AM..
# 3  
Old 09-13-2010
Hi rd

almost done. Problem is in the grouping still as you may have noticed if np occurs 3 times then all the three rows become the same group e.g if np occures 4 times i say row 8,9,10,11 then the group id for those four rows will be the same say 8 (entrey four times).

I did the awk you suggest and this was the o/p

Code:
x               y       group   z
752670.2\t2150082\t2\t2777.672
752729.8\t2149930\t2\t904.061
752786.5\t2150128\t3\t913.8535
752726.9\t2150280\t3\t2784.2
752783.6\t2150477\t4\t959.5513
752902.9\t2150173\t4\t2807.049
752899.9\t2150523\t5\t904.061
753019.2\t2150219\t5\t2774.407
752897\t2150872\t6\t923.6458

(Occurence of group id only twice according to the above output)

Any suggestion pls?

Last edited by Scott; 09-13-2010 at 09:02 AM.. Reason: Use code tags, please.
# 4  
Old 09-13-2010
Not understand your problem.

Seems your awk don't know OFS, then try this code:

Code:
awk '
BEGIN{i=0;print "x\t\ty\tgroup\tz"}
/^$/{i++}
$1~/^[0-9]/&&NF>3 {printf $2"\t"$3"\t"i"\t"$4"\n" }
' infile
x               y       group   z
767203.9        2120710 0       917.2959
767071.6        2120658 0       2793.661
767216.5        2120580 1       917.2959
766846.8        2120435 1       2779.101
767160.6        2120424 2       909.7954
766825.5        2120292 2       2767.58
766825.5        2120292 2       2793.502

So with your sample, I see three np line, so the group id start from 0 to 2.

Any problem?
This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 09-13-2010
perl

Code:
local $/="\n\n";
while(<DATA>){
  my @tmp = split("\n",$_);
  foreach(@tmp){
    if(!/[a-zA-Z]/){
    my @t = split;
    print $t[1]," ",$t[2]," ",$.-1," ",$t[3],"\n";
}
  }
}
__DATA__
2 Points:
  np           x               y             z
   0        767203.9         2120710     917.2959
   1        767071.6         2120658     2793.661

Surface Polyline
Color: (0.0400229 1 0.845803)
2 Points:
  np           x               y             Depth
   0        767216.5         2120580     917.2959
   1        766846.8         2120435     2779.101

Surface Polyline
Color: (0.0400229 1 0.845803)
3 Points:
  np           x               y             Depth
   0        767160.6         2120424     909.7954
   1        766825.5         2120292      2767.58
   2        766825.5         2120292     2793.502

# 6  
Old 09-13-2010
USE this:
Code:
awk 'BEGIN{OFS="\t";i=1;print "x\t\t y\tgroup\t\tz"} /^$/ {i++}$1~/[0-9]/ && $2 ~/[0-9]/ {print $2,$3,$4,i}' file


Last edited by Franklin52; 09-14-2010 at 03:47 AM.. Reason: Please use code tags, thank you!
# 7  
Old 09-14-2010
Thanks to Rahul, Summer and rdc:

Problem is np occurs not 3 times it may occur 4 times with 0 1 2 3 also in the pattern of rows.
Original quoted input files is being cut only for space constraint. Rahul's suggestion also o/p the same result as rdc where grouping is also only limted upto two rows (here occurences of 0 1 2....)

Pls guy throw some light? I am nawking?

---------- Post updated at 03:35 PM ---------- Previous update was at 03:07 PM ----------

Hi rdcwayx,

Gre8t of that....mod on the awk. Yeah I re-ran the script. I think it works. Tho problem still in the group. I wud like the grouping to begin always from 0 (say if np occurs thrice in the first 3 rows) then the group for those 3 rows will be 0. Next occurences of np (say two times then those 2 rows will have group id as 1 etc).

Great idea......n thanks a helluva
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Format columns and heads using shell script

My program output text file data in columns format. My problem is the column headers do not align with the data and if filesystem and Mounted On columns pathname are too long the text will not wrap. Is there any function in Unix that I can use in my code that will align the data for each... (11 Replies)
Discussion started by: dellanicholson
11 Replies

2. Shell Programming and Scripting

Sum of columns and format the output

Input file: 011100020100 0.00 1 20000 30000 20000 011110000025 0.00 1 000 240000 10000 011100020100 0.00 1 200000 2324000 403500 032200030025 0.00 1 2077500 3077500 250000 032200030025 0.00 1 2565000 25536400 320000 022220000005 0.00 1 10000 300000 300000 022220000005 0.00 1 200050... (7 Replies)
Discussion started by: vinus
7 Replies

3. Programming

Transforming 3 columns to matrix format

Dear All I have a huge data for 3 columns similar to this D2cls0 D2cls0 1 D2cls0 D2cls1 0.308 D2cls0 D2cls2 0.554 D2cls0 D2cls3 0.287 D2cls0 D2cls4 0.633 D2cls0 D2cls5 0.341 D2cls0 D2cls6 0.665 D2cls0 D2cls7 0.698 D2cls0 D2cls8 0.625 D2cls0 D2cls9 0.429 I... (9 Replies)
Discussion started by: bala06
9 Replies

4. Shell Programming and Scripting

awk printf format in columns

Hi all, i have written this script: awk -F';' ' BEGIN { printf "\n" printf "\n" printf "\n" printf "----------------------------------------------\n" print " For test " printf "----------------------------------------------\n" test_200 = 0 test_300 = 0 test_500 = 0 test_1000 = 0... (11 Replies)
Discussion started by: arrals_vl
11 Replies

5. Shell Programming and Scripting

How can I format a text file into uniform columns?

I have a file : e.g. Charles Dixon Age 23 Hometown Darlington Postcode DL1 2DC Fred Bixton Age 34 Hometown Leeds Postcode LS1 5XS Jim Davis Age 48 Hometown Cardiff CF2 8YY Is it possible to format this file into uniform columns using, say, the spaces as... (11 Replies)
Discussion started by: bigbuk
11 Replies

6. Shell Programming and Scripting

Format row data into columns

I have a file which looks like this: /* ----------------- EDW$MOC139_R_NNA_BR_SUM_FACT2 ----------------- */ insert_job: EDW$MOC139_R_NNA_BR_SUM_FACT2 job_type: c command: /home/btchproc/load_process/batch_files/batch_nna_brn_split_sum_fact2.sh m machine: edwprod02.dsm.pwj.com #owner:... (29 Replies)
Discussion started by: Gangadhar Reddy
29 Replies

7. Shell Programming and Scripting

Format columns

I have a very easy one for you this morning :) A file containing this type of formated data: 500190 488.356 500193 546.7 566486 466.75 506654 288 However, it should be formated like this: 500190 488.356 500193 546.700 566486 466.750 506654 288.000 I know that this can be... (3 Replies)
Discussion started by: Medova
3 Replies

8. Programming

Creating a table like format with rows and columns

I have few files which have two columns in each. like e2 1 1 2694 2 4 2485 3 2 2098 5 1 2079 6 5 2022 9 4 1734 11 5 1585 13 2 1461 18 1 1092 21 2 1019 24 1 915 25 3 907 27 1 891 28 3 890 34 1 748 39 1 700 (1 Reply)
Discussion started by: kamuju
1 Replies

9. Shell Programming and Scripting

Format data to columns addind spaces

Hi all, I have a problem to format data from different database queries into one look. The input data are as follows, every line has the same number of values but a different number of characters: adata, bdata, cdata, ddata fffdata, gdata, hdata, idata jdata, kdata, ... (6 Replies)
Discussion started by: old_mike
6 Replies

10. Shell Programming and Scripting

How to format columns using AWK

Hi, How to format something like this: John Roberts 324-141-984 Acct Jack Williams 159-555-555 Acct William Jackson 949-911-888 Acct Mark J Walton 145-852-252 Acct Fred P Milton 483-244-390 Acct Bill P J Miller 404-050-223 Acct into... (12 Replies)
Discussion started by: ora_umair
12 Replies
Login or Register to Ask a Question