Replacing trailing space with single quote


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing trailing space with single quote
# 1  
Old 07-18-2013
Replacing trailing space with single quote

Platform : RHEL 5.8

I want to end each line of this file with a single quote.

Code:
$ cat hello.txt
blueskies
minnie
mickey
gravity
snoopy

At VI editor's command mode, I have used the following command to replace the last character with a single quote.
Code:
~
~
~
:%s/$/'/g

Now, the lines in the file look like

Code:
$ cat hello.txt
blueskies '
minnie'
mickey '
gravity'
snoopy'

As you can see the line blueskies and mickey had a trailing space. So, the single quote was place after that.

Using vi (or any tool), how can I remove the trailing space and append a single quote at end of the line ?
# 2  
Old 07-18-2013
Try:
Code:
:%s/[ ]*$/'/g

This User Gave Thanks to Yoda For This Post:
# 3  
Old 07-18-2013
Thank you very much Yoda.

What is the role of [ ] in your vi command ?
# 4  
Old 07-18-2013
[] are metacharacter to match one item in the list.

[ ] matches a blank space. [ ]* means zero or more occurrences of blank space.

From Wikipedia: Regular Expression
Code:
A bracket expression. Matches a single character that is contained within the brackets. 
For example, [abc] matches "a", "b", or "c". [a-z] specifies a range which matches any lowercase letter from "a" to "z". 
These forms can be mixed: [abcx-z] matches "a", "b", "c", "x", "y", or "z", as does [a-cx-z].
The - character is treated as a literal character if it is the last or the first (after the ^) character within the brackets: [abc-], [-abc]. 
Note that backslash escapes are not allowed. The ] character can be included in a bracket expression if it is the first (after the ^) character: []abc].

# 5  
Old 07-18-2013
Thank you again Yoda.

Would it be correct to say [ ]* means zero or more occurrences of blank space or any other character ?
If * wasn't there , then, only those lines which end with a trailing space will be replaced with a single quote. Right ?
# 6  
Old 07-18-2013
Yes you are right..
# 7  
Old 07-18-2013
Quote:
Originally Posted by John K
Would it be correct to say [ ]* means zero or more occurrences of blank space or any other character ?
That is not correct. It means zero or more occurrence of just blank space and not any other character.
Quote:
Originally Posted by John K
If * wasn't there , then, only those lines which end with a trailing space will be replaced with a single quote. Right ?
If * is not there it will replace with single quote only those lines that have a trailing blank space.
This User Gave Thanks to Yoda 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 trailing empty spaces within a quote

Platform: Oracle Linux 6.5 I have a file with hundreds of values enclosed in single quotes like below. I want the trailing empty spaces before the ending quote to be removed. Expected output shown below. Can this be done using good old vi editor ? Or should I use sed or awk for this ? $ cat... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

3. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

4. Shell Programming and Scripting

Replacing Double Quote in Double Quote incsv file

Hi All , We have source data file as csv file and since data could contain commas ,each attribute is quoted into double quotes.However problem is that some of the attributa data also contain double quotes which is converted to double double quote while creating csv file XLs data : ... (2 Replies)
Discussion started by: Shalini Badal
2 Replies

5. UNIX for Dummies Questions & Answers

Replacing double spaces with single space

I am looking for a regular expression that uses sed to replace multiple spaces with single spaces on every line where it is not at the start of the line and not immediately before double slashes ('//') or between quotes ("). In its simplest form, it would look like this: sed -e 's# # #g'... (4 Replies)
Discussion started by: figaro
4 Replies

6. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

7. Shell Programming and Scripting

replacing a quote in some lines with multiple quote fields

i want to replace mistaken quotes in line starting with tag 300 and relocate the quote in the correct position so the input is 223;25 224;20100428064823;1;0;0;0;0;0;0;0;8;1;3;9697;18744;;;;;;;;;;;; 300;X;Event:... (3 Replies)
Discussion started by: wradwan
3 Replies

8. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

9. Shell Programming and Scripting

replacing single space in argument

I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument so, program will be ran ./programname hi hello hi usa now hello hello so, inside of program,... (7 Replies)
Discussion started by: convenientstore
7 Replies

10. Shell Programming and Scripting

Replacing a single quote

Hi there I have a data file like so below 'A/1';'T100002';'T100002';'';'01/05/2004';'31/05/2004';'01/06/2004';'08/06/2004';'1.36';'16';'0.22';'0';'0';'1.58';'0';'0';'0';'0';'0';'0';'clientes\resumen\200405\resumen_T100002_T100002_1.pdf';'';'0001';'S';'20040501';'';'02';'0';'S';'N'... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question