Extract string between 2 special characters

Hi,

I have a unix file with contents as below

Line1: ABC MNN X$$QWERTY$$ JKL

Line2: HELLO $$HOW$$ ARE $$YOU$$ DOING

i want to extract the string between $$ and $$ ie i want the output as QWERTY

HOW YOU

i want those strings seperated by some character say |

desired output is

QWERTY|HOW|YOU

i tried using sed, but couldn't figure it out. Any help is appreciated.

This is on AIX6.1 (ksh)

try:

awk -F"[$][$]" '{for (i=2; i<=NF; i+=2) {o=o $i"|"}}END {sub("[|]$","",o); print o}' infile
awk ' {
       for(i=1;i<=NF;i++) {
         if($i~/\$\$/) {
           gsub(/\$\$/,"",$i);
           op=sprintf ("%s|%s",op,$i);
         }
       }
      } END {
      sub(/\|/,"",op);
      printf ("%s\n", op);
}' file

Assume you have data in a file 'data.txt' you can achieve that using the below

cat data.txt | grep -o  '\$\$\([^$ ]*\)\$\$' | sed 's/\$//g'

Thanks
Sandeep

Useless Use of Cat Award

Hello itkamaraj,

is that a wrong solution ?

Thanks
Sandeep Lade

This is a way to do it with perl:

perl -ne 'push(@a,/\$\$([^\$]*)\$\$/g); END { print join("|",@a) }' file

grep,sed,awk ... commands can able to read the file by its own. so no need to use the cat command.

1 Like