Solved

remove quotes from beginning and end of file using sed or awk

Posted on 2013-06-14
16
499 Views
Last Modified: 2013-06-14
Greetings,
I have a freetext.txt file that has like 4000 characters.
There are double quotes (") throughout the document.

To include one in the front, and one in the end.

I want to remove the one in the front and the one in the end.
I heard sed and awk are good for things like this...

I'm still learning those two...
does anyone have experience in this?
Thanks.
0
Comment
Question by:Evan Cutler
  • 5
  • 4
  • 3
  • +2
16 Comments
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39248014
try regular expression?
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39248031
in a bash?
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39248039
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39248043
You can easily do it with sed but you have to give a sample of what you really mean
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39248044
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39248049
my text files are different each time, but below is something that I would have in it.

"This is the "good times"; and this is the "bad times"."

I want the quotes at the beginning and the end to be removed, but quotes at "good times" and "bad times" to remain.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39248053
You can do it like

sed -i 's/^"//' filename

will remove " at the beginning of line
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39248081
that's awesome farzanj,
can you do that for both ends at the same time?

Or a repeatable thing that will overwrite the original filename?
Thanks
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Expert Comment

by:brianash
ID: 39248188
sed -i 's/^"\(.*\)"$/\1/' filename

The ^" matches the first quote
The \(.*\) matches the body of the text you want to keep
The "$ matches the quote at the end of the line

Using -i with sed makes it replace the content in the original file.

This is for files that are all on one line.  
"this is the "good times"; and this is the "bad times"."

It won't work if your file is like this:
"this is the "good times"; and thi
s is the "bad times"."

Do you need one that works with multi-lines?
0
 
LVL 9

Author Comment

by:Evan Cutler
ID: 39248209
yes...I expect my files to come in with CRLFs in there as well...
Thank you so much.
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
ID: 39248279
For both ends:

sed -i 's/^"//";s/"$//' filename

Open in new window

0
 
LVL 9

Author Closing Comment

by:Evan Cutler
ID: 39248288
Thank you my friend.
you are awesome.

Most appreciated.

Evan
0
 

Expert Comment

by:brianash
ID: 39248315
It doesn't work, though.
I don't think sed is going to work for you, I think you need awk.
i couldn't get farzanj's solution to run, but even so, it would remove the quote at the beginning and end of each line in a file, rather that the first quote at the beginning of the file and the one at the end of the file.
If you have input:
"This is "a test"
and "more lines"."

you would end up with:
This is "a test
and "more lines".

missing the quote after the word, test

The result I think you are looking for is
This is "a test"
and "more lines".

Which his solution (or mine) does not give.
I think you need awk.  I will work on something for you.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 39248328
You are welcome Evan.

In case you want to remove any " you could use.

sed -i 's/"//g' filename

Open in new window


Hope it helps.  Cheers!
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39248332
One tiny tweak - this will remove double quotes at the start and end of other lines, e.g. the " at the end of the first line in:
"This is the "good"
this is the "bad"."

Open in new window

if you really only want the " at the start of the first line, and the " at the end of the last, to be removed, use:
sed -i '1s/^"//;$s/"$//' file

Open in new window

0
 

Expert Comment

by:brianash
ID: 39248351
Nice work, simon3270
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bad ownership or modes for chroot directory 6 63
Linux  Script File Attributes 5 51
nagios 1 21
CentOS/RHEL 7 Linux maillog worldwide readable 2 39
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now