LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using sed how it is possible?? (https://www.linuxquestions.org/questions/linux-newbie-8/using-sed-how-it-is-possible-869801/)

anishkumarv 03-20-2011 11:52 AM

Using sed how it is possible??
 
hi all,

im having problems. I need to change all number 10 in a text file to word form, or in short from 10->ten. the thing is number 10 including in dates such as 10/22/1997 or 03-10-2011 should not be changed. im having some trouble because the file contains numbers like "price range from 10-50k".

this is just a sample.

name: john smith
birthday: 10-11-1995
date hired: 05/10/2010
expected salary: 10-50k
typing speed: 10 wpm
coordinates: (10, 10)

should come out like this:

name: john smith
birthday: 10-11-1995
date hired: 05/10/2010
expected salary: ten-50k
typing speed: ten wpm
coordinates: (ten, ten)

Using sed command is it possible to change like this.. please guide me!!

sycamorex 03-20-2011 11:59 AM

Try the following:
Code:

sed -e '/day/ !s/10/ten/g' -e '/date/ !s/10/ten/g' file

anishkumarv 03-20-2011 12:05 PM

Hi

If i execute this commad

Code:

sed -e '/day/ !s/10/ten/g' -e '/date/ !s/10/ten/g' sed1
The output comes like this!!!

Quote:

name: john smith
birthday: ten-11-1995
date hired: 05/ten/20ten
expected salary: ten-50k
typing speed: ten wpm
coordinates: (ten, ten)

sycamorex 03-20-2011 12:09 PM

Quote:

Originally Posted by anishkumarv (Post 4297124)
Hi

If i execute this commad

Code:

sed -e '/day/ !s/10/ten/g' -e '/date/ !s/10/ten/g' sed1
The output comes like this!!!

Ooops, didn't check it. Nevermind, I've got a better solution:

Code:

sed -E '/day|date/ !s/10/ten/g' file

anishkumarv 03-20-2011 12:15 PM

Code:

sed -E '/day|date/ !s/10/ten/g' file

are u damn sure...ie caps -E beacuse it shows error!!!

I think we cant able to use -E option in sed command!!!

sycamorex 03-20-2011 12:18 PM

It works for me:
Code:

sycamorex@thinkslack:~/temp/ttt$ cat file.txt
name: john smith
birthday: 10-11-1995
date hired: 05/10/2010
expected salary: 10-50k
typing speed: 10 wpm
coordinates: (10, 10)

sycamorex@thinkslack:~/temp/ttt$ sed -E '/day|date/ !s/10/ten/g' file.txt
name: john smith
birthday: 10-11-1995
date hired: 05/10/2010
expected salary: ten-50k
typing speed: ten wpm
coordinates: (ten, ten)

What version of sed are you using?
Code:

sed --version

anishkumarv 03-20-2011 12:20 PM

GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law


can u post your version?? that means i can update!!!

sycamorex 03-20-2011 12:24 PM

I am using 4.2.1. Can you try with the -r flag instead of -E?

anishkumarv 03-20-2011 12:27 PM

Thanks for Your immediate response!!

It works like charm!!!

sycamorex 03-20-2011 12:28 PM

Having done some googling, it turns out that I should have used -r in the first place.
The -E flag is added just to provide compatibility with the BSD version of sed. The standard
flag for extended regular expressions in GNU Sed is -r.

http://stackoverflow.com/questions/3...ed-e-and-sed-e

anishkumarv 03-20-2011 12:33 PM

Hi

Thanks a ton!!! :-)

kurumi 03-20-2011 07:58 PM

Ruby(1.9+)

Code:

$ cat file
name: john smith
birthday: 10-11-1995
date hired: 05/10/2010
expected salary: 10-50k
typing speed: 10 wpm
coordinates: (10, 101)

$ruby -pne '$_.gsub!(/\b10\b/,"ten") if not $_[/date|day/]' file
name: john smith
birthday: 10-11-1995
date hired: 05/10/2010
expected salary: ten-50k
typing speed: ten wpm
coordinates: (ten, 101)

Note, if you really wanted the number "10" to be replaced, and "101" appears, the sed solution suggested so far would not work.


All times are GMT -5. The time now is 07:07 PM.