All strings within two special chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting All strings within two special chars
# 1  
Old 01-12-2013
All strings within two special chars

I have a file with multiple lines. From each line I want to get all strings that starts with '+' and ends with '/'. Then I want the strings to be separated by ' + '

Example input:
Code:
+$A$/NOUN+At/NSUFF_FEM_PL+K/CASE_INDEF_ACC

Sample output:
Code:
$A$ + At + K

# 2  
Old 01-12-2013
Try:
Code:
perl -nle 'print join " + ", map {s/[\+\/]//g;$_} /\+.*?\//g' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-12-2013
May need some polishing:
Code:
$ sed 's:$:+:;s:^+::;s:/[^+]*+: + :g;s: + $::' file
$A$ + At + K

# 4  
Old 01-12-2013
Thanks! That works. Except that for some lines, there isn't a + at the start.
So is there a way to insert a '+' on each line only when there isn't one?
Then your suggested code will work.
# 5  
Old 01-12-2013
Quote:
Originally Posted by Viernes
Thanks! That works. Except that for some lines, there isn't a + at the start.
So is there a way to insert a '+' on each line only when there isn't one?
Then your suggested code will work.
Your specification of what should happen when there are no + and/or / characters and when + and / characters don't appear as pairs resulted in different people trying to help making different assumptions. If you answer the following questions, we can probably provide something that will do what you want:
  1. If there is text before the 1st + with a terminating / on a line, should that text be skipped?
  2. If there is no + with a terminating /, should the line be:
    1. ignored completely,
    2. be replaced by an empty line, or
    3. be copied to the output without change.
  3. If there is a + between a + and a /, should:
    1. the 1st + start the field and any more +s before the / be printed as is, or
    2. should leading + signs be discarded as text that is not between a + and a /?
  4. If there is a + on a line with no / following it (after a + with a matching / has been found), should:
    1. the + and any text following it be discarded,
    2. the + be disarded but any following text be copied to the output, or
    3. should the remainder of the line be copied to the output as thought a trailing / appeared at the end of the line?
If the answer to question 1 is yes, 2 is C, 3 is A, and 4 is A, the following awk script should do what you want:
Code:
awk '{  while(match($0, "[+]([^/])*[/]") > 0) {
                o = (o != "" ? o " + " : "") substr($0, RSTART + 1, RLENGTH - 2)
                $0 = substr($0, RSTART + RLENGTH)
        }
        if(o != "") print o
        else print
        o=""    
}' input

As always with awk, if you are using a Solaris system, use /usr/xpg4/bin/awk or nawk, instead of awk.
When the file input contains:
Code:
+$A$/NOUN+At/NSUFF_FEM_PL+K/CASE_INDEF_ACC
before+$A$/NOUN+At/NSUFF_FEM_PL+K/CASE_INDEF_ACC
before+$A+B$/NOUN+At/NSUFF_FEM_PL+K/CASE_INDEF_ACC
this line has no terminating slash+data after plus
this line has no starting plus sign/after slash
+this whole line is a single field between a plus sign and a slash/
+++++++++++++++++++++++++++
+previous line was a string of plus signs/
///////////////////////////
+previous line was a string of slashes/
++++++++++//////////
+previous line was 10 plus signs followed by 10 slashes/
+D=A+B+C/

this script produces:
Code:
$A$ + At + K
$A$ + At + K
$A+B$ + At + K
this line has no terminating slash+data after plus
this line has no starting plus sign/after slash
this whole line is a single field between a plus sign and a slash
+++++++++++++++++++++++++++
previous line was a string of plus signs
///////////////////////////
previous line was a string of slashes
+++++++++
previous line was 10 plus signs followed by 10 slashes
D=A+B+C

# 6  
Old 01-13-2013
Yes you have a point. I apologise about this.

1. No, it shouldn't be skipped. So for
Code:
Al/DET+>ax/NOUN+

I want to get
Code:
Al + >ax

2. A. If there is no '+' nor '/', the line should be skipped. Although the file doesn't contain such a line.
3. Use the '+' as the delimiter in this case.
So for
Code:
Al+>ax/NOUN+
Al+>a+x/NOUN+

I want to get
Code:
Al + >ax
Al + >a + x

4. A
# 7  
Old 01-13-2013
Insert a plus at the start when there isn't one and the line contains at least one character:
Code:
sed 's/^[^+]/+&/' file


Last edited by Scrutinizer; 01-13-2013 at 02:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. UNIX for Dummies Questions & Answers

How to search for a string with special chars?

Hi guys, I am trying to find the following string in a file, but I always get pattern not found error, not sure what is missing here. Can you help please? I do a less to open the xrates.log and then do a /'="18"' in the file and tried various combinations to search the below string. String... (8 Replies)
Discussion started by: santokal
8 Replies

3. Shell Programming and Scripting

If condition matching with special chars

Hi, I have file #cat drivers.txt fcs0 fcs1 vscsi1 vscsi2 In this i need to check the availabality of "fcs" or "vscsi" alone not vscsi0,fcs1 I tried with "if condition" but it is not working. cat drivers.txt| while read ADAP do echo "Checking for $ADAP" if ;then echo "FC... (9 Replies)
Discussion started by: ksgnathan
9 Replies

4. Shell Programming and Scripting

print all between patterns with special chars

Hi, I'm having trouble with awk print all characters between 2 patterns. I tried more then one solution found on this forum but with no success. Probably my mistakes are due to the special characters "" and "]"in the search patterns. Well, have a log file like this: logfile.txt ... (3 Replies)
Discussion started by: ginolatino
3 Replies

5. UNIX for Dummies Questions & Answers

Remove Unicode/special chars from XML

Hi, We are receiving an XML file in Unix which has some special characters between tags like '^' etc <Tag> 1e^O7f%<2304e.$d8f57e8^Bf-&e.^Zh7/327e^O7 </Tag> We need to remove all special characters like ^ ones and also any '&' or '<' or '>' being sent within the start and close tags i.e.... (6 Replies)
Discussion started by: dsrookie7
6 Replies

6. UNIX for Dummies Questions & Answers

Strings with Special chars in IF condition

I was trying to run a code to check if a fax number is empty or not. for that, I've written the following code which is throwing an error. #!/bin/ksh fax= "999-999-9999" if ; then fax_no="000-000-0000" else fax_no=$fax fi echo $fax_no And I get the... (7 Replies)
Discussion started by: hooaamai
7 Replies

7. Shell Programming and Scripting

Special chars in sed variable

Hi, For years ive been using this script to do mass search & replaces on our websites. Its worked with all sorts of spaces, quotes, html or whatever with a little adjusting here and there. But I just cant get this pattern to work: #!/bin/bash OLDURL="document.write('<script... (2 Replies)
Discussion started by: mutex
2 Replies

8. Shell Programming and Scripting

special chars arrangement in code

here is my simple script to show process and owners except me: ps `-ef |grep xterm |grep -v aucar` | while read a1 a2 a3 a4 a5 a6 a7 a8 do echo KILL..\($a1\).. $a2 |more done how can I pass values from command "ps -ef |grep xterm|grep -v aucar" to ? because above command... (2 Replies)
Discussion started by: xramm
2 Replies

9. Shell Programming and Scripting

treating special chars

Hi, I need some advise on treating non printable chars over ascii value 126 Case 1 : On some fields in the text , I need to retiain then 'as-is' and load to a database.I understand it also depends on database codepage. but i just wanna know how do i ensure it do not change while loading... (1 Reply)
Discussion started by: braindrain
1 Replies

10. UNIX for Advanced & Expert Users

Supress special chars in vi

Hi, One of our application is producing log files. But if we open the log file in vi or less or view mode, it shows all the special characters in it. The 'cat' shows correctly but it shows only last page. If I do 'cat' <file_name> | more, then again it shows special characters. ... (1 Reply)
Discussion started by: divakarp
1 Replies
Login or Register to Ask a Question