The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Help needed please. jerryboy78 UNIX for Dummies Questions & Answers 3 03-16-2008 02:06 PM
help needed nnayagam Shell Programming and Scripting 2 03-07-2008 06:34 AM
Little help needed. Netghost AIX 5 08-10-2006 02:29 PM
Help needed dsravan Shell Programming and Scripting 2 07-20-2006 09:37 AM
awk help needed. cskumar Shell Programming and Scripting 0 07-20-2006 07:24 AM

Closed Thread
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 12-02-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
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 03:42 AM..
  #2 (permalink)  
Old 12-02-2005
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,952
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
  #3 (permalink)  
Old 12-02-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
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
  #4 (permalink)  
Old 12-02-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
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.
  #5 (permalink)  
Old 12-02-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Code:
[~/temp]$ echo "A100 ; 
B2000 ; B2000
C30 ; C30 ; C30" |  sed -e 's/;.*//g'
A100 
B2000 
C30 
[~/temp]$
  #6 (permalink)  
Old 12-05-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 114
Thanks vino!
Closed Thread

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 08:23 AM.


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