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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help needed please. jerryboy78 UNIX for Dummies Questions & Answers 3 03-16-2008 11:06 AM
help needed nnayagam Shell Programming and Scripting 2 03-07-2008 03:34 AM
Little help needed. Netghost AIX 5 08-10-2006 11:29 AM
Help needed dsravan Shell Programming and Scripting 2 07-20-2006 06:37 AM
awk help needed. cskumar Shell Programming and Scripting 0 07-20-2006 04:24 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Question Sed help needed

Could someone tell me how to replace varibles using SED inside Korn Shell?

e.g. I have a ksh file program.ksh below:
------------------------------------
#!/bin/ksh

sed -n '/ABC/p' $1 > output.txt

if [[ $2 = AAA ]]
then
status=new
elif [[ $2 = BBB ]]
then
status=old
fi

sed -n '/$status/p' $1 >> output.txt
------------------------------------


I have a text file called filename.txt with the contents below:
------------------------------------
DDD old
CCC new
ABC old
------------------------------------

When I run program.ksh with filename.txt and BBB as input I want output.txt to contain the two lines below but with the current program it only outputs "ABC new".

DDD old
ABC new


I guess $status is not being recognized as a varible but how do I get this work? I tried putting "\" in front of $status but it didn't work.

Any help will be greatly appreciated.

Last edited by stevefox; 12-02-2005 at 12:42 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 12-02-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
i wonder how you would be arriving at
DDD old
ABC new <-- this line

there is no such file entry as ABC new

> cat filename.txt
DDD old
CCC new
ABC old


here's the modification

Code:
> cat program.ksh

#!/bin/ksh

sed -n '/ABC/p' $1 > output.txt
# ABC old
if [[ $2 = AAA ]]
then
status=new
elif [[ $2 = BBB ]]
then
status=old
fi

sed -n "/$status/p" $1 >> output.txt
#DDD old
#ABC old
>cat output.txt
ABC old
DDD old
ABC old
Reply With Quote
  #3  
Old 12-02-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Smile

Thanks matrixmadhan!
It worked!
Yes, you are right about "ABC new" line. It should have been "ABC old".
Thank you for your quich response!

cheers
Steve
Reply With Quote
  #4  
Old 12-02-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Question

I have a different question on sed.
How do you delete everything after a particular string using sed?

e.g. I have a file with three lines below:

A100 ;
B2000 ; B2000
C30 ; C30 ; C30

I want to delete everything after ";" to become like below:

A100
B2000
C30

Any help will be greatly appreciated.
Reply With Quote
  #5  
Old 12-02-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Code:
[~/temp]$ echo "A100 ; 
B2000 ; B2000
C30 ; C30 ; C30" |  sed -e 's/;.*//g'
A100 
B2000 
C30 
[~/temp]$
Reply With Quote
  #6  
Old 12-04-2005
Registered User
 

Join Date: Aug 2005
Posts: 111
Thanks vino!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0