Shell script to filter certain lines from a file

hi~
i need script on AIX. and have a text file following :

create aa
1
2
3
from a@a;

create bb
from b;

create cc
3
4
5
6
6
7
from c@c;

I want to find the create statement that contains the '@' character as bellow

create aa
from a@a;
create cc
from c@c;

anybody help me.

Any attempts / ideas / thoughts from your side?

When you say "AIX" you perhaps imply the usage of ksh . ksh (which is a ksh88) or ksh93 ? Or does it not matter to you?

bakunin

Note that if you keep coming to us with requests for us to write code for you with you not attempting to write any code on your own, we're likely to lose interest in helping you. We are here to help you learn how to do this stuff on your own; not to act as your unpaid programming staff.

I would be tempted to try something like:

#!/bin/ksh
awk '
/^create / {
	c = $0
}

/^from .*@/ {
	print c
	print
}' "$@"

Store the above in a file named whatever you want your utility to be named and make it executable in a directory in your search path (i.e. one of the directories named in the environment variable PATH ).

If you name your utility scriptname , you can then invoke it any of the following forms:

cat pathname... | scriptname
scriptname pathname...
scriptname < pathname

where pathname... can be replaced with the pathname(s) of one or more files you want to process. (Note that there is no ... in the last form (and only one pathname can be processed using that form).

4 Likes

Hi, Don Cragun
Thank you so much, i solved it with your help.

--- Post updated at 02:27 AM ---

Don Cragun,
i need your help more, i want to put one line on your suggestion.
is it possible?

I can put thousands of lines on my suggestion. Is there something in particular that you want the line that you put on my suggestion to do? Or should I just make something up for the fun of it? My crystal ball is a bit cloudy today and I'm not getting a clear view of what you might want it to do.

hi,
i solved it, i have one the following.

#>cat test.txt | awk '/CREATE / {c = $0}/[from|FROM] .*@/ {print c;   print}' "$@"

Thank you.

You're welcome.

Wow! That wasn't at all what I was expecting. From your sample data, I thought you were looking for lowercase words create and from at the start of a line of input. But instead you are looking for an uppercase CREATE followed by a <space> anywhere on a line and are looking for any of the characters f , F , m , M , o , O , r , R , or | followed by a <space> followed by any number of characters that includes an @ .

What output would you hope to get from the input file:

reCREATE xyzzy
| @
f @
O x@y
r v@w
from: a@b

Does it surprise you that the only line in the above sample that is not printed at least once by your code is the last line? Is that what you want your code to do?

2 Likes