awk/gawk to separate data

January 13, 2012 at 06:11:18
Specs: Macintosh
Hey guys,
I have a file like this

> BGR78-02 51614034.70S3300627.10E
> BGR78-02 80614122.30S3300655.60E
> BGR78-02 108614210.50S3300719.10E
> BGR78-02 136614258.10S3300750.90E
> BGR78-02 164614345.70S3300821.30E
> BGR78-02 192614433.60S3300850.20E
> BGR78-02 220614521.40S3300918.70E
> BGR78-02 248614609.00S3300949.20E
> BGR78-02 276614657.10S3301016.20E
> I want to separate as :
> BGR78-02 51 61 40 34.70 S 330 0 6 27.10 E
> ........
> in total ,11 columns in a new file
> How can I make it by awk or gawk?


See More: awk/gawk to separate data

Report •

#1
January 13, 2012 at 09:51:50
First, please don't crosspost.

Second, I see no way of doing this other than placing a space at positions 12, 14, 16, 21, 22, 25, 26, 27, and 32 in order to create 11 columns. That means parsing the line by character position.

Third, awk is not the best tool for doing that in my opinion. This is a Linux solution:

#!/bin/bash

while read line
do
  mypos=0
  len=${#line} # length of line
  myline=$(echo "$line"|tr ":NW" "BGR")
  len=${#myline} # length of myline
  while [[ $mypos -le $len ]]
  do
     # get the next char position
     mypos=$((mypos+1))
     # grab the character
     mychar=$(echo "$myline"| cut -c$mypos)
     # if statement is all on one line
     if [[ $mypos -eq 12 || $mypos -eq 14 || $mypos -eq 16 || $mypos -eq 21 || $mypos -eq 22 || $mypos -eq 25 || $mypos -eq 26 || $mypos -eq 27 || $mypos -eq 32 ]]
     then # print a space
         echo -n " "
     fi
     if [[ $mypos -eq $len ]]
     then # print the character
        echo "$mychar"
     else
        echo -n "$mychar"
     fi
  done
done < myfile.txt

Fourth, if you really insist on awk, you can use the awk internal substr function to parse the record.
I will do the first 3 columns for you, and leave the rest to you:

awk ' {
gsub(":NW","BGR")
col1=substr($0, 1, 11)
col2=substr($0, 12, 2)
col3=substr($0, 14, 2)
printf("%s %s %s\n", col1, col2, col3)
} ' myfile.txt
# end awk script

Here is how you use substr:

substr(s, p, n)

returns the suffix of s ($0 in this case in the entire line), starting at position p, for a length of n


Report •
Related Solutions


Ask Question