LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-25-2012, 10:46 AM   #1
radhikamody
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Rep: Reputation: Disabled
how to extract numbers from a word using sed


my string is
$$INPUT_FILE_DATE=201202

i want to extract 201202 from it and replace it with "last"

cat test | sed '/^\$\$INPUT_FILE_DATE=/s/[0-9]{6}/last/' | echo test

i tried this but it is not working

how do i search for [0-9]{6} in this string.
 
Old 02-25-2012, 11:13 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
The problem is in the type of regular expression syntax you tell sed that you're using. There's an option to sed (-r) that tells sed to use extended regular expressions. In brief, without the -r, most regular expressions treat text literally--characters have to be escaped to imbue their "special meaning." The opposite approach is used when -r is present: characters have their "special meaning" by default--unless they are escaped.

Here is your command using both styles:

basic regular expression (no -r) :
Code:
user@localhost$ echo '$$INPUT_FILE_DATE=201202' | sed '/^\$\$INPUT_FILE_DATE=/s/[0-9]\{6\}/last/'
$$INPUT_FILE_DATE=last
extended regular expressions (with -r):
Code:
user@localhost$ echo '$$INPUT_FILE_DATE=201202' | sed -r '/^\$\$INPUT_FILE_DATE=/s/[0-9]{6}/last/'
$$INPUT_FILE_DATE=last
As you can see, the basic regular expression does not follow the rule I described 100% (e.g. the square brackets form a character set without having to escape them). Search the web for basic and extended regular expressions to read about all the differences.
 
1 members found this post helpful.
Old 02-25-2012, 11:29 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
The other point to make would also be that you do not have to single out the numbers or the number of numbers but just replace everything after the equals sign:
Code:
echo '$$INPUT_FILE_DATE=201202' | sed 's/=.*/=last/'
Of course there may be other equal signs to watch out for, but you get the general idea.
 
Old 02-25-2012, 11:39 AM   #4
radhikamody
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
Dark_Helmet: thanks a lot it worked. din know about the extended regex..
 
Old 02-25-2012, 11:41 AM   #5
radhikamody
LQ Newbie
 
Registered: Feb 2012
Posts: 7

Original Poster
Rep: Reputation: Disabled
@grail: i have multiple lines with = signs in the file. So it will not be advisable to rewrite each line in the search part
 
Old 02-26-2012, 12:05 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Quote:
Originally Posted by radhikamody View Post
@grail: i have multiple lines with = signs in the file. So it will not be advisable to rewrite each line in the search part
Just address the command properly so that it only modifies the lines you want.

Code:
sed '/INPUT_FILE_DATE/ s/=.*/=last/'
Here are a few useful sed references.
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
sed append word at end of line if word is missing franjo124 Linux - Newbie 3 03-08-2012 08:41 PM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM
[SOLVED] Delete range word to word with sed when all you have is one line subby80 Linux - Enterprise 16 09-02-2010 05:04 AM
[SOLVED] How to extract the first word following a string Feynman Linux - Newbie 24 08-24-2010 12:27 PM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM

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

All times are GMT -5. The time now is 04:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration