Remove pipe(|) symbol in except the ones which are enclosed in double quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove pipe(|) symbol in except the ones which are enclosed in double quotes
# 1  
Old 06-24-2015
Remove pipe(|) symbol in except the ones which are enclosed in double quotes

I have file with are delimited by pipe(|) symbol, I wanted those to be removed except the ones which are enclosed in double quotes.

If your quote file is:
Code:
|Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|"

It should return:
Code:
Life is Beautiful"|"Indeed life is beautiful too"|"But unix is fun is not "|"


Last edited by Don Cragun; 06-24-2015 at 07:02 PM.. Reason: Add CODE tags,
# 2  
Old 06-24-2015
Is this a homework assignment?

What have you tried?

Are you just trying to keep pipe symbols in the three character sequence "|" or are you trying to keep any pipe symbol between paired double quotes?
# 3  
Old 06-25-2015
I am trying to keep the pipe symbol between the quotes

I have tried these, still not getting the expected results.

sed -e ':a;s/^\(\(\(["]\)[^\3]*\3\|[^"]*\)*\)|/\1/;ta' data_file_name

Code:
awk '{  for (i = 0; ++i <= NF;) if (i != NF) {printf "%s",$i} else {printf "%s\n",$i}}' FPAT='("[^"]+")|([^|]+)' data_file_name

# 4  
Old 06-25-2015
Would that work?
Code:
sed -e 's/"|"/@@/g; s/|//g; s/@@/"|"/g' data_file_name

# 5  
Old 06-25-2015
Nope does not
# 6  
Old 06-25-2015
Code:
$sed 's/|\([[:alnum:]]\)/\1/g;s/\([[:alnum:]]\)|/\1/g' a
Life is Beautiful"|"Indeed life is beautiful too"|"But unix is fun is not"|"
$cat a
|Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|"
$

# 7  
Old 06-25-2015
Try:
Code:
awk '{for(i=1; i<=NF; i+=2) gsub(/\|/,x,$i)}1' FS=\" OFS=\" file

or

Code:
awk 'NR%2{gsub(/\|/,x)}1' RS=\" ORS=\" file


Last edited by Scrutinizer; 06-25-2015 at 02:10 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove pipe(|) symbol ina file, except the ones which are enclosed in double quotes

I have file with are delimited by pipe(|) symbol, I wanted those to be removed except the ones which are enclosed in double quotes. If your quote file is: |Life is |Beautiful"|"Indeed life |is beautiful too|"|"But unix is fun| is not"|" It should return: Life is Beautiful"|"Indeed life is... (1 Reply)
Discussion started by: Sathyapts
1 Replies

2. Shell Programming and Scripting

How to delete the commas in a .CSV file that are enclosed in a string with double quotes?

Okay, I would like to delete all the commas in a .CSV file (TEST.CSV) or at least substitute them with empty space, that are enclosed in double quote. Please see the sample file as below: column 1,column 2,column 3,column 4,column 5,column 6,column 7,column 8,column 9,column 10... (8 Replies)
Discussion started by: dhruuv369
8 Replies

3. Shell Programming and Scripting

Trying to remove double quotes

Hi, I am little new to forum and new on unix side. I have a small issue below: I am reading a file that has 5 columns something like below. col1,col2,col3,col4,col5 Some records are having double quoted values something like below: "value1","value2","value3","value4","value5" I need... (8 Replies)
Discussion started by: Saanvi1
8 Replies

4. Shell Programming and Scripting

Remove whitespace after pipe symbol but not inside words

I have a file that looks like this: 102| #2 X 1/4-INCH| 30188| EA| FTW| A| NOT SERIAL TRACKING| NOT LOT TRACKING| TRUE| #2 X 1/4-INCH 102| #2 X 1/4-INCH| 30188| EA| VPS| A| NOT SERIAL TRACKING| NOT LOT TRACKING| TRUE| #2 X 1/4-INCH 102| #6 X 1/2"| ... (2 Replies)
Discussion started by: djehresmann
2 Replies

5. Shell Programming and Scripting

Convert csv to pipe delimited except the ones in double quotes

I have a csv data file : A,B,C,D,"A,B",E,"GG,H" E,F,G,H,I,J,"S,P" I need to replace all "," with "|" except the ones between double quotes i.e A|B|C|D|"A,B"|E|"GG,H" E|F|G|H|I|J|"S,P" CAn someone assist? (8 Replies)
Discussion started by: Shivdatta
8 Replies

6. Shell Programming and Scripting

How to remove characters enclosed in single quotes?

How to remove characters enclosed in single quotes? My data is something like this (03/22/2011 08:17:26.650) : ( -> '1' -> '1-1-3' -> '6' -> '1' -> 'SALMOR58BB4' aaaaa bbbbbb ccccc ((dddd)) I want the output to be (03/22/2011 08:17:26.650) : ( -> -> -> -> -> aaaaa... (2 Replies)
Discussion started by: rdhanek
2 Replies

7. Shell Programming and Scripting

removing extra double quotes between pipe dilimeter

I have a flat file sample like this - "COURSE"|"ddddd " " dddd"|"sssddd sdsdsdsdx" dddddddd ffffff "aaaaa" dddddddd ffffff sdsdsd"|"xxxxxxx"| "COURSE"|"ffff " " bbbb"|"lllll"| The delimiter is pipe character (|) and the text are enclosed in double quotes... (5 Replies)
Discussion started by: vishalzone
5 Replies

8. Shell Programming and Scripting

To Replace comma with Pipe inside double quotes

Hi, I have a requirement to replace the comma's inside the double quotes. The comma's inside the double quotes will get changed dynamically. Input Record: "Washington, DC,Prabhu,aju",New York Output Record: "Washington| DC|Prabhu|aju",New York I tried with the below command but it... (3 Replies)
Discussion started by: prabhutkl
3 Replies

9. Shell Programming and Scripting

remove verticalbar or pipe symbol

hi guys i have 6000 rows column the text in the column has the symbol vertical bar |. i tried some of the commands to remove it but none of the commands are reconzng this symbol. would u plz help to remove this symbol from the text with any kind of unix command u r help would be appreciated ... (9 Replies)
Discussion started by: bogu0001
9 Replies

10. UNIX for Advanced & Expert Users

How to remove a character which is enclosed in Double quotes

I want to remove the comma which is present within the double quoted string. All other commas which is present outside double quotes should be present. Input : a,b,"cc,dd,ee",f,ii,"jj,kk",mmm output : a,b,"ccddee",f,ii,"jjkk",mmm (3 Replies)
Discussion started by: mohan_tuty
3 Replies
Login or Register to Ask a Question