popawu
February 25, 2011, 10:20am
1
Hi all,
I'm trying to split fields separated by multiple characters :
Here's the string :
"toto"||"ta|ta"||"titi"
Here's what I want :
"toto"||"ta|ta"
I tried several ways, but it seems that my delimiter || is not working :
echo "\"toto\"||\"ta|ta\"||\"titi\"" | awk 'BEGIN {FS="[||]+";}{print $1"||"$2}'
"toto"||"ta
It works when there's no | in field two :
echo "\"toto\"||\"tata\"||\"titi\"" | awk 'BEGIN {FS="[||]+";}{print $1"||"$2}'
"toto"||"tata"
I don't understand why only one | is considered as a delimiter as I specified a double |...
Any help will be much appreciated !
Mat
Here is one way of doing it without using awk.
Note the single quote around the string:
echo '"toto"||"ta|ta"||"titi"' | cut -d'|' -f1-4
1 Like
% cat infile
"toto"||"ta|ta"||"titi"
% awk -F'[|][|]' '{ print $1, $2 }' OFS='||' infile
"toto"||"ta|ta"
1 Like
popawu
February 25, 2011, 10:49am
4
Thx Shell_Life for this quick response.
Sorry for not being enough specific : I don't know the content of field2, and it may contain special characters. I'm just sure it does not contain "||", that's why I used it as a delimiter.
Your solution is great, but if I have another pipe in field two :
echo '"toto"||"ta|ti|a"||"titi"' | cut -d'|' -f1-4
"toto"||"ta|ti
end of field 2 is missing.
---------- Post updated at 10:49 AM ---------- Previous update was at 10:46 AM ----------
wow
I don't even have time to reply...
Thx radoulov, that was exactly what I was looking for.
(sorry for code tags !)
first replace the multi charecter with one spl charecter.
then use that as the delimiter.
echo '"toto"||"ta|ta"||"titi"' |sed "s/$val/,/g"
regards
rajesh