Removing one or more blank characters from beginning of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing one or more blank characters from beginning of a line
# 1  
Old 07-09-2012
Removing one or more blank characters from beginning of a line

Hi,

I was trying to remove the blank from beginning of a line.

when I try:

Code:
sed 's/^ +//' filename

it does not work
but when I try
Code:
sed 's/^ *//' filename

it works
But I think the first command should have also replaced any line with one or more blanks.
Kindly help me in understanding why the first command did not work.
# 2  
Old 07-09-2012
Which variant of sed did you try that on? Remember, not all seds support the quantifier '+'; GNU sed supports this quantifier.

Alternatively, you can try perl,
Code:
perl -pe 's/^ +//g' filename

# 3  
Old 07-09-2012
Quote:
Originally Posted by babom
Hi,

I was trying to remove the blank from beginning of a line.

when I try:

Code:
sed 's/^ +//' filename

it does not work
but when I try
Code:
sed 's/^ *//' filename

it works
But I think the first command should have also replaced any line with one or more blanks.
Kindly help me in understanding why the first command did not work.
try like this Smilie [use the backslash for escape the plus_metacharacter in ext. regex expres]
Code:
# sed 's/^ \+//' infile

This User Gave Thanks to ygemici For This Post:
# 4  
Old 07-09-2012
The +-operator is only supported in extended regex, standard sed only supports basic regex. So it would have worked if your sed (BSD,GNU) supports the -E option.

In regular sed:
Code:
sed 's/^ \{1,\}//' filename

But in this case a plain asterisk should work too:
Code:
sed 's/^ *//' filename



--
Note: \+ is GNU sed only
# 5  
Old 07-09-2012
Quote:
Originally Posted by Scrutinizer

--
Note: \+ is GNU sed only
yes Smilie, it is a GNU extension.
# 6  
Old 07-09-2012
Thanks guys, it worked with
Code:
\+

.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Inserting n characters to beginning of line if match

I would like to insert n number of characters at the beginning of each line that starts with a given character. If possible, I would be most appreciative for a sed or awk solution. Given the data below, I would like to be able to insert either 125 spaces or 125 "-" at the beginning of every line... (6 Replies)
Discussion started by: jvoot
6 Replies

2. UNIX for Beginners Questions & Answers

Removing characters from beginning of multiple files

Hi, I have been searching how to do this but I can't seem to find how to do it. Hopefully someone can help. I have multiplr files, 100's example 12345-zxys.213423.zyz.txt. I want to be able to take all these files and remove the first '12345-' from each of the files. '12345-' these characters... (5 Replies)
Discussion started by: israr75
5 Replies

3. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : ®¿¿ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

4. Shell Programming and Scripting

Removing blank/white spaces and special characters

Hello All , 1. I am trying to do a task where I need to remove Blank spaces from my file , I am usingawk '{$1=$1}{print}' file>file1Input :- ;05/12/1990 ;31/03/2014 ; Output:- ;05/12/1990 ;31/03/2014 ;This command is not removing all spaces from... (6 Replies)
Discussion started by: himanshu sood
6 Replies

5. Shell Programming and Scripting

sed - Removing all characters from token to end of line

Hello. The token is any printable characters between 2 " . The token is unknown, but we know that it is between 2 " Tok 1 : "1234x567" Tok 2 : "A3b6+None" Tok 3 : "A3b6!1234=@" The ligne is : Line 1 : "9876xABCDE"Do you have any code fragments or data samples in your post Line 2 : ... (3 Replies)
Discussion started by: jcdole
3 Replies

6. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

7. Shell Programming and Scripting

Removing characters from end of line (length unknown)

Hi I have a file which contains wrong XML, There are some garbage characters at the end of line that I want to get rid of. Example: <request type="product" ><attributes><pair><name>q</name><value><!]></value></pair><pair><name>start</name><value>1</value></pair></attributes></request>�J ... (7 Replies)
Discussion started by: dirtyd0ggy
7 Replies

8. UNIX for Dummies Questions & Answers

Removing extra new line characters

Hello, I have a text file that looks like: ABC123|some text|some more text|00001 00002 0003 0004 000019|000003|Item I have searched and found an example to remove the extra new line characters using grep and sed, but it (I think) assumes the lines start with a number and the... (5 Replies)
Discussion started by: c56444
5 Replies

9. Solaris

deleting blank space in beginning of line in vi

How can we delete all the blank spaces in the beginning of some lines in a text in vi? Thanks, (2 Replies)
Discussion started by: Pouchie1
2 Replies

10. Shell Programming and Scripting

Deleting the blank line in a file and counting the characters....

Hi, I am trying to do two things in my script. I will really appreciate any help in this regards. Is there a way to delete a last line from a pipe delimited flat file if the last line is blank. If the line is not blank then do nothing..... Is there a way to count a word that are starting... (4 Replies)
Discussion started by: rkumar28
4 Replies
Login or Register to Ask a Question