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

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-06-2005
Srini75 Srini75 is offline
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
  #2 (permalink)  
Old 12-06-2005
Perderabo's Avatar
Perderabo Perderabo is online now Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,126
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

  #3 (permalink)  
Old 12-06-2005
Srini75 Srini75 is offline
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
  #4 (permalink)  
Old 12-06-2005
Perderabo's Avatar
Perderabo Perderabo is online now Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,126
./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.
  #5 (permalink)  
Old 12-07-2005
Srini75 Srini75 is offline
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
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 10:17 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