Replacing trailing space with single quote


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing trailing space with single quote
# 8  
Old 07-19-2013
Got it yoda. Thanks again.

My above assumption was wrong as you have said. I have misunderstood the role of asterik * in regular expression.

In regular expression (unlike file pattern matching in shell) an asterik will match the preceding item (space in this case) zero or more times.
It is the asterik which did the trick of getting lines which didn't end with trailing space to be included (because of the 'zero times' property mentioned in the last sentence)



In the below mentioned file heroes.txt , the line batman has a trailing space

Code:
$ cat heroes.txt
jimihendrix
batman
katman
dogman
$

# The following returns only batman, because grep is looking for lines which end with man and at least one space.
Code:
$ grep "man[ ]$" heroes.txt
batman

# In the below attempt, both katman and dogman were also returned because the lines end with the pattern man followed by zero occurences of space
Code:
$
$ grep "man[ ]*$" heroes.txt
batman
katman
dogman

Hope my assumption is right this time.
# 9  
Old 07-22-2013
Yes. Your assumption is correct John.
# 10  
Old 07-22-2013
Just a minor note: There is no need to use [ ] since this is equivalent to a single space character. So both grep 'man[ ]*$' and grep 'man *$' match zero or more spaces after man
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 07-22-2013
Quote:
Originally Posted by John K
... ... ...

In the below mentioned file heroes.txt , the line batman has a trailing space

Code:
$ cat heroes.txt
jimihendrix
batman 
katman
dogman
$

# The following returns only batman, because grep is looking for lines which end with man and at least one space.
Code:
$ grep "man[ ]$" heroes.txt
batman

... ... ...

Hope my assumption is right this time.
Actually, in the code segment shown above there was no trailing space after batman (but I added one when I quoted your text above).

The command:
Code:
grep "man[ ]$" heroes.txt

doesn't look for
Quote:
man and at least one space
it looks for man followed by exactly one space at the end of the line. The asterisk you showed in your later example:
Code:
grep "man[ ]*$" heroes.txt

matches man followed by zero or more spaces at the end of the line. And, the commands:
Code:
grep "man[ ][ ]*$" heroes.txt
        and
grep "man  *$" heroes.txt
        and
grep -E "man +$"

(note two space characters after man in the second command) will all look for lines ending with man followed by one or more spaces at the end of the line.

Many systems have an egrep utility that is a synonym for grep with the -E option; some ancient systems may have egrep but not recognize -E as a valid option to grep.

Hope this helps.
This User Gave Thanks to Don Cragun 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