awk with multiple character delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with multiple character delimiter
# 1  
Old 02-25-2011
awk with multiple character delimiter

Hi all,

I'm trying to split fields separated by multiple characters :

Here's the string :
Code:
"toto"||"ta|ta"||"titi"

Here's what I want :
Code:
"toto"||"ta|ta"

I tried several ways, but it seems that my delimiter || is not working :

Code:
echo "\"toto\"||\"ta|ta\"||\"titi\"" | awk 'BEGIN {FS="[||]+";}{print $1"||"$2}'

"toto"||"ta

It works when there's no | in field two :

Code:
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

Last edited by radoulov; 02-25-2011 at 11:37 AM.. Reason: Code tags, please!
# 2  
Old 02-25-2011
Here is one way of doing it without using awk.
Note the single quote around the string:
Code:
echo '"toto"||"ta|ta"||"titi"' | cut -d'|' -f1-4

This User Gave Thanks to Shell_Life For This Post:
# 3  
Old 02-25-2011
Code:
% cat infile
"toto"||"ta|ta"||"titi"
% awk -F'[|][|]' '{ print $1, $2 }' OFS='||' infile
"toto"||"ta|ta"

This User Gave Thanks to radoulov For This Post:
# 4  
Old 02-25-2011
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 :

Code:
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.
Smilie

(sorry for code tags !)

Last edited by popawu; 02-25-2011 at 11:54 AM..
# 5  
Old 02-25-2011
first replace the multi charecter with one spl charecter.
then use that as the delimiter.
Code:
echo '"toto"||"ta|ta"||"titi"' |sed "s/$val/,/g"

regards
rajesh

Last edited by radoulov; 02-25-2011 at 12:40 PM.. Reason: Code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. UNIX for Dummies Questions & Answers

How to use multiple delimiter?

How to split 2:6..5 in to separate columns (7 Replies)
Discussion started by: Nadela
7 Replies

3. UNIX for Dummies Questions & Answers

How to replace particular occurrence of character in between a delimiter?

Hi, Hi, I have a file with following format 1|" "text " around " |" fire "guest"|" " 2| "xyz"" | "no guest"|"3" 3| """ test3""| "one" guest"|"4" My requirement is to replace all occurrences of " to ' which are occurring between |" "|delimiter so my output should look like this 1|"... (3 Replies)
Discussion started by: H_bansal
3 Replies

4. Shell Programming and Scripting

Cutting a string using more than one character as delimiter

Hi , I have a set of files in a folder which i need to cut in to two parts.... Sample files touch AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch AE_JUNFOR_2014_YTD_2013-05-30-03-30-02.TXT touch temp_AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT touch... (4 Replies)
Discussion started by: chillblue
4 Replies

5. Shell Programming and Scripting

Split file into multiple files using delimiter

Hi, I have a file which has many URLs delimited by space. Now i want them to move to separate files each one holding 10 URLs per file. http://3276.e-printphoto.co.uk/guardian http://abdera.apache.org/ http://abdera.apache.org/docs/api/index.html I have used the below code to arrange... (6 Replies)
Discussion started by: vel4ever
6 Replies

6. Shell Programming and Scripting

Pasting multiple files using awk with delimiter

hi, i want to PASTE two files, with a delimiter in between, using awk and pipe the output to another file. i am able to achive the reqirement using PASTE command. but it has a limitation of length till 511 bytes. Example: ------- File1: ---- sam micheal file2: ---- bosco... (11 Replies)
Discussion started by: mohammedsadath
11 Replies

7. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

8. Shell Programming and Scripting

php setcookie multiple values with delimiter

Hi all, setting a cookie multiple values with delimiter $value = 'TM=1276245099:LM=1276245099'; // etc etc setcookie('unix',"$value"); This generates %3ATM%3D1276245099%3ALM%3D1276245099 I want the data as it is TM=1276245099:LM=1276245099 Any suggestions? (0 Replies)
Discussion started by: ./hari.sh
0 Replies

9. UNIX for Dummies Questions & Answers

multi character delimiter

Hi All I have to encrypt files and add a suffix ".gpg_<key used>" to it For example if the file name is test.dat after encryption the file name will be test.dat.gpg_X005 (here X005 is the key). I want to decrypt the file later using that key and the original file name.both of them... (7 Replies)
Discussion started by: dr46014
7 Replies

10. UNIX for Advanced & Expert Users

How can I use double character delimiter in the cut command

Hi All, Can the cut command have double character delimiter? If yes, how can we use it. if my data file contains : apple || mango || grapes i used cut -f1 -d"||" filename but got an error. Plz help.... Thanks. (1 Reply)
Discussion started by: AshishK
1 Replies
Login or Register to Ask a Question