Divide up lines with string delimiter
Hello,
I have lines in the following format: text1^~^text2^~^text3 where the delimiter is ^~^ How can I divide it up to text1, text2, text3? Thanks! |
Assuming you have no extraneous "^" characters You can do it with awk:
Try: echo text1^~^text2^~^text3 |awk -F^ '{print $1,$3,$5}' The above will result in: text1 text2 text3 The "-F" tells it to set the field delimiter to "^". Since your pattern has 2 of these between each field you just print every 2nd field. The "," in the print statement tells awk to separate the output with spaces. You can leave out the "," if you want "text1text2text3" instead. |
Thanks for fast answer!
Unfortunately the text can contain the ^ and the ~ characters too. That's why I want to use the complete ^~^ as delimiter. |
Ugh - you're forcing me to think of proper "sed" syntax:
The following will work: echo text1^~^text2^~^text3 | sed "s/\^~^/ /g" This would result in: text1 text2 text3 For "sed" The "s" (substitute) the string between the first two "/" with the one between the last two (which is just a space). The "g" (global) says to do it every time it finds the pattern as otherwise it would only do it the first time. You need the "\" before the first "^" because in regular expressions "^" has the special meaning of "first character in the line and the "\" escapes this meaning so it sees it as a literal rather than special. Actually sed is a fairly powerful tool - being lazy I only use it when I need to do so and only think about its syntax then. |
All times are GMT -5. The time now is 11:15 PM. |