Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting a large dataset

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I have a series of dataset, with same kind of pattern. Here is an example dataset:
"A","B","C","D","E"
"1","2","3","4",""
"6","7","","9","10"

My final output should be something like:
A B C D E
1 2 3 4
6 7 9 10

Any help appreaciated.
Thanks.
 
sed 's/""/ /g;s/"\(.*\)"/\1/g;s/,/ /g'


not tested.


:) guggach
 
Try something like this:
sed 's!""!" "!g;s!^"!!;s!","! !g;s!"$!!' input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Instead of sed, if the sceipt is in awk that would be helpful. I have access to awk95.
Thanks.
 
hill007,

This should work OK:

awk '{gsub(/,/,"");gsub(/"/," ");print}' filename

Hope this helps.

John
 
for this kind of string manipulation (NO formatting,
NO computing, JUST replacements) the appropriate tool are:
SED, VI, EX. sure awk, perl, c ... can also do it, but you
need a lot more efforts for the same result.
why you insist using AWK ?


:) guggach
 
The conversion of:
sed 's!""!" "!g;s!^"!!;s!","! !g;s!"$!!' input > output
To awk:[tt]
awk '{
gsub(/""/,"\" \"") # s!""!" "!g
sub(/^"/,"") # s!^"!!
gsub(/","/," ") # s!","! !g
sub(/"$/,"") # s!"$!!
}' input > output[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
This works by setting the field separator...

Code:
[B]BEGIN[/B] [COLOR=#4444FF][B]{[/B][/color]
  [B]FS[/B] = [COLOR=#008000]"([COLOR=#008000]\042[/color],[COLOR=#008000]\042[/color]|^[COLOR=#008000]\042[/color]|[COLOR=#008000]\042[/color]$)"[/color];
[COLOR=#4444FF][B]}[/B][/color]
[COLOR=#4444FF][B]{[/B][/color]
   [B]for[/B] [COLOR=#4444FF][B]([/B][/color]x=2; x<[B]NF[/B]; x++[COLOR=#4444FF][B])[/B][/color] [COLOR=#4444FF][B]{[/B][/color]
      [COLOR=#a52a2a][B]printf[/B][/color] [COLOR=#008000]"%-10s"[/color], $x;
   [COLOR=#4444FF][B]}[/B][/color]
   [COLOR=#a52a2a][B]printf[/B][/color] [COLOR=#008000]"[COLOR=#008000]\n[/color]"[/color];
[COLOR=#4444FF][B]}[/B][/color]

PS: guggach, not all awk users are on UNIX.
 
This is solution with comma as field separator:

Code:
BEGIN { FS = "," }

{ 
    for (i = 1; i <= NF; i++) {
        l = length($i)
        oStr = substr($i, 2, l - 2)
        if ($i ~ /""/)
            oStr = " "
        printf "%s ", oStr
    }
    printf "\n"
}

It's a variation of Ygor's brilliant solution.

KP.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top