LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-20-2011, 11:52 AM   #1
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Rep: Reputation: 10
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!!
 
Old 03-20-2011, 11:59 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Try the following:
Code:
sed -e '/day/ !s/10/ten/g' -e '/date/ !s/10/ten/g' file
 
Old 03-20-2011, 12:05 PM   #3
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
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)
 
Old 03-20-2011, 12:09 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Quote:
Originally Posted by anishkumarv View Post
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
 
Old 03-20-2011, 12:15 PM   #5
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
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!!!

Last edited by anishkumarv; 03-20-2011 at 12:16 PM. Reason: nead to change in body content
 
Old 03-20-2011, 12:18 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
 
Old 03-20-2011, 12:20 PM   #7
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
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!!!
 
Old 03-20-2011, 12:24 PM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
I am using 4.2.1. Can you try with the -r flag instead of -E?
 
1 members found this post helpful.
Old 03-20-2011, 12:27 PM   #9
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
Thanks for Your immediate response!!

It works like charm!!!
 
Old 03-20-2011, 12:28 PM   #10
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
 
Old 03-20-2011, 12:33 PM   #11
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 294

Original Poster
Rep: Reputation: 10
Hi

Thanks a ton!!! :-)
 
Old 03-20-2011, 07:58 PM   #12
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
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.

Last edited by kurumi; 03-20-2011 at 08:01 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed 's/Tb05.5K5.100/Tb229/' alone but doesn't work in sed file w/ other expressions Radha.jg Programming 6 03-03-2011 07:59 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration