awk print string with removing all spaces

Hi all,

I want to set 10 set of strings into a variable where:

  1. removing all spaces within each string
  2. change the delimiter from "|" to ","

Currently, I've the below script like this:

Table=`ten character strings with spaces in-between and each string with delimiter "|" | tr -d ' ' | awk -F'|' '{print $1","$2","$3","$4","$5","$6","$7","$8","$9","$10}'`

Today I've got an error 0403-029 There is not enough memory available now. while running the tr -d ' ' (delete space characters) so I suspect either I've used too many pipelines or the tr command (translate or delete) itself may eat up the memory.

I'd like to know if there is a way to remove all spaces within each string within the command:

awk -F'|' '{print $1","$2","$3","$4","$5","$6","$7","$8","$9","$10}'`

So as to use less pipeline commands thus reduce the memory consumption

Thanks in advance

Paul

Welcome to the forums.
Your input string seems to be confusing to me. Could you please provide some sample data which is seperated by "|" and provide expected output? ALso how do you reading those input? e.g. from file?

Hi Clx,

The input was come from a api command's output and the format as follows:

aaa1|bbbb1   | cc1   | dddd ddd1 |eee eee eee1|ff1|gg1|hh1|ii1|jj1
aaa2|bbbb2   | cc2   | dddd ddd2 |eee eee eee2|ff2|gg2|hh2|ii2|jj2
aaa3|bbbb3   | cc3   | dddd ddd3 |eee eee eee3|ff3|gg3|hh3|ii3|jj3

and I want to have the output from awk -F'|' '{print $1","$2","$3","$4","$5","$6","$7","$8","$9","$10}'` becomes:

aaa1,bbbb1,cc1,dddd ddd1,eee eee eee1,ff1,gg1,hh1,ii1,jj1
aaa2,bbbb2,cc2,dddd ddd2,eee eee eee2,ff2,gg2,hh2,ii2,jj2
aaa3,bbbb3,cc3,dddd ddd3,eee eee eee3,ff3,gg3,hh3,ii3,jj3

then finally set the output from awk to the variable Table (for some reasons that I don't want to use file so that's why I keep using pipe and then put all result records into a variable)

Paul

Please use code tags as required by forum rules!

That won't work. tr -d ' ' will remove ALL spaces, so those inside fields "eee eee" etc as well.
How about

awk '{gsub(/ *\| */,",")}1' file
aaa1,bbbb1,cc1,dddd ddd1,eee eee eee1,ff1,gg1,hh1,ii1,jj1
aaa2,bbbb2,cc2,dddd ddd2,eee eee eee2,ff2,gg2,hh2,ii2,jj2
aaa3,bbbb3,cc3,dddd ddd3,eee eee eee3,ff3,gg3,hh3,ii3,jj3

Hi RudiC,

Sorry that I made a mistake for the sample output, it is indeed to remove all spaces as follows:
and I want to have the output from awk -F'|' '{print $1","$2","$3","$4","$5","$6","$7","$8","$9","$10}'` becomes:

aaa1,bbbb1,cc1,ddddddd1,eeeeeeeee1,ff1,gg1,hh1,ii1,jj1
aaa2,bbbb2,cc2,ddddddd2,eeeeeeeee2,ff2,gg2,hh2,ii2,jj2
aaa3,bbbb3,cc3,ddddddd3,eeeeeeeee3,ff3,gg3,hh3,ii3,jj3

Sorry for any inconvenience caused

Paul

Try

awk '{gsub(/\|/,","); gsub(/ /,"")}1' file

, then, or

tr '|' ','  <file | tr -d ' '

The first one is roughly twice as fast with your sample data; this may become less on larger data files.

Hi RudiC,

So I can have the set of command become:

Table=`an api command that produces multiplues line where each line with ten character strings with spaces in-between and each string with delimiter "|" | awk '{gsub(/\|/,","); gsub(/ /,"")}1' file

So as to set the output from awk to the variable Table ?

Thanks in advance

Paul

How about just giving it a shot? There's at least one backtick missing...