Replacing a string with its substring


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a string with its substring
# 1  
Old 08-20-2011
Replacing a string with its substring

Hi All,

Below is some sample content of my input file:

There are many types and traditions of anarchism, some of which are [[mutually exclusive]]. Strains of anarchism have been divided into the categories of [[social anarchism|social]] and [[individualist anarchism]] or similar dual classifications. Anarchism is often considered to be a radical [[left-wing]] ideology, and much of [[anarchist economics]] and [[anarchist law|anarchist legal philosophy]] reflect [[anti-statism|anti-statist]] interpretations of [[anarcho-communism|communism]], [[collectivist anarchism|collectivism]], [[anarcho-syndicalism|syndicalism]] or [[participatory economics]].

For the above content, if the square bracket [[mutually exclusive]] doesnt contain the delimiter '|',the substring inside the brackets, mutually exclusive, should replace the whole pattern [[mutually exclusive]].

If the square bracket contain strings separated with '|' delimiter [[social anarchism|social]] , the substring after the final delimiter in that pattern, social, should replace the whole pattern [[social anarchism|social]].

I believe this would be possible with sed and awk commands. I tried it. As am not that much conversant in unix, i could not achieve this.

Any help is appreciated. Also please recommend some useful sites/books to learn sed,awk and other text processing commands.

Thanks
Satheesh
# 2  
Old 08-20-2011
Try:
Code:
perl -pe 's/\[\[[^\]]+\|([^\]]+)\]\]/\1/g;s/\[\[([^\]]+)\]\]/\1/g' input

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 08-20-2011
Thats Great Bartus. it is working. Do u suggest perl over shell scripts for text processing.

If so, please recommend me some sites to learn perl.

Regards
Satheesh
# 4  
Old 08-20-2011
To learn Perl you need this book: Learning Perl, Third Edition - O'Reilly Media
# 5  
Old 08-21-2011
Thank you Bartus Smilie
# 6  
Old 08-22-2011
Alternatively, a single regex could be used with the substitution operator like so -

Code:
$
$
$ cat f8
There are many types and traditions of anarchism, some of which are [[mutually exclusive]].
Strains of anarchism have been divided into the categories of [[social anarchism|social]]
and [[individualist anarchism]] or similar dual classifications. Anarchism is often
considered to be a radical [[left-wing]] ideology, and much of [[anarchist economics]]
and [[anarchist law|anarchist legal philosophy]] reflect [[anti-statism|anti-statist]]
interpretations of [[anarcho-communism|communism]], [[collectivist anarchism|collectivism]],
[[anarcho-syndicalism|syndicalism]] or [[participatory economics]].
$
$
$
$ perl -plne 's/\[\[[^|]*?\|*([^|]*?)\]\]/$1/g' f8
There are many types and traditions of anarchism, some of which are mutually exclusive.
Strains of anarchism have been divided into the categories of social
and individualist anarchism or similar dual classifications. Anarchism is often
considered to be a radical left-wing ideology, and much of anarchist economics
and anarchist legal philosophy reflect anti-statist
interpretations of communism, collectivism,
syndicalism or participatory economics.
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 08-22-2011
Thank you very much tyler Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

sed - replacing a substring containing a hyphen

I'm attempting to replace a substring that contains a hyphen and not having much success, can anyone point out where i'm going wrong or suggest an alternative. # echo /var/lib/libvirt/images/vm888b-clone.qcow | sed -e 's|vm888-clone|qaz|g' /var/lib/libvirt/images/vm888b-clone.qcow (1 Reply)
Discussion started by: squrcles
1 Replies

3. Shell Programming and Scripting

Date substring from a string

Hi, I have 2 statements in a file a.sh start time is Fri Jan 9 17:17:33 CST 2015 a.sh end time is Fri Jan 9 17:47:33 CST 2015 I am required to get only the time out of it. like 17:17:33 & 17:47:33 PLs suggest (21 Replies)
Discussion started by: usrrenny
21 Replies

4. Shell Programming and Scripting

Remove a substring from string

Good morning friends, how can i remove a string with linux scripting from a file? In specific i want to remove from a file all the tweet names and links eg @aerta and links such as http://dst.co/pIiu3i9c Thanx!!! (4 Replies)
Discussion started by: paladinaeon
4 Replies

5. Shell Programming and Scripting

How to extract a substring from a string

Hi, I have an input string say for example: ABC,DEF,IJK,LMN,...,XYZ The above string is comma delimited. Now I have to extract the last part after the comma i.e. XYZ. :b: (3 Replies)
Discussion started by: bghosh
3 Replies

6. Shell Programming and Scripting

Help with string and substring also I/O

#!/bin/sh PRINTF=/usr/bin/printf PASSWD=/etc/passwd $PRINTF "Enter a UserID\n" read USERID if ; then $PRINTF "$USERID does not exist, please contact IT service\n" exit 1 fi USERHOME=`grep "^$USERID:" $PASSWD | awk -F : '{print $6}'` USERSHELL=`grep "^$USERID:"... (1 Reply)
Discussion started by: ikeQ
1 Replies

7. Shell Programming and Scripting

get substring from string

Hi All, Problem Description: XML_REP_REQUEST=`CONCSUB "$LOGIN" "SQLAP" "$RESP_NAME" "$USRNM" WAIT="Y" "CONCURRENT" "APPLICATION_SHORT_NAME" "CP_SHORT_NAME"` echo Report Request: $XML_REP_REQUEST --to print value in log file While execution the value of 'XML_REP_REQUEST' is 'Prozess... (5 Replies)
Discussion started by: suman.g
5 Replies

8. UNIX for Dummies Questions & Answers

How to get the substring from the string

Hi All, Can anybody help me to get the substring from the given string. (3 Replies)
Discussion started by: Anshu
3 Replies

9. Shell Programming and Scripting

getting a substring from a string

hi all, I am trying to extract SUBSTRINGS out of a string using ksh. The string is "SAPR3K.FD0.FA.TJ.B0010.T050302" I tried using a= `expr substr $stringZ 1 2` which is giving me a syntax error, donno why?? any ideas why its not working?? I also tried echo "welcome" | awk '{... (3 Replies)
Discussion started by: maradona
3 Replies

10. Programming

can i get a substring from a string?

for example, the string a is "abcdefg", can i get a substring "bcd" (from ato a) from string a? thank you (4 Replies)
Discussion started by: dell9
4 Replies
Login or Register to Ask a Question