Extract string between 2 special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract string between 2 special characters
# 1  
Old 01-07-2013
Extract string between 2 special characters

Hi,

I have a unix file with contents as below

Code:
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
Code:
QWERTY|HOW|YOU

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

This is on AIX6.1 (ksh)

Last edited by Scrutinizer; 01-07-2013 at 07:31 PM.. Reason: code tags
# 2  
Old 01-07-2013
try:
Code:
awk -F"[$][$]" '{for (i=2; i<=NF; i+=2) {o=o $i"|"}}END {sub("[|]$","",o); print o}' infile

# 3  
Old 01-07-2013
Code:
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

# 4  
Old 01-08-2013
Assume you have data in a file 'data.txt' you can achieve that using the below

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

Thanks
Sandeep
# 5  
Old 01-08-2013
Quote:
Originally Posted by Sandeep Lade
Assume you have data in a file 'data.txt' you can achieve that using the below

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

Thanks
Sandeep
Useless Use of Cat Award
# 6  
Old 01-08-2013
Hello itkamaraj,

is that a wrong solution ?

Thanks
Sandeep Lade
# 7  
Old 01-08-2013
This is a way to do it with perl:
Code:
perl -ne 'push(@a,/\$\$([^\$]*)\$\$/g); END { print join("|",@a) }' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract string between two special chracters

Hi Folks - I'm trying to extract the string between two special characters, the "-" and "." symbols. The string format is as such: _PBCS_URL_PRD=https://plan-a503777.pbcs.us6.ocloud.com _PBCS_URL_TST=https://pln-test-a503777.pbcs.us6.ocloud.comIn the above case, I need to extract "a503777".... (7 Replies)
Discussion started by: SIMMS7400
7 Replies

2. Shell Programming and Scripting

Help to replace the string with special characters

{"name":"alR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","description":"aLR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","json_class":"Chef::Role","default_attributes":{},"override_attributes":{"yoapp":{"jboss":"5.1.0","port":"2243","warname":"soap","datacenter":"alR","ip":"192.168.211.123","... (3 Replies)
Discussion started by: nikhil jain
3 Replies

3. UNIX for Dummies Questions & Answers

Extract string between two special characters

Hi, I have a file that looks something like >1-18*anc... (12 Replies)
Discussion started by: jyu429
12 Replies

4. Shell Programming and Scripting

How to remove some special characters in a string?

Hi, I have string like this ="Lookup Procedure" But i want the output like this Lookup Procedure =," should be removed. Please suggest me the solution. Regards, Madhuri (2 Replies)
Discussion started by: srimadhuri
2 Replies

5. Shell Programming and Scripting

Remove string between two special characters

Hi All, I have a variable like AVAIL="\ BACK:bkpstg:testdb3.iad.expertcity.com:backtest|\ #AUTH:authstg:testdb3.iad.expertcity.com:authiapd|\ TEST:authstg:testdb3.iad.expertcity.com:authiapd|\ " What I want to do here is that If a find # before any entry, remove the entire string... (5 Replies)
Discussion started by: engineermayur
5 Replies

6. Shell Programming and Scripting

Replacing string with special characters in shell

Hi, I am trying to replace a string in shell but it is not working correctly. @xcom.file@ needs to be replaced with tb137 Plz help.Thx. Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: manish72
4 Replies

7. Programming

C++ Special Characters in a String?

Hello. How can i put all of the special characters on my keyboard into a string in c++ ? I tried this but it doesn't work. string characters("~`!@#$%^&*()_-+=|\}]{ How can i accomplish this? Thanks in advance. (1 Reply)
Discussion started by: cbreiny
1 Replies

8. Shell Programming and Scripting

Remove special characters from string

Hi there, I'd like to write a script that removes any set of character from any string. The first argument would be the string, the second argument would be the characters to remove. For example: $ myscript "My name's Santiago. What's yours?" "atu" My nme's Snigo. Wh's yors? I wrote the... (11 Replies)
Discussion started by: chebarbudo
11 Replies

9. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies

10. Shell Programming and Scripting

Add string after another string with special characters

Hello everyone, I'm writing a script to add a string to an XML file, right after a specified string that only occurs once in the file. For testing purposes I created a file 'testfile' that looks like this: 1 2 3 4 5 6 6 7 8 9 And this is the script as far as I've managed: ... (2 Replies)
Discussion started by: heliode
2 Replies
Login or Register to Ask a Question