Sorting and extracting

I would like to know aside from while or for loop statement.
how can i extract i file like this:

File:

name1$%<name1#first>; name2$%<name2#second>; name3$%<name3#third>

Output:

name1#first
name2#second
name3#third

Thanks

sed 's/[^<]*<\([^>]*\)>;\? \?/\1\n/g' file
2 Likes

thanks a lot..

---------- Post updated at 11:53 PM ---------- Previous update was at 11:52 PM ----------

care to explain ? the syntax?

---------- Post updated 10-09-12 at 12:05 AM ---------- Previous update was 10-08-12 at 11:53 PM ----------

how about this, put exclamation before the number sign?

OUTPUT:

name1!#first
name2!#second
name3!#third
1 Like
sed 's/[^<]*<\([^>]*\)>;\? \?/\1\n/g' file

can anyone explain the above?

with awk.....

awk -F "[<>]" '{ for(i=2;i<=NF;i+=2){sub("#","!#",$i);print $i}}' file
1) [^<]*     --> 0 or more occurrences of a character which is not a <
2) <         --> literal <
3) \([^>]*\) --> 0 or more occurrences of a character which is not a >
4) ;\?       --> optional ; character (0 or 1 occurrence)
5)  \?       --> optional space character (0 or 1 occurrence)
6) \1        --> string matched by the pattern in 3 (\( and \) are used to save the string matched by the pattern)
7) \n        --> newline

But, this might not work with all sed implementations. If you want something which works on most (if not all) implementations, use a slight modification of the above sed command:

sed 's/[^<]*<\([^>]*\)>;\{0,1\} \{0,1\}/\1\
/g' file
1 Like

in perl