Substitute a character with sed


Login or Register to Reply

 
Thread Tools Search this Thread
# 8  
Old 6 Days Ago
GNU sed has the [ICODE]-c/--copy[/ICODE] Option which keeps hard+soft links. But this is not an atomic operation. (the mv-Variant is atomic).

Alternatively sponge is there too. (Not very portable, too ;-) ).

Sponge soaks up stdin and after that writes to stdout:

Code:
sed -e s/bla/blub/ file.txt | sponge file.txt

One should know when atomic updates are more important than keeping files/inodes and vice versa. Use the solution which fits your case.

Note
GNU sed version 4.7.6(current sed git version) does not contain --copy. There is a man-page on the web(This one: sed(1) - Linux man page) which states there is a --copy. But it seems not to be really there.

Last edited by stomp; 1 Day Ago at 08:49 AM..
This User Gave Thanks to stomp For This Post:
MadeInGermany (5 Days Ago)
# 10  
Old 6 Days Ago
Quote:
Originally Posted by bakunin
Moderator's Comments:
Mod Comment I edited the threads title to correct the typo ("substite"-> "substitute", "charather" -> "character").


Please note that the -i switch is NOT part of the standard sed and is only understood by GNU-sed. In addition i strongly suggest NOT to use it even if it is available:

Code:
sed -i '....'  /path/to/file                              # not recommended

if sed '....' /path/to/file > /path/to/file.tmp ; then    # recommended way
     mv  /path/to/file.tmp  /path/to/file
     chown <correct ownership> /path/to/file              # optional
     chmod <correct filemode> /path/to/file               # optional
else
     <error handling procedure here>
fi

................................................
Hello Bakunin sir,

With full respect I competely agree with you on this part, would like to add 1 point here that GNU sed is having -i.bak option too, which takes backup of Input_file before editing it in-place, here is an example for same.

Let's say following is my Input_file:
Code:
cat Input_file
A                           B        C                  D

Now when I run following command:
Code:
sed -i.bak 's/^A/Singh/'   Input_file

I could see backup file named Input_file.bak is created with actual content and Input_file is in-placed with new string. IMHO may be we could use this for safer side?

Code:
-rw-r--r--  1 singh  test_singh_bla   411 Mar 15 09:00 Input_file.bak
-rw-r--r--  1 singh  test_singh_bla   415 Mar 15 09:01 Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
bakunin (5 Days Ago)
# 11  
Old 5 Days Ago
Quote:
Originally Posted by RavinderSingh13
GNU sed is having -i.bak option too, which takes backup of Input_file before editing it in-place
First: I haven't known that, so thank you for teaching me something new.

The point was not that it takes or takes not a backup, that it changes or doesn't change the inode (as MadeInGermany mentioned - thank you for providing a way to circumvent that too, should the need arise), though. It should be worth noting, though, that using a mv within filesystem boundaries is faster than cp. Across filesystems mv and cp are probably equally fast.

For me the main point is portability. I can choose between using the POSIX-way of doing things and that works on all systems and using the GNU-way and that works on less than all systems. For the same reason i would rather hesitate to use a feature in the POSIX-standard which i know would not work (or work differently) on GNU. This is not limited to sed options or something particular at all. The same goes for anything else i do on a UNIX (or Linux) system.

The second reason i would not suggest to use -i is NOT, that it rolls some usually necessary actions into one: i talked about not preserving the inode or filemode or owner, but it won't always matter if they are being preserved or not. The point is it makes it look like it would be the same file while it is not. One can do everything and anything but one: lie to your users. If you do something, whatever it is, do it - but don't pretend you do something else.

bakunin

Last edited by RudiC; 5 Days Ago at 07:24 AM.. Reason: repaired ICODE tag.
This User Gave Thanks to bakunin For This Post:
RavinderSingh13 (5 Days Ago)
# 12  
Old 5 Days Ago
Quote:
Originally Posted by bakunin
I talked about not preserving the inode or filemode or owner, but it won't always matter if they are being preserved or not. The point is it makes it look like it would be the same file while it is not. One can do everything and anything but one: lie to your users. If you do something, whatever it is, do it - but don't pretend you do something else.
You're right. The exact method is not fully explained in the man-page. There's only a very short sentence about -i.

In the sed info-page however it is explained more in detail.

Code:
'-i[SUFFIX]'
'--in-place[=SUFFIX]'
     This option specifies that files are to be edited in-place.  GNU
     'sed' does this by creating a temporary file and sending output to
      this file rather than to the standard output.(1).

I would feel better if somone's called a liar not so easily.

---

What's regarding portability, I think the message is clear:
If one wants portability: Stick to POSIX. Don't use GNU, bash, php, python, ruby, whatever, ...

Last edited by stomp; 5 Days Ago at 09:28 AM..
# 13  
Old 5 Days Ago
Speaking strictly as a scientist: python ruby, and julia are extremely important for research and analysis.

GNU supports the environment variable POSIXLY_CORRECT. This helps a lot when you are porting code back and forth between several platforms. I would suggest that you check it out. Defining it changes behavior to be pretty close to POSIX. Not perfect, IMO.

Linux is effectively inescapable in modern data centers. Even appliances like routers run on embedded Linux. So does your refrigerator ....

GNU.org position on POSIX:
GNU Coding Standards: Non-GNU Standards

Some OSes kind of go off the deep end in attempt to be a sort of "all versions of standards" - which is kind of what you are discussing here.

Solaris is an example:

Compliance with standards in that OS - for a new user or somebody porting an app - can be confusing. You get different versions of awk, tr and so on. This depends on which of several possible paths, and their order, ex: /usr/xpg4/bin versus /usr/bin, you have set up in PATH. This can break a lot of things that worked correctly on HP-UX but not Solaris, for example. I spent a lot of time tweaking the PATH variable for different applications and their associated users

XPG4 -
man pages section 5: Standards, Environments, and Macros


Don Cragun here actively works on standards, and definitely will have have some opinions here.
# 14  
Old 5 Days Ago
Maybe the Standards-Part should be separated from the thread? (Last time I requested Thread separation there was a broken thread afterwards in the forum, which might have nothing to do with that, but I just wanted to mention).

--- Post updated at 03:08 PM ---

I would throw in another question:

Does POSIX contain a programming language environment?
Is there a programming language available in every environment?

In contrast to Linux the situation of HP-UX,Solaris,AIX, ... in terms of easy available and easy installable software for me(as linux-only-user) seems like the availablity of water in the ocean and the desert.

So I'm wondering if there is any programming language installed in those unices so there maybe one programming environment available everywhere. I'm not talking about sh/ksh/bash. I do not consider them as real programming language. Yes, one can write very large shell scripts, but I think it's a mess. Bad maintainability. Slow Speed. High Resource Footprint(New process creatings for most things). Unsafe Programming environment. Inferior programming feature set.

I would imagine perl 5(perl 5 is 25 years old now. Having perl means: just the perl-base, no modules) could be everywhere. Despite being the opposite of my favourite programming language, if I had/wanted to write portable code, perl would be a lot better than any shell. The developer of inxi(hardware information tool,Inxi at github) went that way - inxi a 20,000 line perl script - written targeted at very high compatibility(Before switching to perl he had used bash and gnu awk).

Last edited by stomp; 5 Days Ago at 10:31 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
sed searches a character string for a specified delimiter character, and returns a leading or traili fspalero Shell Programming and Scripting 2 01-09-2017 04:31 AM
[sed]: Substitute a string with a multiline value dae Shell Programming and Scripting 4 08-02-2016 04:56 PM
sed substitute command -- need help pathunkathunk Shell Programming and Scripting 3 03-07-2013 12:55 PM
sed fails to apply substitute commands Adolf1994 Shell Programming and Scripting 23 08-08-2012 10:58 AM
How to substitute variable in sed for special character? vinothsekark Shell Programming and Scripting 9 04-29-2012 06:39 AM
using sed or gsub to substitute characters! puttster Shell Programming and Scripting 3 10-31-2010 03:00 AM
In Sed how can I replace starting from the 7th character to the 15th character. mohullah Shell Programming and Scripting 5 09-22-2010 08:39 PM
how to substitute filepaths with sed or awk? ergy1983 Shell Programming and Scripting 3 07-02-2010 10:28 AM
Sed question to substitute data in \2 Peace_Dude1 Shell Programming and Scripting 6 05-28-2010 02:26 PM
Using sed to substitute between quotes. Steve_altius UNIX for Dummies Questions & Answers 3 06-22-2009 09:00 AM
Using sed to substitute first occurrence quixoticking11 Shell Programming and Scripting 2 07-09-2008 10:36 AM
check for a particular character inside a file and substitute with a given character? karthikprasathk AIX 1 07-01-2008 03:29 AM
Using SED to substitute between two patterns. Steve_altius Shell Programming and Scripting 2 06-10-2008 12:03 PM
read a variable character by character, substitute characters with something else vipervenom25 UNIX for Dummies Questions & Answers 2 06-06-2008 03:18 PM
sed substitute situation newbreed1 UNIX for Dummies Questions & Answers 16 06-03-2005 02:26 PM