1

I have a fixed length matrix type file where each field is 20 characters long.

  1. How can I convert this to a space delimited file?

Also there are some missing values, which I would like to replace with a hyphen (-).

  1. How could I achieve this?

I will be using the output for awk operations.

Example input with 10 character field length file.

 head1     head2     head3
 a         b         c
           c          
 c                   a

Example output

head1 head2 head3
a b c
- c -
c - c
  • Each table element is only 1 character long? – jimmij Oct 22 '14 at 20:33
  • that may not be the case, this is just an example. – Hia Sen Oct 22 '14 at 20:42
  • not awk, but alternatively, tr will do the trick – zamnuts Oct 23 '14 at 1:35
4

Perl:

perl -ne '
    BEGIN {$width = 10}
    while (length) {
        ($word = substr($_,0,$width)) =~ s/^\s+|\s+$//g; # trim whitespace
        $word ||= "-";
        print $word, " "; 
        substr($_,0,$width) = "";
    }
    print "\n";
' file
2

That worked for me:

sed 's/^ //g' input | tr -s "[:blank:]" "," | \
awk -F, '!$1{printf "- "} $1{printf "%s ", $1} \
  !$2{printf "- "} $2{printf "%s ", $2} \
  !$3{print "-"} $3{print $3}'

Explanation:

  • sed 's/^ //g' input removes a leading space if present
  • tr -s "[:blank:]" ",": replaces all spaces with comma
  • awk ...: uses the delimiter , and prints the values or - if the value is empty.
  • 1
    That will break if there are 2 or more whitespace-separated words within the fixed-width field. – glenn jackman Oct 22 '14 at 23:22
1

Awk way

awk '{for(x=0;x<length($0);x+=10)y=y" "((z=substr($0,x,10))~/[[:graph:]]/?z:"-")
      gsub(/ +/," ",y);print y;y=""}' file

If they can have spaces in the fields

awk '{for(x=0;x<length($0);x+=10){z=gensub(/(^ +| +$)/,"", "g",substr($0,x,10))
      y=y" "(z?z:"-")}print y;y=""}' file
0

If you have gawk then you can use FIELDWIDTHS variable which tells gawk how to split input with fixed, columnar boundaries. It is an experimental feature. Assigning to FIELDWIDTHS overrides the use of FS for field splitting.

Here is example of usage,

gawk 'BEGIN{
             FIELDWIDTHS = "10 10 10"
           }
           {
             s = q = "" 
             for(i=1;i<=NF;i++)
             { 
               s = $i
               gsub(/ +/,"",s)
               s = (length(s) )? s : "-"
               q = sprintf("%s%s%s",q,(length(q)?OFS:""),s)  
             } 
               print q
           }
      ' file
  • thanks Akshay, I have thousands of columns to work with and not just 3, can that be adapted? – Hia Sen Oct 23 '14 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.