Operating System - HP-UX
1756435 Members
2927 Online
108847 Solutions
New Discussion юеВ

Redirect output to different filenames help

 
SOLVED
Go to solution
Chris Frangandonis
Regular Advisor

Redirect output to different filenames help

Hi

Once again I really need your assistance with this awk script:

In the below script you will notice that I am altering certain fields within a file but would like to output to different filename acorrding to the keys for each record for e.g

The record starts at "3 CDR b" and finishes where the next "3 CDR" occurs. If the file has 3 records and are not the same in the Key fields then the result should have 3 output files

The Keys are line 70 (textpad) if its equal to B or I or F and line 72 =215012407" then group them in one file. If they do
not match then the result should be in 2 serpated files.

555 20080716143045 20080716150045 0 1800 0126675266 R Ma B 0 215012407 BCR7WB PBNHAB rjb+:01.ig17.0002363.03626344.20080716160007
555 20080716151744 20080716153045 0 781 0126675266 R Ma F 0 215012407 BCR7WB PBNHAB rjb+:01.ig17.0002363.03626344.20080716160007

SCRIPT
======
TOOL=`/usr/bin/which TimePrint`

awk -v Tool=$TOOL '{
opt=115
ReC="3 CDR"
ReV="S7"
Del="12 DeliveryData"

{if($0~ReC)
{
a=NR
opt2=3
}
}
}

a&&NR==(a+opt2){
if($1~ReV)
{
FLAG=1
}
else
{
FLAG=0
}
}

a&&NR==(a+opt){
split(buf[NR-47],PCI," ")
split(buf[NR-49],DUR," ")
if(FLAG==1 && PCI[2]=="F")
{
#Sub 1800 from Disc_D_t to be Start
split(buf[NR-79],DT_Unix," ")
Total_S_t=sprintf("%d",DT_Unix[1]-1800)
cmd=Tool" "Total_S_t" | grep Local"
cmd | getline ST_TIME; close(cmd);
split(ST_TIME,St_Dt," ")
Ful_St_Dt=substr(St_Dt[3],1,4)substr(St_Dt[3],6,2)substr(St_Dt[3],9,2)substr(St_Dt[4],1,2)substr(St_Dt[4],4,2)substr(St_Dt[4],7,2)

#Add Dur to be Final Date
split(buf[NR-9],DT_Unix," ")
Total_D_t=sprintf("%d",Total_S_t+DUR[1])
cmd_d=Tool" "Total_D_t" | grep Local"

buf[NR-9]=sprintf("%d 0 D",Total_S_t) # St_D_t
buf[NR-8]=sprintf("%d D",int(substr(St_Dt[3],1,4))) # St_Y
buf[NR-7]=sprintf("%d D",int(substr(St_Dt[3],6,2))) # St_M
buf[NR-6]=sprintf("%d D",int(substr(St_Dt[3],9,2))) # St_D
buf[NR-5]=sprintf("%d D",int(substr(St_Dt[4],1,2))) # St_H
buf[NR-4]=sprintf("%d D",int(substr(St_Dt[4],4,2))) # St_M
buf[NR-3]=sprintf("%d D",int(substr(St_Dt[4],7,2))) # St_S
buf[NR-2]=sprintf("0 D") # St_MM
#buf[NR-0]=sprintf("%d %s D",length(Ful_St_Dt),Ful_St_Dt)

#DIS Date & Time
buf[NR-79]=sprintf("%d 0 D",Total_D_t) # Dt_D_t
buf[NR-78]=sprintf("%d D",int(substr(Dt_Dt[3],1,4))) # Dt_Y
buf[NR-77]=sprintf("%d D",int(substr(Dt_Dt[3],6,2))) # Dt_M
buf[NR-76]=sprintf("%d D",int(substr(Dt_Dt[3],9,2))) # Dt_D
buf[NR-75]=sprintf("%d D",int(substr(Dt_Dt[4],1,2))) # Dt_H
buf[NR-74]=sprintf("%d D",int(substr(Dt_Dt[4],4,2))) # Dt_M
buf[NR-73]=sprintf("%d D",int(substr(Dt_Dt[4],7,2))) # Dt_S
buf[NR-72]=sprintf("0 D") # Dt_MM
buf[NR-70]=sprintf("%d %s A",length(Ful_Dt_Dt),Ful_Dt_Dt)
}
}

{buf[NR]=$0}
END{
for(i=1;i<=NR;++i)print buf[i] }' $1 > $1.Final



I really appreciate your assistance

Thanks
chris
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Redirect output to different filenames help

Hi CHris:

? but would like to output to different filename acorrding to the keys

By example:

# awk 'BEGIN{for(i=1;i<=3;++i) {print i > i".Final"}}'

...would create three different files, named "1.Final", "2.Final" and "3.Final" containing the lines with 1, 2 and 3 respectively. Does that help?

Regards!

...JRF...
Chris Frangandonis
Regular Advisor

Re: Redirect output to different filenames help

Hi JRF,

Maybe I dont understand.

In the script I do the altering in the front which must be done and when I get to the "END" I need to buffer out the result (changes done) but in that step I need to start checking for the 3 keys and then re-direct them to either a file or different filnames. e.g. The filename must contain


3 CDR b
J
214 C
S7 A
1 G D
1 D
0 D
0 D
0 D
0 D
0 D
0 D
0 D
etc.....
till the next "3 CDR b"
read next "3 CDR b"
and if keys "B or I or F" and line 72 =215012407" is true to the previous records, keep in the same file and so on

Hope this make sence

Thanks
Chris

Hein van den Heuvel
Honored Contributor
Solution

Re: Redirect output to different filenames help

Ah, I think I see what you want.
There is a fixed number of (short) lines after each '3 cdr' and you need to change some before printing that section.

correct?

Let's take a more simple input:

-------------- test.txt ----------------
1
2
3
test
4
5
6
7
8
9
10
test
6
7
8
a
a
a
test
p
q
r
s
t
u
v
w
x
---------------------------
We look for 'test', and assume 6 useful lines with the before-last one to be updated.

In line with you approach, the awk might look like

------ test ---------
#!/usr/bin/awk -f
BEGIN { my_match = "test"; }
{buf[NR]=$0}
$0 ~ my_match {
file_nr++;
file_start[file_nr]=NR;
}
NR==file_start[file_nr]+5 {
buf[NR-1]="updated";
}
END {
while (i++ < file_nr) {
file_name = "file_" i ".tmp";
for (l=0; l<6; l++) {
print buf[file_start[i] + l] > file_name;
}
}
}
----------------------------------


But why not process each chunk at a time, starting in the beginning of 'buf' for each new chunk?
That way you can use absolute line numbers, nor relative, and you dop not need to remember as much.

--------------- alternate awk --------

#!/usr/bin/awk -f
BEGIN { my_match = "test"; }
{ buf[line++]=$0
}
$0 ~ my_match {
line = 0;
file_nr++;
file_name = "file_" file_nr ".tmp";
}
line == 5 {
buf[5]="updated for file " file_nr;
for (l=0; l<6; l++) {
print buf[l] > file_name;
}
}
----------------------------------------

hth,
Hein.



Chris Frangandonis
Regular Advisor

Re: Redirect output to different filenames help

Hi Hein,

Welkom back. I always new that you would be the one to help me. Much appreciated it

When you specify "6" , does this mean that I need to count the lines from "3 CDR" till the next "3 CDR" - 1 line?
I also need to group each "test" cdr when "6" is the same, into one file for e.g.
1
2
3
test
4
5
6
7
8
9
10
test
6
7
6
a
a
a
test
p
q
r
s
t
u
v
w
x

Result of file_1.tmp should contain
=====
test
4
5
6
7
8
9
10
test
6
7
6
a
a
a

and Result of file_2tmp should contain
test
p
q
r
s
t
u

Does this make sence

Thanks Chris
Chris
Chris Frangandonis
Regular Advisor

Re: Redirect output to different filenames help

Hi Hein,

Sorry to be of nuisance , your script works fine but I still need to group into one file if in the second "test" has the same keys then append it to the first file

#!/usr/bin/awk -f
BEGIN { my_match = "test"; }
$0 ~ my_match {
file_nr++;
file_name = "test_" file_nr ".tmp" ;
print file_nr, file_name
}
file_nr > 0 {
print $0 > file_name
}

for e.g.

1
2
3
test
4
5
6 -------->
7
8
9
10
test
6
7
6 --------->
a
a
a
test
p
q
r
s
t
u
v
w
x

Result of file_1.tmp should contain
=====
test
4
5
6 --------->
7
8
9
10
test
6
7
6 ---------->
a
a
a

and Result of file_2.tmp should contain
test
p
q
r
s
t
u

This is the last one favour

Thanks
Chris