Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Pattern and then sub the next 3 or 4 lines 3

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All, PHV

What I need is to find a pattern 128 then count 32 lines read $2 which is 20060212092325
prompt the user to which date would he like to change to, change the date to the reply but
at the same time read reply then substr date into :
Y_R=substr(reply,1,4)
M_R=substr(reply,4,2);gsub("^0"," ",M_R)
D_R=substr(reply,7,2);gsub("^0","",D_R)
H_R=substr(reply,10,2);gsub("^0","",H_R)
M_R=substr(reply,1,2);gsub("^0","",M_R)
S_R=substr(reply,1,2);gsub("^0","",S_R)

go back 8 lines which is "2006 D" and sub {$1=Y_R;$2="D"} go to the next line "2 D" and
{$1=M_R;$2="D"} and so on until the S_R the search again

File_Example
=============

128 A
8 12301089 A
1 1 A
1 2 A
1 3 A
1 0 A
1 1 A
1 0 A
1 8 A
1 9 A
0 A
0 A
0 A
0 A
0 A
0 A
0 A
0 A
0 D
0 D
0 D
0 A
0 M
1139729005 0 D
2006 D
2 D
12 D
9 D
23 D
25 D
0 D
1 A
14 20060212092325 A
10 0000038050 A


Code from PHV
=====

awk -v opt=24 -v Num=6 -v Num2=$3 -v Num3=D 'BEGIN{tty="/dev/tty"}
/^128 A/||/^248 A/||/^129 A/{a=NR}
a&&NR==(a+opt){
printf "Line %d\n======\n%s : Please enter New Date-->",opt,$1 > tty
if((getline reply<tty)>0){
Y_R=substr(reply,1,4)
M_R=substr(reply,4,2)#;gsub("^0"," ",M_R)
D_R=substr(reply,7,2);gsub("^0","",D_R)
H_R=substr(reply,10,2);gsub("^0","",H_R)
M_R=substr(reply,1,2);gsub("^0","",M_R)
S_R=substr(reply,1,2);gsub("^0","",S_R)
if (reply){
{$1=Y_R;$2="D"};
{$1=M_R;$2=D}
{$1=D_R;$2=D}
{$1=H_R;$2=D}
{$1=M_R;$2=D}
{$1=S_R;$2=D}
{$1=slen=length(tty);$2=tty}
#printf "%d After: %s\n",NR,$0 >"change.log"
}
}
}
{buf[NR]=$0}
END{for(i=1;i<=NR;++i)print buf}
' $2 > $2.txt


Many Thanks
Chris
 
You may try something like this:
awk -v opt=32 '
BEGIN{tty="/dev/tty"}
/^128 A/||/^248 A/||/^129 A/{a=NR}
a&&NR==(a+opt){
printf "Line %d\n======\n%s : Please enter New Date-->",opt,$2 > tty
if((getline reply<tty)>0){
if (length(reply)==14){
$2=reply
buf[NR-8]=sprintf("%d D",substr(reply,1,4))
buf[NR-7]=sprintf("%d D",substr(reply,5,2))
buf[NR-6]=sprintf("%d D",substr(reply,7,2))
buf[NR-5]=sprintf("%d D",substr(reply,9,2))
buf[NR-4]=sprintf("%d D",substr(reply,11,2))
buf[NR-3]=sprintf("%d D",substr(reply,13,2))
}
}
}
{buf[NR]=$0}
END{for(i=1;i<=NR;++i)print buf}
' $2 > $2.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi Again PHV,

Many Thanks, it works perfectly. One question, so to read backwards I can use the buffer ( buf[NR-5])

Thanks
Chris
 
Code:
awk '
{ a[NR] = $0 }

END {
  for (i=1; i in a; i++)
  { if ( a[i] ~ /^128 / )
    { here = i + opt
      $0 = a[here]
      printf "Old date: %s\nEnter new date: ", $2  >"/dev/stderr" 
      getline r  <"-"
      $2 = r ; a[here] = $0
      for (j=here-8; j<here-2; j++)
        r = change( a, j, r )
    }
    print a[i]
  }
}

function change( ary, i, str,     len )
{ len = ( length(str)==14 ? 4 : 2 )
  ary[i] = 0 + substr( str, 1, len ) " D"
  return substr( str, len + 1 )
}'  opt=32 infile >outfile
 
Hi Futulet,

Thanks, nice way to put it. One question, could you explain the following

len = ( length(str)==14 ? 4 : 2 )


Many Thanks
Chris

 
One question, could you explain the following

len = ( length(str)==14 ? 4 : 2 )

This is a shortcut that is also available in C and in Ruby.

It's equivalent to
Code:
if ( length(str)==14 )
  len = 4
else
  len = 2
The format is:
[tt]<boolean> ? <value 1> : <value 2>[/tt]
If the boolean value is true, value 1 is returned; otherwise, value 2 is returned.
 
That ?: is a C-style conditional operator, in essence it is a shortcut for an if/else:


len = length(str)==14 ? 4 : 2

is equivalent to

if (length(str)==4)
len=4;
else
len=2;

set len to 4 if str is 14 chars long, else set len to 2


HTH,

p5wizard
 
Hi All,


Thanks A lot. A pleasure to be a member on this forum. I know I can learn a lot from yours.

Thanks
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top