sed replace return code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replace return code
# 1  
Old 06-15-2012
sed replace return code

Hi

i have multiple sed replacements happening in my script and on those sed replacements, my requirement is to print a message if actually a replacement happened

for eg : if sed 's/abc/xyz/g' if [$replacement_return_code = 0 ]; then print "replacement happended from abc to xyz"

do we have any return codes like the above in sed to know if the actual replacement operation happened or not

please advise
# 2  
Old 06-15-2012
Hi
You can get this in perl:


Code:
$ echo "abc efg" | perl -ne 'print s/abc/ABC/; '
1


Substitution in perl returns the number of substitutions made. If none, it will not throw any result.

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 06-16-2012
thanks for the perl command, but
how to perform the below sed operations in perl and get return code

sed ''$start_replace_num',$s/^#[ ]*//g' // to replace all the "# " starting from nth line

sed 's/[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\} *$/'`date +"%Y-%m-%d"`'/g' // replace the date at the end of the line with the current date
# 4  
Old 06-16-2012
Hi

You can do something like this in your shell:


Code:
$ cat a
welcome
to india

$ sed 's/to/TO/' a > b

$ diff a b >/dev/null

$ echo $?
1

I have a file "a". The output of sed command sent to file b. On doing diff between these files, I get return code non-zero if they are different, 0 if they are same. Hope this should do for you.
# 5  
Old 06-16-2012
thanks ...this will work for me
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create Carriage Return using SED

I am hoping someone can help me with a solution to this problem using SED. My issue is actually two-fold. First, I have an order file that is sent to us from another system, however, there has been a change in current processes that requires a carriage return added to the end of each order. The... (5 Replies)
Discussion started by: chino_1
5 Replies

2. Shell Programming and Scripting

How do I use sed to return only the serial number here?

I'd appreciate the help and explaining "which each switch/command does. Thanks in advance. 1742@3min# ./fmtopo|grep serial hc://:product-id=SUNW,Sun-Blade-2500:server-id=c3admin:serial=130B58E3146/motherboard=0/cpu=0 (2 Replies)
Discussion started by: LittleLebowski
2 Replies

3. Shell Programming and Scripting

Replace carriage return with colon on specific column

Hi, I have to process four source text files with possibility to contain carriage return on the particular column. Thus, i need to replace the carriage return with 3 colons <:::> The particular column position in the source files is not fix but the name is fixed. That is, say for example,... (4 Replies)
Discussion started by: agathaeleanor
4 Replies

4. Shell Programming and Scripting

Return Number of Substitutions made by SED?

Hi guys, Is there any way this can be done, or return whether any substitutions have been made? thanks for any input. skinnygav (using Bash shell) (2 Replies)
Discussion started by: skinnygav
2 Replies

5. UNIX for Dummies Questions & Answers

use sed to replace whitespace with a carriage return

Greetings I need to replace "whitespace" in a file with the newline character aka carriage return My command is either wrong or not interpreted properly by me shell sed s/" "/\\n" "/g nets > nets1 or sed s/" "/\n" "/g nets > nets1 nets (input file) 13MHZ_IN... (4 Replies)
Discussion started by: awk_sed_hello
4 Replies

6. Shell Programming and Scripting

Delete carriage return in SED

Hi everybody! I'm working in one script with sed, I have file with the next content: <voms.db.type value="changeme"/> <voms.db.host value="changeme"/> <voms.admin.smtp.host value="changeme"/> <voms.mysql.admin.password value="changeme"/> <glite.installer.verbose value="true"/> ... (3 Replies)
Discussion started by: juedsivi
3 Replies

7. Shell Programming and Scripting

sed and character return problem

Hi, I have a problem with sed. It doesn't recognize the "\n" character. It substitudes an "n", instead of introducing a new line. This doesn't happend with print $ print "test \n \n" (it deos introduce two lines after hello) (AIX) $ sed s/bc/\n/g test.1 >test.2 $ cat test.1 bcdefg... (3 Replies)
Discussion started by: Santiago
3 Replies

8. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

9. Shell Programming and Scripting

How to delete carriage return in SED

Could someone tell me how to do the below without opening the file? (eg in sed or awk) I have a file with the contenst below: $ more file1.txt 10 AAA; 200 BBB; 3 CCC; I want to delete the carriage return of one line above the line which has ";" at the end to get the... (3 Replies)
Discussion started by: stevefox
3 Replies

10. Shell Programming and Scripting

sed removing carriage return and newline

Hi, I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this: sed -e "s#\#\#g" file.txt but it doesn't work. Thanx... (2 Replies)
Discussion started by: mored
2 Replies
Login or Register to Ask a Question