Substitute a character with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Substitute a character with sed
# 8  
Old 03-14-2019
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; 03-19-2019 at 09:49 AM..
This User Gave Thanks to stomp For This Post:
# 9  
Old 03-15-2019
another option
Code:
sed 'y%-%/%' file

# 10  
Old 03-15-2019
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:
# 11  
Old 03-15-2019
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; 03-15-2019 at 08:24 AM.. Reason: repaired ICODE tag.
This User Gave Thanks to bakunin For This Post:
# 12  
Old 03-15-2019
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; 03-15-2019 at 10:28 AM..
# 13  
Old 03-15-2019
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 03-15-2019
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; 03-15-2019 at 11:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed substitute command -- need help

I am trying to do what I thought should be a simple substitution, but I can't get it to work. File: Desire output: I thought I'd start with a sed command to remove the part of the header line preceding the string "comp", then go on to remove the suffix of the target string (e.g. ":3-509(-)"),... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

2. Shell Programming and Scripting

How to substitute variable in sed for special character?

Hi , I have input file like below Hi this is "vinoth". Hi happy to work with 'unix' USA(united states of America) My script variables are below : Dquote=Ộ Squote=&#$567 Obrac=&^986 Cbrac=&^745 I want to read the variables in my SED command to replace the double quote,single... (9 Replies)
Discussion started by: vinothsekark
9 Replies

3. Shell Programming and Scripting

using sed or gsub to substitute characters!

Is there a way to substitute the URL-encoding references of ( & and ` ) with their actual appearance? for example.... %26 is & say I want to convert every %26 in my file to &..... awk '{gsub(/%26/,"&");print}' Is there a way to do this? I also want to be able to convert ` too! (3 Replies)
Discussion started by: puttster
3 Replies

4. Shell Programming and Scripting

Sed question to substitute data in \2

Hi All, Here is what I'm trying to do with sed: Input File: somechangeVariable1=Something I would like to change somechangeVariable2=Something else I would like to change ... Output File: somechangeVariable1=Something I would like to different somechangeVariable2=Something else I would... (6 Replies)
Discussion started by: Peace_Dude1
6 Replies

5. UNIX for Dummies Questions & Answers

Using sed to substitute between quotes.

I'm using sed to perform a simply search and replace. The typical data is: <fig><image href="Graphics/BAV.gif" align="left" placement="break" I need to replace the value in the first set of quotes, keeping the remainder of the line the same. Thus: <fig><image href="NEW_VALUE" align="left"... (3 Replies)
Discussion started by: Steve_altius
3 Replies

6. Shell Programming and Scripting

Using sed to substitute first occurrence

I am trying to get rid of some ending tags but I run into some problems. Ex. How are you?</EndTag><Begin>It is fine.</Begin><New> Just about I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>) I tried the following sed... (2 Replies)
Discussion started by: quixoticking11
2 Replies

7. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

8. Shell Programming and Scripting

Using SED to substitute between two patterns.

Hi All, I'm currently using SED to make various changes to some .xml files I'm working on, but I'm stuck on this particular problem. I want to remove '<placeholder>element-name</placeholder>' from the following: <heading>Element <placeholder>element-name</placeholder> not... (2 Replies)
Discussion started by: Steve_altius
2 Replies

9. UNIX for Dummies Questions & Answers

read a variable character by character, substitute characters with something else

im having trouble doing this: i have a variable with 2 characters repeating e.g. aababbbaaaababaabbaabbba is there a way i can search the variable for a's and b's and then change a's to b's and b's to a's? im guessing its like getting the 1's compliment of the string im doing this in... (2 Replies)
Discussion started by: vipervenom25
2 Replies

10. UNIX for Dummies Questions & Answers

sed substitute situation

I am having a problem executing a sed substitute in a file. I have tried alot of different things I found in previous posts, however non seem to work. I want to substitute this in $FILE: VALUE=33.4 In the script I have tried the following: prev=$(awk -F"=" '{ print $2 }' $FILE ) new=$(echo... (16 Replies)
Discussion started by: newbreed1
16 Replies
Login or Register to Ask a Question