variable fixed-width fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable fixed-width fields
# 1  
Old 11-21-2011
variable fixed-width fields

Hi there,

Code:
CTL  Port        IO Rate(IOPS)      Read Rate(IOPS)     Write Rate(IOPS) Read Hit(%) Write Hit(%)    Trans. Rate(MB/S) Read Trans. Rate(MB/S) Write Trans. Rate(MB/S)   09:36:48
  0     A                  136                    0                  135          97          100                    7                      0                       7   09:36:48
  0     B                    3                    0                    2         100          100                    0                      0                       0   09:36:48
  0     C                   88                   84                    3          35          100                    9                      9                       0   09:36:48
  0     D                  391                  240                  151          44          100                   11                      6                       4   09:36:48
  0     E                    0                    0                    0           0            0                    0                      0                       0   09:36:48

Quote:
awk '{print NF}' inputfile
shows the following result:
21
11
11
...

but I need to correlate header fields with rest of table and substitute fixed-width field separator with something else, for instance "\t".

Please advise.

The second task is to merge some columns into the one:
0 A to 0A
0 B to 0B
and so on.
# 2  
Old 11-21-2011
Try:
Code:
perl -pe 's/ {2,}/\t/g;s/\) +/\)\t/g;s/^\t//' inputfile

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 11-21-2011
Quote:
Originally Posted by bartus11
Try:
Code:
perl -pe 's/ {2,}/\t/g;s/\) +/\)\t/g;s/^\t//' inputfile

Thanx a lot, but I forgot to mention that the "inputfile" is a result of my previous parsing other files in which I use 'awk' and 'csplit' and for further parsing I plan to use the same tools plus 'sort', 'sed'...
Perl is too much Smilie
# 4  
Old 11-21-2011
There you go:
Code:
awk '{gsub("  +","\t");gsub("\\) +",")\t");sub("^\t","")}1' inputfile

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 11-21-2011
Quote:
Originally Posted by bartus11
There you go:
Code:
awk '{gsub("  +","\t");gsub("\\) +",")\t");sub("^\t","")}1' inputfile

regular expressions! of course!
where was mind? Smilie

going to manual page to learn what "1" after breket does mean

---------- Post updated 22-11-11 at 12:02 AM ---------- Previous update was 21-11-11 at 11:44 PM ----------

'1' means {print}
# 6  
Old 11-21-2011
Quote:
Originally Posted by gray380
'1' means {print}
Indeed.
# 7  
Old 11-22-2011
Okay. About my second task (fields merging). If number of fields is known value then it's easy:

Code:
 awk  'BEGIN {FS="\t"; OFS="\t"} {print $1$2, $3, $4, $5, $6, $7, $8, $9, $10 }'

what is the solution when number of fields is unknown?

---------- Post updated at 12:57 PM ---------- Previous update was at 12:45 PM ----------

the answer:
Code:
awk  'BEGIN {FS="\t"; OFS="\t"} { $1=$1$2; $2=""; print}' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Alter Fixed Width File

Thank u so much .Its working fine as expected. ---------- Post updated at 03:41 PM ---------- Previous update was at 01:46 PM ---------- I need one more help. I have another file(fixed length) that will get negative value (ex:-00000000003000) in postion (98 - 112) then i have to... (6 Replies)
Discussion started by: vinus
6 Replies

2. Shell Programming and Scripting

Using printf (or other?) to create variable fixed width text

I would like to use printf (or something else?) to create a line of text that has varying column widths. This will be used to create a fixed width file (with varying column widths). For example, consider variables $1 $2 $3 are equal to a, b, c respectively and they should be printed in column... (10 Replies)
Discussion started by: farrenthorpe
10 Replies

3. Shell Programming and Scripting

How to parse fixed-width columns which may include empty fields?

I am trying to selectively display several columns from a db2 query, which gives me a fixed-width output (partial output listed here): --------- -------------------------- ------------ ------ 000 0000000000198012 702 29 000 0000000000198013 ... (9 Replies)
Discussion started by: ahsh79
9 Replies

4. Shell Programming and Scripting

Printing Fixed Width Columns

Hi everyone, I have been working on a pretty laborious shellscript (with bash) the last couple weeks that parses my firewall policies (from a Juniper) for me and creates a nifty little columned output. It does so using awk on a line by line basis to pull out the appropriate pieces of each... (4 Replies)
Discussion started by: cixelsyd
4 Replies

5. Shell Programming and Scripting

summing up the fields in fixed width file

Hi, I have a fixed width file with some records as given below: " 1000Nalsdjflj243324jljlj" "-0300Njfowjljl309933fsf" " 0010Njsfsjklj342344fsl" I want to sum-up first field values(i.e from 2nd character to 6th character)of each record. so for the above file i want to add (1000 - 300+... (2 Replies)
Discussion started by: srilaxmi
2 Replies

6. Shell Programming and Scripting

Comparing column of variable length anf fixed width file

Hi, I have two input files. File1: ID Name Place 1-234~name1~Newyork 1-34~name2~Boston 1-2345~name3~Hungary File1 is a variable length file where each column is seperated by delimitter "~". File2: ID Country 1-34<<11 SPACES>>USA<<7 spaces>> 1-234<<10 SPACES>>UK<<8... (5 Replies)
Discussion started by: manneni prakash
5 Replies

7. Shell Programming and Scripting

Combining Two fixed width columns to a variable length file

Hi, I have two files. File1: File1 contains two fixed width columns ID of 15 characters length and Name is of 100 characters length. ID Name 1-43<<11 spaces>>Swapna<<94 spaces>> 1-234<<10 spaces>>Mani<<96 spaces>> 1-3456<<9 spaces>>Kapil<<95 spaces>> File2: ... (4 Replies)
Discussion started by: manneni prakash
4 Replies

8. UNIX Desktop Questions & Answers

Help with Fixed width File Parsing

I am trying to parse a Fixed width file with data as below. I am trying to assign column values from each record to variables. When I parse the data, the spaces in all coumns are dropped. I would like to retain the spaces as part of the dat stored in the variables. Any help is appreciated. I... (4 Replies)
Discussion started by: sate911
4 Replies

9. Shell Programming and Scripting

Extracting records with unique fields from a fixed width txt file

Greetings, I would like to extract records from a fixed width text file that have unique field elements. Data is structured like this: John A Smith NY Mary C Jones WA Adam J Clark PA Mary Jones WA Fieldname / start-end position Firstname 1-10... (8 Replies)
Discussion started by: sitney
8 Replies

10. UNIX for Dummies Questions & Answers

Fixed Width file using AWK

I am using the following command at the Unix prompt to make my 'infile' into a fixed width file of 100 characters. awk '{printf "%-100s\n",$0}' infile > outfile However, there are some records with a special character "©" These records are using 3 characters in place of one and my record... (2 Replies)
Discussion started by: alok.benjwal
2 Replies
Login or Register to Ask a Question