0

I have a file that is coming in tags "{}". Need to convert it into a pipe format.

Here is the sample:

{Id USXXXXXX} {Code USXXXXX} {Desc abcd 6.000% 5/24}

Expected result 1:

{Id|USXXXXXX} {Code|USXXXXX} {Desc|abcd 6.000% 5/24}

Expected result 2:

Id|USXXXXXX|Code|USXXXXX|Desc|abcd 6.000% 5/24
1

How will these work for you?

awk '{for(i=1; i<=NF; i++) {if ($i ~ /^{/) {printf("%s|", $i)} else {printf("%s ", $i)}} printf("\n")}' test.in

for expected result 1, and

awk '{for(i=1; i<=NF; i++) {if ($i ~ /^{/) {printf("%s|", $i)} else {printf("%s ", $i)}} printf("\n")}' test.in | sed -e 's#^{##' -e 's#} {#|#g' -e 's#} $##'

for expected result 2.

  • Glad they work for you. Please remember to like the answer, and mark it as an answer if you find it acceptable. Thanks. – Lewis M Oct 12 '18 at 14:15
0

Try also

awk -F"{" '{for (i=2; i<=NF; i++) sub (" ", "|", $i)}1' OFS="{" file
{Id|USXXXXXX} {Code|USXXXXX} {Desc|abcd 6.000% 5/24}

and

awk -F"{" '{gsub (/}( |$)/, ""); for (i=2; i<=NF; i++) sub (" ", "|", $i); sub (/\|/, "")}1' OFS="|" file
Id|USXXXXXX|Code|USXXXXX|Desc|abcd 6.000% 5/24
0

For result 1 below is the command

@praveen_2 ~]# echo "{Id USXXXXXX} {Code USXXXXX} {Desc abcd 6.000% 5/24}"| sed "s/{[A-Za-z]\{2,\}/&|/g"

output

{Id| USXXXXXX} {Code| USXXXXX} {Desc| abcd 6.000% 5/24}

For result2 below is the command

echo "{Id USXXXXXX} {Code USXXXXX} {Desc abcd 6.000% 5/24}"| sed "s/\}//g"| sed 's/ {/|/g'| sed 's/^{//g'| sed 's/ [a-zA-Z]\{2,\}/|&/g'

output

Id| USXXXXXX|Code| USXXXXX|Desc| abcd 6.000% 5/24

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.