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
forums to hire unix experts itmgr Shell Programming and Scripting 3 05-08-2007 07:06 PM
Calling All Aix Experts Courtney3216 AIX 0 03-17-2006 05:30 PM
Shell Coding question for any experts out there dfran1972 Shell Programming and Scripting 4 01-12-2004 06:04 AM
Help from lint experts needed amatsaka High Level Programming 2 03-29-2002 05:42 AM
Experts Only! Hard Question Ahead!!!! Foo49272 UNIX for Advanced & Expert Users 1 01-07-2002 07:22 PM

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

Join Date: Aug 2005
Posts: 23
Need help - from awk, sed experts

Hi ,

I have a file as below.

Contents of the file are
--------------------
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
bbbbbbbbbbb
ccccccccccc
ddddddddddd

aaaaaaaaaaa


I want to do the follwing

if exists "ccccccccccc"
get
"bbbbbbbbbbb"
"ccccccccccc"
"ddddddddddd"
fi


Can someone help me in doing this using awk/sed..

thanks
srini
Reply With Quote
Forum Sponsor
  #2  
Old 12-06-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
Hard to do with sed, but this seems to work on HP-UX. The script needs one arg which string to find.
Code:
#! /usr/bin/ksh

sed -n -e ":l" -e "h;n;s/$1/$1/;t m" -e "b l" -e ":m" -e "H;n;H;g;p"
exit 0
Reply With Quote
  #3  
Old 12-06-2005
Registered User
 

Join Date: Aug 2005
Posts: 23
Hi Perderabo,

will it work in Linux ..

can you please let me know , how to run this .

Assumig file name is abc.txt and the search string is "ccccccc"


is it correct

./script.sh abc.txt



Thanks
Srini
Reply With Quote
  #4  
Old 12-06-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,667
./script.ksh ccccccccc < abc.txt

EDIT:

On linux, you may not have /usr/bin/ksh. I think it should work with bash. Change first line to a shell you have
#! /bin/bash
or whatever.
Reply With Quote
  #5  
Old 12-07-2005
Registered User
 

Join Date: Aug 2005
Posts: 23
Hi Perderabo,


It works fine .

But can you please explain me what exactly happening here ,

sed -n -e ":l" -e "h;n;s/$1/$1/;t m" -e "b l" -e ":m" -e "H;n;H;g;p"

and what if,

if I want to get 3 lines before the search string and one line after the search string .


i.e
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
kkkkkkkkkkk
llllllllllllllllllllllllll
tttttttttttttt
bbbbbbbbbbb
ccccccccccc
ddddddddddd

aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
aaaaaaaaaaa
kkkkkkkkkkk
llllllllllllllllllllllllll
tttttttttttttt
bbbbbbbbbbb
ccccccccccc
ddddddddddd

aaaaaaaaaaa
aaaaaaaaaaa
kkkkkkkkkkk
llllllllllllllllllllllllll
tttttttttttttt
bbbbbbbbbbb
ccccccccccc
ddddddddddd
aaaaaaaaaaa





Thanks
srini
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 05: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