awk or sed help

Hi Help,

I hope you guys are doing good!

I have an input file which looks like

11 12 13 16 17 |$678.09 78 p|
11 12 13 16 17 |     8.09278 p|
11 12 13 16 17 |3678             |

I want the op file to look like

$678.09078p0008.09278p36780000000

i.e only to print the characters within |&|(within the pipes) and to replace all the blank space within pipe by 0.The number of chars within th pipe are constant and here it is 11.

Is it possible.?

Thanks a lot in advance

Cheers!!

Some like this?
One space " " is replaced by one 0
In your output you have multiple 0 , not sure why.

awk -F\| '{gsub(/ /,"0");print $2} END {print "\n"}' ORS= file
$678.090780p08.092780p36780
1 Like

Please use code tags as required by forum rules!

The number of chars within the pipe is not constant and it is not 11. Your output does not meet your requirements.

Try

awk -F\| '{gsub(/ /,"0",$2); printf "%s", $2} END {printf"\n"}' file
$678.090780p8.09278p3678

EDIT: Tried to interpret your desired output a bit more; still it doesn't fit what you require, but try:

awk -F\| '{$2=sprintf ("%-11s", $2); gsub(/ /,"0",$2); printf "%s", $2} END {printf"\n"}' file
$678.090780p08.092780p036780000000
1 Like

Thanks to both RudiC and Jotne for helping out!!!
Have a great day1!

Jotne and RudiC were confused because the original posting in this thread did not originally contain CODE tags. I have added CODE tags for you, but I agree with RudiC; the number of characters between the pipe symbols in your input lines is not a constant and is not 11 in any of your sample input lines. The number of characters between the pipe symbols is 12, 14, and 17 characters, respectively.

Replacing each space in your input with a "0" and adding a <newline> at the end produces the output:

$678.090780p000008.092780p36780000000000000

which is similar to, but certainly not the same as the output you said should be produced:

$678.09078p0008.09278p36780000000

The characters printed in red do not appear in what you say the output should be. I don't see a clear pattern to describe what spaces you want converted to zeros and what spaces you want to be deleted.