Need help - from awk, sed experts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help - from awk, sed experts
# 1  
Old 12-06-2005
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  
Old 12-06-2005
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  
Old 12-06-2005
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  
Old 12-06-2005
./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  
Old 12-07-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

2. Shell Programming and Scripting

To all the awk experts out there!

Hello All, I recently encountered this difficulty in processing a File. Input File has millions of records with fields like below ID1,ID2,DATE,FLAG,VAL 123,432,0604,1,-0.5 123,432,0604,22,0.5 123,433,0604,1,-0.54 123,433,0604,22,6.77 123,543,0605,22,0.94 To put this simply, I will have... (8 Replies)
Discussion started by: PikK45
8 Replies

3. Shell Programming and Scripting

Couple of easy questions for experts on awk/sed

Hello Experts.. I have 3-4 C codes with Oracle SQL statements embedded. All the SQL statements starts with EXEC SQL keyword and ends with ;. I want to extract all the SQL statements out of these codes. I did awk '/^EXEC SQL/,/\;/' inputFile (I use this on all of the codes individually). That... (2 Replies)
Discussion started by: juzz4fun
2 Replies

4. Shell Programming and Scripting

awk experts please help

I have a log file of 60 MB with 20k records which contains data like below. this contains some data so removed 2891358271000020, 2012-12-02 23:16:17 , 2012-12-02 23:16:17 , 378015123, 2012-12-02 23:16:19 , 2012-12-02 23:16:19 , (15 Replies)
Discussion started by: mirwasim
15 Replies

5. UNIX for Advanced & Expert Users

Experts!! please help me

Hi experts, Please help me on the below: how to write a shell script to search and delete files on windows server. -script runs on unix box -it should search for specific files on windows server and delete them periodically. (1 Reply)
Discussion started by: chpradeepch
1 Replies

6. Shell Programming and Scripting

awk question for experts

Hi guys I am trying to perform a substitution using 'awk' command, but it fails. I work in ksh. Here is my code: $ line="F 18:30 10 23:00 ts1632back" $ n="ts1632back" $ m="18:45" $ echo ${line} | nawk -v a=$n -v b=$m '{if ($5==a) $2=m; print }' F 10 23:00 ts1632back $It should've... (2 Replies)
Discussion started by: aoussenko
2 Replies

7. UNIX for Advanced & Expert Users

for experts

Hi i'm working with mpi programs every thing ok but i need after i compile the mpi to calc the area of the rectangle for example (not my program) accept two arguments from the command line: mpirun -n 4 myprog 24 100 here 24 and 100 two arguments i'll pass them to the program how can... (1 Reply)
Discussion started by: Scotch
1 Replies

8. Shell Programming and Scripting

usage of sed question for experts

I need a little help with sed. Basically, I need to parse out selections from the output of hddtemp so conky can display some hdd temps for me. I have hddtemp in daemon mode so A simple 'nc localhost 7634' displays the following: $ nc localhost 7634... (3 Replies)
Discussion started by: audiophile
3 Replies

9. UNIX for Dummies Questions & Answers

Looking for Advice from Experts

Where to start... I am a system administrator who didn't think he would ever be one. My first work was on Window 2000 as a tech(hardware, installs, stuff like that). Then we got Macs (I work in photography and Videography). The I was sent to Mac cert school. Set up a Mac Xserve and about 100... (2 Replies)
Discussion started by: Squidy P
2 Replies
Login or Register to Ask a Question