LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2009, 04:40 PM   #1
dmason165
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Rep: Reputation: 0
Substitue single-digit, two-digit, and 3-digit numbers with text using sed


Hi,

I'm trying to write a sed script that will evaluate a file with exit codes, and substitutes the numerical exit codes (which are single-, two-, or three-digit numbers) for their description.

The process goes like this:

I have a script that obtains an exit code/status for backups and dumps the output in a file. We'll call the file bkp_status. Entriess in bkp_status are in this format:

server1,exit_code
server2,exit_code
server3,exit_code
.
.
.

I have tried for a couple days now to pipe this file into a sed script that will substitute the exit_code for its description, and generate output as follows:

server1,exit_code,(exit_code_description)
server2,exit_code,(exit_code_description)
server3,exit_code,(exit_code_description)
.
.
.

The problem I keep running into is that sed will not evaluate two- or three-digit numbers as a single number. In other words, this input:

server1,86

piped through these sed commands:

s/,6/,6,(the backup failed to back up the requested files)/
s/,8/,8,(unable to determine the status of rbak)/
s/,86/,86,(media position error)/


returns this:

server1,8,(unable to determine the status of rbak)6

I have tried surrounding the numbers in brackets, parens, single-quotes, double-quotes, escaping them, all to no avail.

Any suggestions?

Thanks in advance.

Last edited by dmason165; 08-05-2009 at 04:44 PM.
 
Old 08-05-2009, 05:11 PM   #2
Berhanie
Senior Member
 
Registered: Dec 2003
Location: phnom penh
Distribution: Fedora
Posts: 1,625

Rep: Reputation: 165Reputation: 165
better to match the end of the line with '$':
Code:
s/,6$/,6,(the backup failed to back up the requested files)/
s/,8$/,8,(unable to determine the status of rbak)/
s/,86$/,86,(media position error)/
 
Old 08-05-2009, 06:29 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
SED has no capacity to evaluate numbers---it works strictly on strings. Before you start piping commands together, test them one at a time to make sure they are doing what you need.

Really good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 08-05-2009, 07:45 PM   #4
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
Why not use awk? Something like:
Code:
BEGIN {
	FS=","
	OFS=","

	# ecd - exit code description
	ecd1 = "exit code #1"
	ecd2 = "exit code #2"
	ecd3 = "exit code #3"
}

$2 ~ /exit_code1/ {
	print $0 , ecd1
}

$2 ~ /exit_code2/ {
	print $0 , ecd2
}

$2 ~ /exit_code3/ {
	print $0 , ecd3
}
in your.awk, then use with:
Code:
awk -f your.awk bkp_status

Last edited by joeBuffer; 08-05-2009 at 07:59 PM. Reason: I added the / / to the exit_code# parts. I left them off on accident.
 
Old 08-05-2009, 07:48 PM   #5
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
Of course, this is assuming your file is layed out like what you're showing, only written differently.
For example:
Code:
server1,exit_code1
server2,exit_code2
server3,exit_code1
server4,exit_code3
the output is:
Code:
server1,exit_code1,exit code #1
server2,exit_code2,exit code #2
server3,exit_code1,exit code #1
server4,exit_code3,exit code #3
I added the numbers to the end of the exit codes in bkp_status, to more accurately represent what you're file looks like.

Last edited by joeBuffer; 08-05-2009 at 07:50 PM.
 
Old 08-05-2009, 07:51 PM   #6
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
It will work the same with a numerical exit status in bkp_status as it will with what I've used for the example.
 
Old 08-05-2009, 07:58 PM   #7
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
Damn, I posted it wrong at first. I just added the //'s around the exit_status# parts. You need those for a regular expression.

Last edited by joeBuffer; 08-05-2009 at 07:59 PM.
 
Old 08-05-2009, 08:05 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
joe;
It's better to edit (or add to one post than to keep adding new ones---but please don't lose your enthusiasm for helping people...
 
Old 08-05-2009, 08:20 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use awk instead of sed....

Code:
awk 'BEGIN{
    code["86"]="eighty6"
    code["87"]="eighty7"
    code["89"]="eighty9"
    OFS=FS=","
}
{
    print $1,$2,code[$2]
}' file
 
Old 08-05-2009, 08:27 PM   #10
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
The first three posts were really how I meant them, I thought they were sort of separate ... sorry about that.
Only the fourth one I added to draw more attention to the fact that I left out the //'s, just in case they maybe saw it quickly and tried it and had it do the wrong thing.

Last edited by joeBuffer; 08-05-2009 at 08:44 PM.
 
Old 08-05-2009, 08:30 PM   #11
joeBuffer
Member
 
Registered: Jul 2009
Distribution: Ubuntu 9.04
Posts: 328

Rep: Reputation: 42
That's basically what I just posted, ghostdog, only shorter. It would be better for space. I also wrote in my post "something like:" because I know it can be made with fewer lines, and maybe something else could come up that the original poster wants to do.

Last edited by joeBuffer; 08-05-2009 at 08:34 PM.
 
Old 08-05-2009, 08:59 PM   #12
dmason165
LQ Newbie
 
Registered: Aug 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you!

Thanks Berhanie, joeBuffer, and ghostdog74 for your posts!! VERY much appreciated. I will test them later tonight and report back with my findings.

pixellany, thanks for the link to grymoire.com. I've referenced it many times before for reminders here-and-there; it's one of the more popular and better-written tutorials on the Web. And FYI, I'm at the part in my script where the pipe didn't work, so I came here for assistance. As a moderator, and somewhat of a forum role-model, you should take care with your tone and suggestions. It may not be what you intended, but your post comes across like you're advising someone who has no clue what they're doing; such a post can be discouraging when it's directed toward someone who doesn't or offensive when it's directed toward someone who does.

Thanks again everyone!
~D
 
Old 08-07-2009, 08:22 AM   #13
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by dmason165 View Post
Thanks Berhanie, joeBuffer, and ghostdog74 for your posts!! VERY much appreciated. I will test them later tonight and report back with my findings.

pixellany, thanks for the link to grymoire.com. I've referenced it many times before for reminders here-and-there; it's one of the more popular and better-written tutorials on the Web. And FYI, I'm at the part in my script where the pipe didn't work, so I came here for assistance. As a moderator, and somewhat of a forum role-model, you should take care with your tone and suggestions. It may not be what you intended, but your post comes across like you're advising someone who has no clue what they're doing; such a post can be discouraging when it's directed toward someone who doesn't or offensive when it's directed toward someone who does.

Thanks again everyone!
~D
I am sorry, but my comments were:
1) a matter of fact statement about how SED does not evaluate numbers, and
2) a suggestion on how to unravel what was being attempted.

If you stick around here for a while, you'll note that many members have time available only in fragments-----you'll quite often see a terse suggestion or comment with more in-depth follow-up later.

Regardless, I'm glad to see you are getting somewhere and that you have the courtesy to thank those that help you.
 
Old 08-07-2009, 10:38 AM   #14
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
Try modifying your s commands like so:

Code:
s/,\<6\>/,6,(the backup failed to back up the requested files)/
s/,\<8\>/,8,(unable to determine the status of rbak)/
s/,\<86\>/,86,(media position error)/
HTH.
-RN.
 
  


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
Text editing: Adding a digit/text to the end of a row CHARL0TTE Linux - Newbie 13 07-16-2009 06:44 AM
how do i produce a single digit random number in BASH? jaepi Linux - Newbie 8 04-29-2007 11:54 PM
[BASH] Search for 5-digit numbers in document General Programming 2 01-10-2007 08:06 PM
54 digit numbers Scratchit General 3 06-23-2004 09:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 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