Stripping double quotes from front and end of a line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Stripping double quotes from front and end of a line
# 1  
Old 09-14-2012
Stripping double quotes from front and end of a line

I have a file and some records may contain double quotes at beginning and at end of line. So how do I strip them?

For Example, file is somethings like this

Code:
Field1;Field2;Field3
01;'Test';'Test Field3'
"01;'This is 2nd field';This is 3rd field' "

Desired Output is:

Code:
Field1;Field2;Field3
01;'Test';'Test Field3'
01;'This is 2nd field';This is 3rd field'

Thanks

Last edited by Scrutinizer; 09-14-2012 at 08:21 PM.. Reason: code tags
# 2  
Old 09-14-2012
No, this isn't what you want. The double quotes are there in the last line because the 3rd field in that line contains an unmatched single quote. Before you strip the double quotes you need to look at the source of your input data and fix whatever is causing the mismatched quotes to appear in your file.
# 3  
Old 09-14-2012
We are getting file from another team, so don't have control over the file.
# 4  
Old 09-15-2012
If you want to remove double quotes present at the start of the line and at the end of the line..

Code:
sed -e 's/^"//g' -e 's/"$//g' file

# 5  
Old 09-15-2012
The g-flags (global) are unnecessary here, since there is only one beginning and one end per line...
Code:
sed 's/^"//; s/"$//' file

# 6  
Old 09-15-2012
Quote:
Originally Posted by vx04
We are getting file from another team, so don't have control over the file.
OK. But, be VERY careful in handling the fields you get from splitting lines in this file. Unmatched quotes have a tendency to cause cause lots of problems in scripts written by people who don't meticulously use quotes appropriately.

Note that the sed scripts that pamu and Scrutinizer have supplied will remove any double quotes at the start of any line and at the end of any line even if they don't form a matched pair. If you only want to remove matched pairs (which is what you requested), you probably want something like:
Code:
sed 's/^"\(.*\)"$/\1/' file

instead. Even then, if you have an input line like:
Code:
"01";'This is 2nd field';"This is 3rd field'"

you will destroy matched quotes leaving you with unmatched double quotes in the 1st and last fields and well as the unmatched single quote in the last field.

I wish you luck. Smilie
# 7  
Old 09-15-2012
Further to DC's remark, if you want to avoid replacing the double quotes when there are other double quotes present in the line than this might work:
Code:
sed 's/^"\([^"]*\)"$/\1/' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Double quotes within double quotes in a column with space while loading a CSV file

Hi All, I'm unable to load the data using sql loader where there are double quotes within the double quotes As these are optionally enclosed by double quotes. Sample Data : "221100",138.00,"D","0019/1477","44012075","49938","49938/15043000","Television - 22" Refurbished - Airwave","Supply... (6 Replies)
Discussion started by: mlavanya
6 Replies

2. Shell Programming and Scripting

Adding double quotes at the end of string

My input is this: Inputfile = file.txt needs to change to, Inputfile = file.txt" I have tried using: Inputfile = `echo ${Inputfile}"' doesn't work for me. Similarly how do I change it to all double quotes: Inputfile = "file.txt" (4 Replies)
Discussion started by: bvnprasad123
4 Replies

3. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

4. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

5. Shell Programming and Scripting

Removal of new line character in double quotes

Hi, Could you please help me in removal of newline chracter present in between the double quotes and replacing it with space. For example ... Every field is wrapped with double quotes with comma delimiter, so I need to travese from first double quote occerence to till second double... (7 Replies)
Discussion started by: vsairam
7 Replies

6. UNIX for Advanced & Expert Users

need to concatenate two lines if the line doesnt end with quotes

Hi I am getting a source file where the columns are seperated by comma and double Quotes Eg1 : "AAA","BBB","CCCC" in the same source file i am also getting few lines where my last columns double quotes are ending in its next line or the next next line Eg2: "AAA","BBB","CCC CC"... (9 Replies)
Discussion started by: laxmi131
9 Replies

7. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

8. UNIX for Dummies Questions & Answers

stripping last character from end of each line

Hi, sorry for being dumb but I have a file with a variable amount of records (day to day it differs) but is fixed at 80 characters wide. Unfortunately the 80th and last charachter in each line is a "^M" carriage return character which i want to get rid of. Is there a really easy command that i can... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. UNIX for Advanced & Expert Users

Front end on Unix

Hi, I would like to develop a user interface on Solaris. Can anybody throw some light on currently available software utilities/ packages..? Thanks in Advance .. JS (4 Replies)
Discussion started by: shibz
4 Replies
Login or Register to Ask a Question