Replace line with sed on OS X


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace line with sed on OS X
# 1  
Old 10-24-2012
Replace line with sed on OS X

So, I want to, in a script, do an in-line replacement of a line in a file. Piece of cake in Linux with GNU sed. With OSX and BSD sed, not so much. I've been Googling, but nothing is really helping... either an answer is for a use case different enough from what I want to do, or it's something like inserting a newline on the command line, and I need this in a script.

I'm not stuck on sed, but that really should be the right tool to use. I do want to use a tool that comes natively with OS X, so installing GNU sed is a last resort. I'm on Lion, but would like one solution that'll work for 10.5 - 10.8

For simplicity's sake, there's a file with a few lines, one of which starts with "flags". I want to just replace that line.
# 2  
Old 10-24-2012
Try perl:

Code:
# cat file
hello
flags this line starts with flags
world
#
# perl -i.bak -pe 's/^flags.*/new line/' file
#
# cat file
hello
new line
world
#

# 3  
Old 10-24-2012
Inserting a line is rewriting the file from that point, so just sed to a temp file and then cat the temp file content over the original. You can also drive ex, vi's foundation, from a script very like sed but with additional commands.
Code:
echo '2c
new line
.
w
q' | ex your_file

Really, to actually rewrite just from line 2, not the whole file, you might need a C/PERL routine that overwrites existing positions within the file, extending or shortening it at the end. I suspect most tools truncate and write data from temp files when you save. After all, they are not tolerant of other processes mmap()ing the file or seeking around and reading lower locations that might disappear momentarily. I will tusc a vi and see. Yep, vi close()s, opens with creat() and writes it all. So, back to "use sed with a temp file." If you dislike temp files and the file is smaller than max env size (a meg or so), You can store the file in the env:
Code:
z=`sed '...' your_file`
echo "$z" >your_file

I wrote a C tool once that overwrote $1 from stdin without truncating and trimmed the file length at the end if shorter using fcntl(), so I guess PERL can do that, too. But generally, update in place is a fantasy.

Last edited by DGPickett; 10-24-2012 at 03:30 PM..
# 4  
Old 10-24-2012
Quote:
Originally Posted by balajesuri
Try perl:

Code:
perl -i.bak -pe 's/^flags.*/new line/' file

Thanks! I gotta learn me some perl...
# 5  
Old 10-25-2012
Hi.
Quote:
Originally Posted by jnojr
So, I want to, in a script, do an in-line replacement of a line in a file. Piece of cake in Linux with GNU sed. With OSX and BSD sed, not so much. ....
The in-place modification of files usually involves writing a scratch-temporary file and copying the scratch file back over the original (or renaming the file). As noted, not all versions of sed, etc., have this capability builtin.

One general solution is the addition of command sponge, a component of package moreutils. So for system OS, ker|rel, machine: FreeBSD, 8.0-RELEASE, i386:
Code:
% mag show moreutils
moreutils is a growing collection of the unix tools that nobody thought to
write thirty years ago.

So far, it includes the following utilities:

 - sponge: soak up standard input and write to a file
 - ifne: run a program if the standard input is not empty
 - vidir: edit a directory in your text editor
 - vipe: insert a text editor into a pipe
 - ts: timestamp standard input
 - combine: combine the lines in two files using boolean operations
 - pee: tee standard input to pipes
 - zrun: automatically uncompress arguments to command
 - mispipe: pipe two commands, returning the exit status of the first
 - isutf8: check if a file or standard input is utf-8
 - lckdo: execute a program with a lock held
 - parallel: run multiple jobs at once

The 'ifdata' utility is not included in this FreeBSD port.


(The utility mag is a local generic repository scanner, so that we need not recall the details of apt-cache, yum, rpm, zypper, ports, etc.)

See thread https://www.unix.com/unix-advanced-ex...-big-file.html for a discussion of in-place editing and a few examples of a sponge-like perl utility.

Best wishes ... cheers, drl
# 6  
Old 10-26-2012
If I understand GNU sed's -i option correctly, the following ex commands on OS X should be roughly equivalent to the corresponding sed commands:
Code:
# ex equivalent of sed -i '/flags/s/.*/new $text/' file
ex -s file <<-"EOF"
        /^flags/s/.*/new $text/
        w
        q
EOF

# ex equivalent of sed -i.bak '/flags/s/.*/new $text/' file
ex -s file <<-"EOF"
        w %.bak
        /^flags/s/.*/new $text/
        w
        q
EOF

text=something
# ex equivalent of sed -i.bak "/flags/s/.*/new $text/" file
ex -s file <<-EOF
        w %.bak
        /^flags/s/.*/new $text/
        w
        q
EOF

# 7  
Old 10-26-2012
I usually just install a personal copy of gnu sed, namng it gsed. The old sed's had line length limits and EOF handling bugs that are aggravating, but often they were faster as the buffer was not so indirect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

sed to do replace on line above

Hello, I need to use sed to find a certain string and then do a replace on the line above. For example if I had the following text: AAAAAAA BBBBBBB CCCCCCC DDDDDD I would like to find CCCCCCC, but I would like the replace to be carried out on BBBBBBB. I have done some... (11 Replies)
Discussion started by: toptiger
11 Replies

7. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

8. Shell Programming and Scripting

sed to replace <cr><cr><cr> in a line with <cr>

Hi, Please help me with the sed command for the following scenario: I've in abc.txt, asdasdada<cr>aasdsad<cr><cr>asdasdfsdfs<cr><cr><cr>asdsada<cr>adasd<cr>daasdaasd<cr><cr><cr> I want a sed command to do asdasdada<cr>aasdsad<cr>asdasdfsdfs<cr>asdsada<cr>adasd<cr>daasdaasd<cr> ... (13 Replies)
Discussion started by: skpvalvekar
13 Replies

9. Shell Programming and Scripting

How to replace one line with three in sed

Hi, I have a problem. I want to use SED to replace one line with three It looks like: PARAM='first_line,second_line,third_line' And in file: blablabla blablabla PARAM blablabla blablabla blablabla All I want is to change this into: blablabla blablabla first_line second_line... (6 Replies)
Discussion started by: ch0sen
6 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question