Sed does not make changes in the file but to the standard output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed does not make changes in the file but to the standard output
# 1  
Old 04-06-2009
Sed does not make changes in the file but to the standard output

I have an xml file.
I am doing some change, say deleting line 770. File name is file.xml. I use:

sed '770d' file.xml

but this does not actually make changes in the *file* but shows the changes on standard output (screen)

if i use

$var=`sed '770d' file.xml`
echo $var > file.xml

this makes me loose all the formatting of xml (no carriage return is inserted, instead everything is inputted in the form of a single paragraph).
# 2  
Old 04-06-2009
Code:
sed '770d' file.xml >temp.xml
mv temp.xml file.xml

Thanks
NT
# 3  
Old 04-06-2009
Because that's how echo works

Try

Code:
sed -e 's700d' <file.xml >foo.xml
mv foo.xml file.xml

You can edit in place with perl:

Code:
perl -n -i.BAK -e 'print if $. != 700' file.xml

The original file is in file.xml.BAK
# 4  
Old 04-06-2009
Hi.

A GNU/Linux package, moreutils, contains a few interesting utilities. Of relevance here is sponge. It sucks up data from STDIN, and spits it out to a file of your choice [default STDOUT] at EOF. This alleviates the common error of re-directing STDOUT to the original input file. It's a good general solution, but it does involve using a temporary file -- we often accept a little overhead for convenience.

Here's a demonstration driver script using cat and sed:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate sponge.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) perl
set -o nounset
echo

FILE=${1-data1}
SPONGE=./sponge

cp original-data $FILE
echo " Data file $FILE:"
cat $FILE

echo
echo " Results for default STDOUT:"
cat $FILE | $SPONGE

cp original-data $FILE
echo
echo " Results for re-writing $FILE:"
sed '/2/d' $FILE | $SPONGE $FILE
cat $FILE

exit 0

Producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
perl 5.8.4

 Data file data1:
1
2
3

 Results for default STDOUT:
1
2
3

 Results for re-writing data1:
1
3

Not everyone wants to obtain GNU/Linux utilities, so I hacked together a perl script to do the basic job. No warranties, etc.:
Code:
#!/usr/bin/perl

# @(#) sponge   Read STDIN, write to file at EOF.

## Modification history: when / who / what: most recent at top.
#  Relocate to end if grows too long, or re-sequence.
#
# 2009.04.06 / drl / original.

use warnings;
use strict;

my ($debug);
$debug = 1;
$debug = 0;

my ( $buf, $file, $tmp, $temporary );
$file = shift || ">-";

$temporary = "/tmp/sponge_$$";
open( $tmp, ">", $temporary ) || die " Cannot open $temporary for write.\n";
print " debug write - temporary is :$temporary:\n" if $debug;
while ( read( STDIN, $buf, 16384 ) ) {
  print $tmp $buf;
}

close $tmp;
open( $tmp, "<", $temporary ) || die " Cannot open $temporary for read.\n";
print " debug read  - temporary is :$temporary:\n" if $debug;
if ( $file eq "-" ) {
  open( FILE, ">-" ) || die " Cannot open STDOUT for writing.\n" if $debug;
}
else {
  open( FILE, ">$file" ) || die " Cannot open $file for write.\n";
}
print " debug write - file is :$file:\n" if $debug;
while ( read( $tmp, $buf, 16384 ) ) {
  print FILE $buf;
}

unlink $temporary;

exit(0);

So sponge can be used for any such situation, sed, awk, grep, etc. To make effective use of sponge, you'd probably want to place it in a directory in your path, say, $HOME/bin for most users. That would eliminate the requirement of needing it in the current directory.

Kernighan and Pike addressed this issue in a slightly different way in 1978 with a script called overwrite in The UNIX Programming Environment, page 152 ff.

One possible useful modification would be to omit the temporary file if the data i short enough -- left as an exercise for the reader Smilie ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SU and Standard Output

Bit of a strange one. Have a script called rapidclone_test.sh which calls Oracle rapidclone using su -c as an oracle osuser. However, if I control+c out to the calling shell anything entered is not displayed on the terminal. Any command executes successfully though. Why is the standard... (1 Reply)
Discussion started by: u20sr
1 Replies

2. Red Hat

Command understanding the output file destination in case of standard output!!!!!

I ran the following command. cat abc.c > abc.c I got message the following message from command cat: cat: abc.c : input file is same as the output file How the command came to know of the destination file name as the command is sending output to standard file. (3 Replies)
Discussion started by: ravisingh
3 Replies

3. Shell Programming and Scripting

Redirecting output of Make to file

Hi, I am unable to get this script to work as desired. Basically, if an argument "log" is sent into the script, it outputs the result of the Make to a file output.log. However, if the argument is not passed, I want the output to be just put on screen (no redirection). See code snippet below. #... (3 Replies)
Discussion started by: srujan45
3 Replies

4. Shell Programming and Scripting

sed command works from cmd line to standard output but will not write to file

Hi all .... vexing problem here ... I am using sed to replace some special characters in a .txt file: sed -e 's/_<ED>_/_355_/g;s/_<F3>_/_363_/g;s/_<E1>_/_341_/g' filename.txt This command replaces <ED> with í , <F3> with ó and <E1> with á. When I run the command to standard output, it works... (1 Reply)
Discussion started by: crumplecrap
1 Replies

5. Shell Programming and Scripting

How redirect standard output to a file

Hi guys, i have a script named purgeErrors.ksh, when i execute this script i need to redirect the output to a log file in the same directory, how can i do that ?? -- Aditya (5 Replies)
Discussion started by: chaditya
5 Replies

6. UNIX for Dummies Questions & Answers

Redirect Standard output and standard error into spreadsheet

Hey, I'm completely new at this and I was wondering if there is a way that I would be able to redirect the log files in a directories standard output and standard error into and excel spreadsheet in anyway? Please remember don't use too advanced of terminology as I just started using shell... (6 Replies)
Discussion started by: killaram
6 Replies

7. Shell Programming and Scripting

make a variable read the value from the standard input

Hi, In Perl, how can we define a variable make it read the value from the standard input? Meaning, how can have the user type in the value that will be assigned to the variable? Thanks, (2 Replies)
Discussion started by: Pouchie1
2 Replies

8. Shell Programming and Scripting

file content to standard output from a script

hi folks how do i output contents of file in standard output. in my script, i say x=`cat filename' echo $x below is the actual file *********** asda afdf fdf sdf dsfsdfsd fds dsfdfsdfsdg ssgd sgdg sdfsdgfsdg dgfd gsfd gs sdg sfdg s in my script, i am trying to output the... (4 Replies)
Discussion started by: bryan
4 Replies

9. UNIX for Dummies Questions & Answers

Error: Internal system error: Unable to initialize standard output file

Hey guys, need some help. Running AIX Version 5.2 and one of our cron jobs is writing errors to a log file. Any ideas on the following error message. Error: Internal system error: Unable to initialize standard output file I'm guessing more info might be needed, so let me know. Thanks (2 Replies)
Discussion started by: firkus
2 Replies

10. Shell Programming and Scripting

Standard Output

When I run a third parties program from the command line (this program basically list's a whole load of stuff) and write the output to a file it splits the output, i.e. in the middle of the file appears the exit command. If I don't redirect the output and write it to tty then the output is... (3 Replies)
Discussion started by: dbrundrett
3 Replies
Login or Register to Ask a Question