Operating System - HP-UX
1831103 Members
3112 Online
110020 Solutions
New Discussion

Need help with scripting control characters

 
SOLVED
Go to solution
Safdar Mustafa
Advisor

Need help with scripting control characters

Hi,

Not sure if this can be explained so well but I'll give it my best shot...

I need to write a shell script that would read a text file as input and output all entries in the file with a blank line between "circuit.." and "Type.." lines (or 2 carriage return characters before the line beginning with the word "Type").

Please look at the following lines as an example of the text file:

*************
circuit1
Type1

circuit2
Type2

circuit3

Type3

circuit4

Type4
*************

So here, I would want the script to output circuit3 and circuit4 since they have blank line gaps between ciruit and Type text.

I'm not too hot on pattern matching over multiple lines. Any advice will be appreciated.

Thanks
6 REPLIES 6
Ivan Ferreira
Honored Contributor

Re: Need help with scripting control characters

Try something like this:

INPUTFILE=
OUTPUTFILE=

cat $INPUTFILE |while read LINE; do
case $LINE in
Type*)
echo "" >> $OUTPUTFILE
echo "" >> $OUTPUTFILE
echo $LINE >> $OUTPUTFILE
;;
cir*)
echo $LINE >> $OUTPUTFILE
esac
done

Or you can use something like this

# File convert_sh:

cat $1 |while read LINE; do
case $LINE in
Type*)
echo "" >> $2
echo "" >> $2
echo $LINE >> $2
;;
cir*)
echo $LINE >> $2
esac
done

Usage: convert.sh
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor

Re: Need help with scripting control characters

Hi Safdar:

# cat .perl.sh
#!/usr/bin/perl
while (<>) {
if (/circuit/) {
$last=$_;
next;
}
if (/^\s+$/) {
print $last if defined $last;
}
if (/Type/) {
undef $last;
}
}

...run as:

# ./perl.sh filename

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor
Solution

Re: Need help with scripting control characters



$ awk '/^circuit/{b=0;c=$1} /^Type/&&(b==1){print c} (NF==0){b++}' your-file


or


$ perl -ne "if (/(circuit\d+)/){$c=$1;$b=0}; $b++ if /^$/; print "$c\n" if ($b==1 and /^Type/)" your-file


in both cases:

When 'circuit' is seen at the begin of a line, remember the first word in c, and clear the blank-line counter b.
When a blank line is seen, increment b
When 'Type' is seen at the begin, and the blank-line-counter is 1, then print.

Now if there can be no-blank lines, then it gets more tricky. You can then for example remember while line (NR in awk, $. in perl) the the first word was found, and check the rigth advance on the second word. For example:

awk '/^circuit/{n=NR;b=0;c=$1} /^Type/ && b==1 && NR==n+2 {print c} (NF==0){b++}" your-file.


or your can to the 'staged search approach' in shell or other script:

loop
if first condition then set stage-1
if stage-1 and second condition then set stage-2
if stage-2 and last condition then report.
clear stage on input and mismatch.


so many options...


cheers,
Hein.






Rodney Hills
Honored Contributor

Re: Need help with scripting control characters

Here is a perl one liner-
perl -ne 'do {$x=$_;$y=$.} if /^circuit/;print $x if /^Type/ and $.-$y>1' yourdata

HTH

-- Rod Hills
There be dragons...
Safdar Mustafa
Advisor

Re: Need help with scripting control characters

Excellent, thanks for response guys!!

I tested all of them and got Heins and Rodneys to work successfully.

Thanks all of you for your help...

Safdar
Sandman!
Honored Contributor

Re: Need help with scripting control characters

Yet another one-line sed construct you could use...

# sed -n '/^circuit/N;s/\n$//p' infile

cheers!