awk - Read fixed format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Read fixed format
# 1  
Old 05-15-2013
awk - Read fixed format

Hi,
I have several fixed format ascii files which I would like to reformat through Awk. The fixed format produces intermittent whitespace (Nastran input file).

Code:
 
$ Grid points
$------1-------2-------3-------4-------5-------6-------7-------8-------9-------0
GRID* 1 0-2.79677946159+2-1.85355339059+2
* 1.000000000000+1 0
GRID* 2 0-2.79677946159+2-1.75355339059+2
* 1.000000000000+1 0
GRID* 3 0-2.79677946159+2-1.65355339059+2
* 1.000000000000+1 0
GRID* 4 0-2.79677946159+2-1.55355339059+2
* 1.000000000000+1 0
 
GRID* 16 0-2.79677946159+2-3.53553390593+1
* 1.000000000000+1 0
GRID* 17 0-2.44322607100+2 0.0
* 1.000000000000+1 0
GRID* 18 0-2.51393674912+2 -7.071067811865
* 1.000000000000+1 0
GRID* 44 0-1.49585269653+1-1.4673061224-13
* 1.000000000000+1 0
GRID* 45 0 -4.986175655110-1.5311020408-13
* 1.000000000000+1 0
GRID* 46 0 4.9861756551108-1.5948979591-13
* 1.000000000000+1 0
GRID* 47 01.495852696533+1-1.6586938775-13
* 1.000000000000+1 0

The Awk script that I have mashed together so far (adding an 'E' to make standard scientific format, then removing the unwanted 'E' at the beginning of the fields that have a minus sign, operating on different cases to cope with the intermittent whitespace, not clever!) is as follows :

Code:
 
awk '/GRID/ {
a = NF;
printf "%s\t",a;
if ($3 != 0 && NF == 3){
$3 = substr($3,2);
$4 = substr($3,1,16);
$5 = substr($3,17,32);
gsub(/+/,"E+");
gsub(/-/,"E-");
/^[E]/; $4 = substr($4,2); $5 = substr($5,2)
}
if ($3 == 0 && NF == 4){
b = length($4);
printf "%s\t",b;
#$4 = substr($4,1,b-1-16);
$4 = substr($4,1,15);
#$5 = substr($4,b-1);
$5 = substr($4,16);
#$4 = 444;
#$5 = 555;
gsub(/+/,"E+");
gsub(/-/,"E-");
/^[E]/; $4 = substr($4,2); $5 = substr($5,2)
}
printf "%-4i%20e%20e\n",$2,$4,$5}' data.txt > revdata.txt

The data that I require is $2 following the '/GRID/, the next two numbers in scientific format and $2 on the next line (after the *). I would prefer to keep the whole process within Awk for my slant on being portable.
Does anyone have a smarter strategy to trap this number format?

Regards, Tim

Last edited by Scrutinizer; 05-15-2013 at 01:09 PM.. Reason: fixed code tags - use 'code' istead of 'icode' - Also changed quote tags to code tags
# 2  
Old 05-15-2013
Can you post a sample of the desired output...
# 3  
Old 05-15-2013
Hi Shamrock,
Thank you for your reply.

This data represents X Y Z coordinates.

The desired output format is:
Code:
 
1 -2.796779e+02 -1.853553e+02 1.000000e+01
2 -2.796779e+02 -1.753553e+02 1.000000e+01
3 -2.796779e+02 -1.653553e+02 1.000000e+01
4 -2.796779e+02 -1.553553e+02 1.000000e+01
16 -2.796779e+02 -3.535534e+01 1.000000e+01
17 -2.4432260+02 0.000000e+00 1.000000e+01
18 -2.5139367+02 -7.071067e+00 1.000000e+01
44 -1.495853e+01 -1.467306e-13 1.000000e+01
45 -4.986176e+00 5.500000e+01 1.000000e+01
46 9.861757e-01 5.500000e+01 1.000000e+01
47 4.958527e+00 -1.658694e-13 1.000000e+01

Regards, Tim

Last edited by Scrutinizer; 05-15-2013 at 01:09 PM.. Reason: quote tags to code tags
# 4  
Old 05-15-2013
And there is no whitespace between the the 3rd and 4th fields correct...
Code:
GRID* 47 01.495852696533+1-1.6586938775-13

# 5  
Old 05-15-2013
The format of the input file is data sets that occupy two lines each.
In line 1 of 1: field 1 is 'GRID*', field 2 is the unique label, field 3 is zero, field 4 is 'X', field 5 is 'Y'.
In line 2 of 2: field 1 is the continuation character '*', field 2 is 'Z', field 3 is zero.

My difficulty is that the fixed fields 4 and 5 on line 1 of 1 are often full (no whitespace) but can be less than full as in example input below, where there can is whitespace between fields 3 and 4 (GRID* 45) and between fields 4 and 5 (GRID* 18):
Quote:
GRID* 18 0-2.51393674912+2 -7.071067811865
* 1.000000000000+1 0
GRID* 44 0-1.49585269653+1-1.4673061224-13
* 1.000000000000+1 0
GRID* 45 0 -4.986175655110-1.5311020408-13
* 1.000000000000+1 0
Tim
# 6  
Old 05-15-2013
Try this sed and awk pipeline...
Code:
sed -e 's/\([0-9]\)\([0-9]\)\([.]\)\([0-9]\)/\1 \2\3\4/g' -e 's/\([0-9]\)\([+-]\)\([0-9]\)\([.]\)/\1 \2\3\4/g' data.txt | \
awk '{s = $2 OFS $4 OFS $5; getline; if ($0 ~ "^[*]") s = s OFS $2; print s}' > revdata.txt

# 7  
Old 05-15-2013
Shamrock,
Your concise code does the job proficiently; I greatly appreciate your help.
Regards, Tim
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace using awk on fixed width file.

All, I used to use following command to replace specific location in a fixed width file. Recently looks like my command stopped working as intended. We are on AIX unix. awk 'function repl(s,f,t,v) { return substr(s,1,f-1) sprintf("%-*s", t-f+1, v) substr(s,t+1) } NR<=10 {... (3 Replies)
Discussion started by: pinnacle
3 Replies

2. Shell Programming and Scripting

Lookup field values in two fixed format file in UNIX - not working

I have 2 fixed length files input#1 & input#2. I want to match the rows based on the value in position 37-50 in both files (pos 37-50 will have same value in both files). If any matching record is found then cut the value against company code & Invoice number from input file #1 (position 99 until... (3 Replies)
Discussion started by: Lingaraju
3 Replies

3. Shell Programming and Scripting

Splitting fixed length file using awk

Hi, I need to split a fixed length file of 160 characters based on value of a column. Example: ABC 456780001 DGDG SDFSF BCD 444440002 SSSS TTTTT ABC 777750003 HHHH UUUUU THH 888880001 FFFF LLLLLL HHH 999990002 GGGG OOOOO I need to split this file on basis of column from... (7 Replies)
Discussion started by: Neelkanth
7 Replies

4. Shell Programming and Scripting

how to read fixed length flat file....

Hi Gurus, Thanks in advance... I am new to writing shell scripting and help me out reading a flat file with fixed length. I have a fixed length flat file with storename(lenth 6) , emailaddress(lenth 15), location(10). There is NO delimiters in that file. Like the following str00001.txt... (2 Replies)
Discussion started by: willywilly
2 Replies

5. Shell Programming and Scripting

Fixed Format File

Hi to All, Would you please help me. I have a issue like in fixed format file there are 80 words should be there. but if file contains greater than 80 words then creates problem. so i have to trim file having more than 80 characters line through unix shell scripting. Please let me know how... (2 Replies)
Discussion started by: div_Neev
2 Replies

6. Shell Programming and Scripting

Awk - Working with fixed length files

OK I am somewhat new to UNIX programming please see what you can do to help. I have a flat file that is a fixed length file containing different records based on the 1st character of each line. The 1st number at the beginning of the line is the record number, in this case it's record #1. I... (3 Replies)
Discussion started by: ambroze
3 Replies

7. Shell Programming and Scripting

Read variables from line to fixed length

I would like to make a script to read three variables (no fixed length or position) from a line and write them into a file, with fixed length and right-justified in each column. The fixed text (text1-text4) prior to the thee variables and the variables themselves are originally separated by spaces... (3 Replies)
Discussion started by: SharkM
3 Replies

8. Shell Programming and Scripting

Awk with fixed length files

Hi Unix Champs, I want to awk on a fixed length file. Instead if the file was a delimited file, then I could have used -F and then could have easily done manipulation on the fields. How do i do the same in case of fixed length file? Thanks in Advance. Regards. (7 Replies)
Discussion started by: c2b2
7 Replies

9. Shell Programming and Scripting

fixed length fields in awk

I am trying to display df -h command out in proper format, how can I display each field of each record in a fixed length. (2 Replies)
Discussion started by: roopla
2 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