Filter unwanted lines

Hi All,

I have the below input and i only want to filter out some un-wanted info from here. Expected output is below. Can somebody help ?

The catch is that i want to grep those lines with term "k=" and lines with term "**" as the 1st column and "07" as the last column. And the number of unwanted lines in between the wanted lines are random. On top of that, the number of field of the wanted lines are also random.
Expected output shown below.

Input:
aaa k= aaaa
dddd
dddd
bbb k= cccc
zzz
zzz
zzzzz
ggg k= pppp
vvvvvv
vvv
v
vvvvv
vvvvvv
** XXXXX ZZZ VVV 07
** ccc kkk lll
** ggg lll ppp
** hhh iii kkk
xxxxxx
mmm k= qqqq
xxxxx
ddd
eee k= yyyy
ffff
ff
rrr k= vvvv
ttt
gggggg
zzzzz

Expected Output:
aaa k= aaaa
bbb k= cccc
ggg k= pppp
** XXXXX ZZZ VVV 07
mmm k= qqqq
eee k= yyyy
rrr k= vvvv

sed -n -e "/k=/p" -e "/^\*\*.*07$/p" input.txt

Hi Vino,

I encounter some error. Can you help?

$ sed -n -e "/k=/p" -e "/^\*\*.*07$/p" inputfile
Variable syntax
$

I guess you are using csh.

Try this.

sed -n -e "/k=/p" -e '/^\*\*.*07$/p' inputfile

See this thread as well - sed command in c shell

HI Vino,

Yeah i am using csh.
Thanks it really works!!
You are such a pro at this!!

But i am not familiar with sed. Can you explain you code so that i can understand better ?

sed -n -e "/k=/p" -e '/^\*\*.*07$/p' inputfile

sed's default behaviour is to print out the pattern space it reads. Turn it off with the -n flag.

-e "/k=/p" Look for all lines containing the pattern k= and then print those lines.

-e '/^\*\*.*07$/p' Look for all lines starting with ** and ending with 07 and the print those lines. The ^ is a placeholder for the start of a line and $ is likewise for the end-of-line.

We need to check for actual ** and so escape it as \*\*.

Hi Vino,

Thanks for the explanation.
But what does the red dot *.*07$/ signify ?

. stands for any character. A .* would stand for any number of characters.

Hi Vino,

That's some new and gd knowledge for me!!
Thanks alot for sharing ! :smiley:

Hi Vino,

If the file happens to be below (as the input) after using the sed earlier, i would want to cut out those with ABC in the first 3 characters of the 2nd field of those lines with "** ABCXX ZZZ VVV 07" pattern
as shown below and save as another filename. However the number of lines in between the ** pattern is random , fields of such patterm " bbb k= cccc" are random as well.
Can you help me ?

Input:

aaa k= aaaa
bbb k= cccc
ggg k= pppp
** XXXXX ZZZ VVV 07
mmm k= qqqq
eee k= yyyy
rrr k= vvvv
** ABCXX ZZZ VVV 07
mma k= qqqq
eer k= yyyy
rrq k= vvvv
** XXXXX ZZZ VVV 07
mgm k= qqqs
eae k= yyys
ppp k= vvvs
** ABCXX ZZZ VVV 07
mma k= qqqq
ear k= yyyy
raq k= vvvdv
** XXXXX ZZx VVx 07
mma k= qqqq
ear k= yyyy

Expected Ouput

** ABCXX ZZZ VVV 07
mma k= qqqq
eer k= yyyy
rrq k= vvvv
** ABCXX ZZZ VVV 07
mma k= qqqq
ear k= yyyy
raq k= vvvdv

You should always experiment a little. That is the best learning.
This may help

sed -n -e "/k=/p" -e '/^\*\* ABC.*07$/p' inputfile > outfile

Hi Vino,

Thanks for lending a hand.
But it seems that all the lines with "k=" are appended in which some are unwanted. Pls help me .

Can you show your input, the output and the expected output ? I dont understand when you say 'k= are appended in which some are unwanted'.

Hi Vino,

My output is below

$ sed -n -e "/k=/p" -e '/^\*\* ABC.*07$/p' input
aaa k= aaaa
bbb k= cccc
ggg k= pppp
mmm k= qqqq
eee k= yyyy
rrr k= vvvv 
** ABCXX ZZZ VVV 07
mma k= qqqq
eer k= yyyy
rrq k= vvvv 
mgm k= qqqs
eae k= yyys
ppp k= vvvs 
** ABCXX ZZZ VVV 07
mma k= qqqq
ear k= yyyy
raq k= vvvdv 
mma k= qqqq
ear k= yyyy
$

But what i need is

** ABCXX ZZZ VVV 07
mma k= qqqq
eer k= yyyy
rrq k= vvvv 
** ABCXX ZZZ VVV 07
mma k= qqqq
ear k= yyyy
raq k= vvvdv 

In other words i only want those data in blue. Pls help me.

aaa k= aaaa
bbb k= cccc
ggg k= pppp
** XXXXX ZZZ VVV 07
mmm k= qqqq
eee k= yyyy
rrr k= vvvv
** ABCXX ZZZ VVV 07
mma k= qqqq
eer k= yyyy
rrq k= vvvv
** XXXXX ZZZ VVV 07
mgm k= qqqs
eae k= yyys
ppp k= vvvs
** ABCXX ZZZ VVV 07
mma k= qqqq
ear k= yyyy
raq k= vvvdv
** XXXXX ZZx VVx 07
mma k= qqqq
ear k= yyyy

The pattern looks like the following three lines after '** AB.*07'

sed -n -e '/^\*\* ABC.*07$/{N;N;N;p}' input

Hi Vino,

The actual data has random number of lines so we can't take those as reference
but there's a fixed number of fields for "mma k= qqqq".
This is tough to tackle.