how to insert a comma using either sed or awk 
Author Message
 how to insert a comma using either sed or awk

I have a data file with about 20 fields or so per line and some of the
fields are descriptive and are therefore made up of a few words.  My problem
is that if I substitute a blank space with a comma, it changes the structure
of the line.  Each field is a fixed length, i.e, field 1 is 14 characters
long, field 2 is 10 characters long and so on.

Going by the above example, is there a way to substitute or insert a space
in columns 15 & 26,etc... with a comma?

Thanks



Tue, 13 Jan 2004 08:37:10 GMT  
 how to insert a comma using either sed or awk

Quote:

> I have a data file with about 20 fields or so per line and some of the
> fields are descriptive and are therefore made up of a few words.  My problem
> is that if I substitute a blank space with a comma, it changes the structure
> of the line.  Each field is a fixed length, i.e, field 1 is 14 characters
> long, field 2 is 10 characters long and so on.

> Going by the above example, is there a way to substitute or insert a space
> in columns 15 & 26,etc... with a comma?

If you simply want to insert commas at fixed positions it is easy,
with sed:

sed -e 's/./&,/14' -e 's/./&,/25' ...

or with awk:

awk '{ print substr($0,1,14)","substr($0,15,24)"," ... }'

If you want to condense arbitrary number of spaces between the fields
into one (and the comma), like this:

12345     John Smith       Home
67890     Jane Doe         Somewhere

into

12345, John Smith, Home
67890, Jane Doe, Somewhere

it gets more interesting. With sed it's still easy: after the
above, add -e 's/  *,/, /g' and that's it.

With awk it can be done like this:

awk '{ res=substr($0,1,14)","substr($0,15,24)"," ...
       gsub(" *,"," ,"); print res }'

Lots of other ways are also possible as well.

--
Tapani Tarvainen



Tue, 13 Jan 2004 16:47:38 GMT  
 how to insert a comma using either sed or awk

Quote:

>I have a data file with about 20 fields or so per line and some of the
>fields are descriptive and are therefore made up of a few words.  My
problem
>is that if I substitute a blank space with a comma, it changes the
structure
>of the line.  Each field is a fixed length, i.e, field 1 is 14 characters
>long, field 2 is 10 characters long and so on.

>Going by the above example, is there a way to substitute or insert a space
>in columns 15 & 26,etc... with a comma?

To insert a comma at the starting position of each field after the first
field, you could use an approach like this.

BEGIN {
    # sample - replace with YOUR field widths in this string
    fws = "14 10 13 14 15 16 17 18 19 20"
    fw[0] = split(fws, fw)
    fs = ", "  # new field separator string - modify as desired
    fsl = length(fs)

Quote:
}

{
    s = $0

    for (p = !(i = 1); i < fw[0]; ++i) {
        p += fw[i]
        s = substr(s, 1, p) fs substr(s, p + 1)
        p += fsl
    }

    print s

Quote:
}



Wed, 14 Jan 2004 04:04:37 GMT  
 how to insert a comma using either sed or awk

Quote:

>I have a data file with about 20 fields or so per line and some of the
>fields are descriptive and are therefore made up of a few words.  My problem
>is that if I substitute a blank space with a comma, it changes the structure
>of the line.  Each field is a fixed length, i.e, field 1 is 14 characters
>long, field 2 is 10 characters long and so on.

>Going by the above example, is there a way to substitute or insert a space
>in columns 15 & 26,etc... with a comma?

Here's a neat solution that works in GAWK, at least as of version 3.0.6 and
beyond (Note that FIELDWIDTHS was introduced in a version prior to 3.0.6,
but was buggy then):

BEGIN { OFS="," ; FIELDWIDTHS = "14 10 ..." }
{ $1=$1;print }         # Or just: $1=$1 -> for the terse & perverse (*)

(*) This could actually be a feature if your input contains garbage
(e.g., blank lines), that *should* be skipped.



Wed, 14 Jan 2004 04:37:18 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How to handle comma in the fields winthin a csv file using awk

2. Extracting hyphenated words using sed/awk

3. Using awk And/Or sed To Edit Fourth Field Of File

4. join a range of lines using awk/sed

5. extracting top 100 data using awk or sed/perl

6. Q: Directory highlighting using sed/awk?

7. deleting records in a file using AWK, SED, or kshell

8. deleting records using AWK, SED, etc.

9. Question: How to remove END OF LINE using AWK or SED

10. Using awk & sed from tcl/tk

11. Insert/update on files using awk

12. Sed: Inserting file in another file

 

 
Powered by phpBB® Forum Software