The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need solution concatenate and display 2 lines as 1 with a condition for 2 line ? vithala UNIX for Advanced & Expert Users 1 07-10-2008 02:27 PM
Linux shell Script not null checking IF condition kaushys UNIX for Dummies Questions & Answers 3 07-02-2008 06:54 AM
Is There a Sed Solution for This? racbern Shell Programming and Scripting 1 03-13-2008 11:31 AM
Checking condition inside the loop ithirak17 Shell Programming and Scripting 1 03-13-2008 08:37 AM
Condition checking trynew Shell Programming and Scripting 14 06-21-2002 12:41 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-21-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
sed solution for condition checking

Hi all ,

Recently i came across this in FAQ's.

I have a file


Code:
cat rem.txt
sreedhar 20
sreedhar 10
sreedhar 15
sreedhar 18
sreedhar 16
sreedhar 30

I have to replace sreedhar with "Sridhar" if the second parameter is > 18.

I need to do it in "sed" only. I am wondering how this can be done.
  #2 (permalink)  
Old 07-21-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,311
If you have gsed you can use egrep like syntax for matching multiple strings, ie.

Code:
(pattern1|pattern2|...)

If you want to stay compatible you can use:

Code:
sed -e '/ 19$/ {s/sreedhar/Sridhar/}' -e '/ [2-9][0-9]\+$/ {s/sreedhar/Sridhar/}' infile
Sridhar 20
sreedhar 10
sreedhar 15
sreedhar 18
sreedhar 16
Sridhar 30

In awk this would have been much easier and also with real arithmetics.
  #3 (permalink)  
Old 07-21-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Thanks zaxxon for the solution.

But it's not working as i am getting the error


Code:
Function / 19$/ { s/sreedhar/Sridhar/ } cannot be parsed.

Yes , i worked with awk and it's easy to do it in awk.

But sed's solution is seems to be cumbersome as we are not doing much like arithmetic operations.

Last edited by panyam; 07-21-2009 at 10:13 AM..
  #4 (permalink)  
Old 07-21-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
With a little modification to above script, you can do this...

Code:
sed -n '
/ 19$/ {
   s/sreedhar/Sridhar/
   p
   }
/ [2-9][0-9]$/ {
   s/sreedhar/Sridhar/
   p
   }' file



---------- Post updated at 07:02 PM ---------- Previous update was at 07:00 PM ----------

If the number is negative or greater than 99... We need to tweak a bit more.
  #5 (permalink)  
Old 07-21-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Thanks Rakesh ...

I tried in the same way ,but .. As i was messing "-e" with "-n" there were some errors i was getting.

sed always makes me "sad" :|
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0