Solved

Overwriting  Original file with new output ?

Posted on 2006-06-25
5
378 Views
Last Modified: 2010-04-22
I am running  sed -f test.sed $file > $file.out              as a result I am getting .out files

( which is the expected output ) but i have to mannually rename them to the original filename
then remove all*.out

is there a way I can overwrite the original one ?  Thanks much

Shawkat

My whole script look like following :


#!/bin/ksh
for file in `echo *`
 do
  sed -f test.sed $file > $file.out
 done
#
0
Comment
Question by:dmshawkat
5 Comments
 
LVL 38

Accepted Solution

by:
yuzh earned 25 total points
ID: 16980763
you can change:
    sed -f test.sed $file > $file.out
to:

    sed -f test.sed $file > $file.out
    mv $file.out $file
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 25 total points
ID: 16988646
Always good to check the sed was successful *before* overwriting the original file.  I'd use

#!/bin/ksh
for file in *
do
  sed -f test.sed $file >file.out && mv file.out $file
done

Note that `echo *` is redundant as the shell does globbing for you.
0
 
LVL 27

Expert Comment

by:Nopius
ID: 17006381
It's possible with 'ed' command. man ed

Usage:
cat test.ed | ed - $file

ed commands are different from sed, so you need to modify script.

Very simple 'ed' script, that will add '1234' to the end of each line is:
--[cut]--
1,$s/$/1234/
w
q
--[cut]--
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

809 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