sed over writes my original file (using sed to remove leading spaces)


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this

I'm using sed to remove only the leading spaces in a file
Code:
bash-280R# cat foofile
some text
 some text
 some text
some text
some text
bash-280R#

bash-280R# sed 's/^ *//' foofile > foofile.use

bash-280R# cat foofile.use
some text
some text
some text
some text
some text
bash-280R#

That works great, however I really don't want to rename the file.
I was hoping this would work

Code:
bash-280R# sed 's/^ *//' foofile > foofile

It just overwrites foofile and makes it blank.

Is there a way to get around this and keep the original file name w/ the modification of removing leading spaces.

I will be running sed in a script

Thank you for your help

L.
# 2  
Some versions of sed have a '-i' option for in-place editing. Check your man page to see if it's available on your system.

Example:

Code:
sed -i  -e 's/^ *//' foofile


Last edited by Autocross.US; 12-01-2008 at 03:27 PM..
# 3  
You are clobbering the file before the modification.
# 4  
Thanks for your help. No -i option, looks like I'm stuck renameing the file.

OPTIONS
The following options are supported:

-e script script is an edit command for sed. See USAGE
below for more information on the format of
script. If there is just one -e option and
no -f options, the flag -e may be omitted.

-f script_file Takes the script from script_file.
script_file consists of editing commands,
one per line.

-n Suppresses the default output.
# 5  
not really, just use perl instead:
Code:
perl -pi -e 's/^ *//' foofile

# 6  
Hammer & Screwdriver You could do all on one command line

Code:
sed 's/^ *//' foofile > foofile.use ; mv foofile.use foofile

Which would execute the sed on the original file, to a new filename. Then, it would move the new file to the original.
# 7  
Thank you very much for the help and even the Perl solution.

I hope this information helps others too.

Thanks

L.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #186
Difficulty: Easy
The primary role of a Moderator at UNIX.com is to enforce the community rules and guidelines.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trying to remove leading spaces

OS : RHEL 6.7 Shell : bash I am trying to remove the leading the spaces in the below file $ cat pattern2.txt hello1 hello2 hello3 hello4 Expected output is shown below. $ cat pattern2.txt hello1 hello2 hello3 hello4 (2 Replies)
Discussion started by: John K
2 Replies

2. Shell Programming and Scripting

Remove leading and trailing spaces from a file

Hi, I am trying to remove leading and trailing spaces from a file using awk but somehow I have not been able to do it. Here is the data that I want to trim. 07/12/2017 15:55:00 |entinfdev |AD ping Time ms | .474| 1.41| .581|green |flat... (9 Replies)
Discussion started by: svajhala
9 Replies

3. Shell Programming and Scripting

Can't remove spaces with sed when calling it from sh -c

The following command works echo "some text with spaces" | sh -c 'sed -e 's/t//g''But this doesn't and should echo "some text with spaces" | sh -c 'sed -e 's/ //g''Any ideas? (3 Replies)
Discussion started by: Tribe
3 Replies

4. UNIX for Dummies Questions & Answers

[Solved] How remove leading whitespace from xml (sed /awk?)

Hi again I have an xml file and want to remove the leading white space as it causes me issues later in my script I see sed is possible but cant seem to get it to work I tried sed 's/^ *//' file.xml output <xn:VsDataContainer id="1U104799" modifier="update"> ... (10 Replies)
Discussion started by: aniquebmx
10 Replies

5. OS X (Apple)

Remove leading spaces from file names and folders

Hi All, I have a vexing issue with leading spaces in file names. Basically, we're moving tons of data from our ancient afp file share to Box.com and Box forbids leading spaces in files or folders. The HFS file system seems to be perfectly fine with this, but almost all other Unix file systems... (1 Reply)
Discussion started by: prometheon123
1 Replies

6. Shell Programming and Scripting

sed delete leading spaces in a .csv if not in a string

Solaris, ksh I have a .csv file I am trying to clean up before loading into the database. The file contains comma separated columns that have leading spaces which I need to remove. The trouble is, some columns that should not be touched are strings which happen to have the same pattern in them. ... (4 Replies)
Discussion started by: gary_w
4 Replies

7. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

8. Shell Programming and Scripting

tr and sed remove spaces. how to stop this?

if the answer is obvious, sorry, I'm new here. anyway, I'm using tr to encrypt with rot-13: echo `cat $script | tr 'a-zA-Z' 'n-za-mN-ZA-M'` > $script it works, but it removes any consecutive spaces so that there is just one space between words. I've had this problem before while using sed to... (5 Replies)
Discussion started by: Trichopterus
5 Replies

9. Shell Programming and Scripting

Remove leading zeroes in 2nd field using sed

Hi Forum. I tried searching the forum but couldn't find a solution for my question. I have the following data and would like to have a sed syntax to remove the leading zeroes from the 2nd field only: Before: 2010-01-01|123|1|1000|2000|500|1500|600|700... (18 Replies)
Discussion started by: pchang
18 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies

Featured Tech Videos