1831388 Members
3438 Online
110025 Solutions
New Discussion

Re: scripting question

 
SOLVED
Go to solution
soji
New Member

scripting question

Hi experts,
Im trying to write a script either in awk or in ksh to read in a text file with a format like shown below:
......start....
sht 2 3 4
trc 4 5 5
trc 3 3 2
trc 2 3 4
trc 3 4 1
trc 1 3 6
trc 3 3 5
trc 4 3 1
trc 2 3 5
sht 1 6 3
trc 4 5 5
trc 3 3 2
trc 2 3 4
trc 3 4 1
trc 1 3 6
trc 3 3 5
trc 4 3 1
trc 2 3 5
.........end.........
It is a really long file with similar format. All I want to do is to read in the file and print the line that starts with "sht" and every fourth line that starts with "trc"

Could somebody help me please

Thanks in advance
8 REPLIES 8
Robin Wakefield
Honored Contributor
Solution

Re: scripting question

Hi,

awk '/^sht/ || (NR%4 == 0 && /^trc/){print}' filename

should do the trick (if I interpret the trc requirement correctly).

rgds, Robin
Stefan Farrelly
Honored Contributor

Re: scripting question

let x=0
cat t|while true
do
read A B C D
[ "$A" = "" ] && exit
[ "$A" = "sht" ] && echo $A $B $C $D
if [ "$A" = "trc" ]
then
let x=$x+1
if [ $x -eq 4 ]
then
echo $A $B $C $D
let x=0
fi
fi
done

where t (on 2nd line in cat t) is the name of the input file with all your data in.
Im from Palmerston North, New Zealand, but somehow ended up in London...
soji
New Member

Re: scripting question

Thanks for the quick reply.
It is very close.

I actually want to print every fourth occurence of lines begining with "trc"

Thanks a lot again
Robin Wakefield
Honored Contributor

Re: scripting question

OK, well try:

awk '/^sht/{print}/^trc/{count++;if (count==4){print;count=0}}' file

this resets the counter after 4 occurrences of trc at the beginning of the line.

rgds, Robin
Steve Steel
Honored Contributor

Re: scripting question

Hi


file=$1
typeset -i x=1
cat $file|while read a b c d
do
case $a in
sht) echo $a $b $c $d ;;
trc) if [ "$x" -eq "4" ]
then
let x=1
echo $a $b $c $d
else
typeset -i x=$x+1
fi ;;
esac
done


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
H.Merijn Brand (procura
Honored Contributor

Re: scripting question

# perl -ne'/^sht/||(/^trc/&&++$t%4==0)and print' log
sht 2 3 4
trc 3 4 1
trc 2 3 5
sht 1 6 3
trc 3 4 1
trc 2 3 5


Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
soji
New Member

Re: scripting question

Thank you All for the replies and help...Especially Robin who was quick...I chose you second solution. By the way what does the NR%4 do in your first solution what does modulus do in awk?

thanks
Yogeeraj_1
Honored Contributor

Re: scripting question

hi,
FYI:

x % y
Remainder. The quotient is rounded toward zero to an integer, multiplied by y and this result is subtracted from x. This operation is sometimes known as "trunc-mod." The following relation always holds:
b * int(a / b) + (a % b) == a

One possibly undesirable effect of this definition of remainder is that x % y is negative if x is negative. Thus,
-17 % 8 = -1


hth
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)