Tag Archives: BEGIN

Count and print the number of occurences

By arch

I have some files as shown below

GLL ALK 654-656 654 656
SEM LYG 655-657 655 657
SEM LYG 655-657 655 657
ALM ARN 656-658 656 658
ALM ARN 656-658 656 658
ALM ARN 656-658 656 658
LEG LEG 658-660 658 660
LEG LEG 658-660 658 660

The value of GLL is 654. Th value of ALK is 656. In the same way, 4th column represents the values of first column. 5th column represents the values of second column.

I tried the following program to count the occurrences of each number in the fourth and fifth column.

for i in folder1/*.pdb;
do
awk ‘
BEGIN {
path=sprintf(“%s”, “/home/arch/Desktop/folder2/”)
}
!s[1″:”$4″:”$5]++{sU[$4]++;tot++}
!s[2″:”$4″:”$5]++{sU[$5]++;tot++}
END {
sub(/.*//,””,FILENAME)
for (x in sU)
print x, sU[x], sU[$1] > path FILENAME;
}’ $i;
done

The above program prints as follows

660 1
654 1
655 1
656 2
657 1
658 2

But I would like to get the output as follows

660 LEG 1
654 GLL 1
655 SEM 1
656 ALM 2
657 LYG 1
658 LEG 2

your suggestions would be appreciated!!

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

How to get result from both of these modules?

By bluemind2005

Hallo All unix Guru’s

I have written a small (static) pl/sql function which is inserting data into database. It is as follows:

Code:

sqlplus $CONNECT <<-EOF
DECLARE
Counter integer :=1;
BEGIN
WHILE Counter <= ${no_of_files} LOOP
INSERT INTO stuck_files(COL_DATE,SER_NAME,TYPE_FILE,FILENAME,FILE_STATUS) VALUES (sysdate,'pqrs','pqrs','pqrs','pqrs');
Counter := Counter + 1;
END LOOP;
END;
/
commit;
exit;
EOF


However the above “insert” needs to be more of dynamic in nature, as such you can see that we want to insert data depending upon the variable value ${no_of_files}.

Now let me show you another module which is giving me the file(s)names which I want to insert.

Code:

N=0
for i in $(find . -path */waiting/* -type f -not -name "SS*" -mmin +120 -print) ; do

testarray[$N]="$i"
echo "$i"

let "N= $N + 1"
done


Sample Output from above command is as follows:

Code:

/d1/d2/d3/d4/waiting/abcd.txt
/d1/d2/d31/d42/waiting/pqrs.txt
/d1/d2/d32/d43/waiting/xyz.txt


Now the data insert which is given in the above pl/sql block has directly to do with this output. It going to be dynamic in nature.

Let us come back to mapping of how we need to insert data based on this output. I will go through the first insert.2 insert and 3rd insert so you would get an idea of design.
But these are very dynamic, meaning the insert depends on the output produced but yes the directory structure would remain same.

Code:

COL_DATE,SER_NAME,TYPE_FILE,FILEFILENAME,FILE_STATUS
sysdate,d3,d4,abcd.txt,waiting
sysdate,d31,d42,pqrs.txt,waiting
sysdate,d32,d3,xyz.txt,waiting


I believe this can definitely be implemented with help of magic like awk or anything but not able to implement it.

Any ideas/help would be greatly appreciated.

Source: FULL ARTICLE at The UNIX and Linux Forums

Please help to fix awk script

By snysmumrik

Good morning, fellows. I would need to ask for your help in editing my awk script. Here is the original version:

BEGIN { printf (“CRYST1 200.000 200.000 200.000 90.00 90.00 90.00 P 1 1n”)
maxatoms=1000
natom=0
found_struct = 0
found_bond = 0
}
{
if( NF == 5 )
{
foundff=0
natom++
fftype[natom]=”UNKNOWN”
if ($1 ~ /CT/)
{
fftype[natom] = “C”
foundff=1
}
else if ($1 ~ /OH/)
{
fftype[natom] = “O”
foundff=1
}
else if ($1 ~ /HC/)
{
fftype[natom] = “H”
foundff=1
}
else if ($1 ~ /N/)
{
fftype[natom] = “N”
foundff=1
}

else if ($1 ~ /H1/)
{
fftype[natom] = “H”
foundff=1
}
else if ($1 ~ /HO/)
{
fftype[natom] = “H”
foundff=1
}
else if ($1 = “C”)
{
fftype[natom] = “C”
foundff=1
}
else if ($1 = “O”)
{
fftype[natom] = “O”
foundff=1
}

next

x[natom] = $1
y[natom] = $2
z[natom] = $3

if (foundff == 0)
printf(“PROBLEM : Atom ff type %s not knownn”, $6)
}

}

END {
for (iatom=1; iatom <= natom; iatom++)
{
printf(“HETATM %d %2s %d %14.9f %14.9f %14.9fn” ,
iatom, fftype[iatom], iatom, x[iatom], y[iatom], z[iatom])
}
printf (“ENDn”)
}

And this is type of file I am working with.

0 3 186 200 75202
timestep 500 186 0 3 0.002000 1.000000
40.0000000000 0.0000000000 0.0000000000
-0.0000000034 40.0000000000 0.0000000000
-0.0000000034 -0.0000000034 40.0000000000
CT_1 1 12.011000 0.061000 1.087513
-1.961325738 1.828501682 -8.933652557
CT_1 2 12.011000 0.061000 0.789711
-3.851025437 3.495427316 -10.05849230
CT_1 3 12.011000 0.061000 0.581330
-5.804493575 4.589489777 -8.369482861

ect

I would like to get this as an output:

CRYST1 200.000 200.000 200.000 90.00 90.00 90.00 P 1 1
HETATM 1 C 1 -1.961325738 1.828501682 -8.933652557
HETATM 2 C 2 -3.851025437 3.495427316 -10.05849230
HETATM 3 C 3 -5.804493575 4.589489777 -8.369482861

ect

But coordinates are not really picking up well (next line after CT_1 1 12.011000 0.061000 1.087513). Can you please have a look and suggest any solutions. Thanks.

From: http://www.unix.com/unix-dummies-questions-answers/221317-please-help-fix-awk-script.html

Perl Module Installation issue.

By help_scr_seeker

HTML Code:

I have issue with the perl module installed in the new Linux server

I have installed the required module, but still the perl program was not able to find the path

I'm getting the below error:

[CODE]Can't locate Log/Log4perl.pm in @INC (@INC contains: /usr/lib/perl5/site_perl/Log4perl.pm /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./audit.pl line 11.
BEGIN failed--compilation aborted at ./audit.pl line 11.
[/CODE]

Linux version:

[CODE]
Red Hat Enterprise Linux Server release 5.8 (Tikanga)
[/CODE]
Perl version:

[CODE]
-bash-3.2$ perl -v
This is perl, v5.8.8 built for x86_64-linux-thread-multi
[/CODE]
Perl module Installed:

[CODE]=head2 Mon Mar 11 07:51:56 2013: C L
=over 4
=item *
C
=item *
C
=item *
C
=item *
C
=back
[/CODE]


Require your assistance please. 🙁

From: http://www.unix.com/shell-programming-scripting/220947-perl-module-installation-issue.html

Print one's place for 1 to N times, ksh Perl whatever?

By KmJohnson

Hello all,
I would like to create a for loop or whatever is quick that will print the one’s place of a number for 1-N times
say for example a printed page formatting is 132 characters wide,
I would like a single line

123456789012345678901234567890… …012

That is 132 characters long. I am putting this in a simple print test program and want the recipient to see at a glance that the printing is not getting truncated for any reason.

I see in a similar thread a slick way to print a single character N times

In these below cases if one * was missing (due to printer/lp miss configuration) in a string of 132 your eye could miss it easily.. TIA. –KJ

nawk ‘BEGIN{$1000=OFS=”*”;print}’ < /dev/null
perl -le ‘print “*” x 1000’
printf “%01000d”|tr “0” “*”

http://www.unix.com/shell-programmin…cters-ksh.html

From: http://www.unix.com/shell-programming-scripting/220881-print-ones-place-1-n-times-ksh-perl-whatever.html

SQL date for last seven days

By zozoo

Hi team,

i have a condition in sql where we have as below

Code:

select sum(column_A)
from temp_t
where date >= tdb_datestr_to_epoch('21/11/1995 10:00:00','dd/mm/yyyy hh24:mi:ss')
and date < tdb_datestr_to_epoch('22/11/1995 10:00:00','dd/mm/yyyy hh24:mi:ss')


the problems is i have to do the above for 7 days repeatedly so im trying to automated this

the ones in orange are DD from day filed and they should be consecutive

i tried this

Code:

DECLARE
i number(1);
BEGIN
i:=7;
while (i > 0 )
LOOP
DBMS_OUTPUT.put_line (TO_CHAR (SYSDATE-i,'DD/MM/YYYY'));
i:= i -1;
END LOOP;
END;


so now how to input these in to the the above sql query i.e in > condition it should if its 12/01/2013 then in < condition the date should be 13/01/2013[/CODE][/COLOR]

i have knowledge in shell,awk as well

Thanks all

happy to learn new things

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

awk to find the max

By sathyaonnuix

Experts,
Here is my question.
I have got file like below

Code:

# cat file
XYZb,24,26,6
XYZc,24,26,6
XYZe,24,25,5
XYZf,23,29,5
XYZi,16,25,5
XYZj,24,26,7
XYZn,17,23,4
XYZz,23,29,5


Now, I want to print the line’s Column1[delimitor is ,] whose Column 3 is the max of all.

My code is

Code:

awk -F',' 'BEGIN {max = 0;} {if ($3>max) {max=$3;server=$1}} END {print server}' file


But, If there are more than 1 row contain the same max value, my snippet is printing only the last one. Please help.

Here my expected output is XYZf,XYZz.

PS: I want to get this accomplished with awk

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

awk execution problem in csh

By nex_asp

This has no error

Code:

awk '($8==150) && ($4>=11.001 && $4 =91.001 && $5<=92){print}' OFS="t" file


following are unable to run in csh

Code:

this is giving error awk: line 1: syntax error at or near
awk '($'$gr'=='$grn') && ($'$ll'>='$Y' && $'$ll' ='$X' && $'$lln'<='$Xm'){print}' OFS="t" file


for if statement

Code:

if (awk -v Xn=$X -v Xm=$Xm -v Yn=$Y -v Ym=$Ym -v gr=$grn 'BEGIN { exit ($'$grd'==gr && $'$ll' >= Yn && $'$ll' =Xn && $'$lln' <=Xm) ? 0 : 1 }' $file)
then
echo "Yes"
else
echo "No"


…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

awk processing of passed variables

By Mid Ocean

Currently have this:

Code:

set current=192.168.0.5
set servicehost = `echo $current | awk -F. '{print $4}'`
echo $numberoffields


5

..but would like to reduce # of variables and eliminate echo to have something like this:

Code:

set servicehost = `awk -v s="$current" -F. 'BEGIN{print $2}'`


But doesn’t work. In a similar vein how to determine NR and NF on variables passed via -v .. if possible. For example how to replace the following:

Code:

set string = "hello world"
set numberoffields = `echo $string | awk '{printf("%sn",NF)}'`
echo $numberoffields


2

..with something like this:

Code:

set string = "hello world"
set numberoffields = awk -v s=$string 'BEGIN{printf("%sn",NF)}'
echo $numberoffields


0

Would be great if it returned 2 somehow 🙂

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Perl DBI error

By prasperl

Hi All,
I installed DBI module in a non INC location and using it in my script via “use lib”.
But it throw the below error at the “use DBI” step.
Please help

Code:

Usage: DBI::_install_method(dbi_class, meth_name, file, attribs=Nullsv) at /xx/xxx/xxxxx/xxxxx/oracle/lib/DBI.pm/oracle/lib/DBI.pm line 500.
Compilation failed in require at tokens.pl line 7.
BEGIN failed--compilation aborted at tokens.pl line 7.
Usage: DBD::_::common::trace_msg(sv, msg, this_trace=1) at /xx/xxx/xxxxx/xxxxx/oracle/lib/DBI.pm line 517.
END failed--call queue aborted at tokens.pl line 7.


…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

What is wrong with my code?

By littlewenwen

Hello, all

Suppose my current directory has 3 files:

Code:

file_1 file_2 file_3


I wrote the following codes:

Code:

awk 'BEGIN{while("ls"|getline d) {myarray[$d]++}}; END{close("ls");for (i in myarray){print i, myarray[i]}}' /dev/null


I expect the output be like:

Code:

1 file_1
2 file_2
3 file_3


However, the ouput I get is simply

Code:

3


What is wrong with my code?

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Script to bind to socket

By SkySmart

i need to come up with a script that when run from the command line, it will bind to a socket, and listen for tcp connections on a certain port.

something like:

Code:

### ./connection_listener 5666


i found the following script on the web but when i run it, it complains about “accept” not being available.

Code:

perl -MIO::Socket::INET -ne 'BEGIN{$l=IO::Socket::INET->new(
LocalPort=>5666,Proto=>"tcp",Listen=>5,ReuseAddr=>1);
$l=$l->accept}print $l $_'


OS: Linux/SunOS/HPUX/AIX

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

awk – printing the passwd file

By westmoreland

I’ve got a number of RHEL systems and I’m trying to use awk to read and format the output of /etc/passwd. But I’d like to display the host name of the system at the beginning of each line of output.

I’ve got it working without the adding the host name in this script:

Code:

#!/bin/bash

#HOST=`hostname -s`

awk -F: '
BEGIN {printf ("%-20s%-8s%-8s%-45s%-30s%-20sn", "Username", "UID", "GID", "Description", "Home Directory", "Shell")}

$3 > 100 && $3 < 50000 {printf ("%-20s%-8s%-8s%-45s%-30s%-20sn", $1,$3,$4,$5,$6,$7) }

' /etc/passwd


Here’s the output and it’s the way I want it except for the host name. I don’t think it copied and pasted well below:

Quote:

Username UID GID Description Home Directory Shell
rrdcached 101 102 rrdcached /var/rrdtool/rrdcached /sbin/nologin
nagios 501 100 /home/nagios /bin/bash
powersdp 502 504 /home/powersdp /bin/bash
westmorc 503 505 …read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Script to get required output from textfile

By surender reddy

Hi

Iam running below script on one text file.

usr/xpg4/bin/awk ‘BEGIN {print “IP HOST_NAME SUB “}
/IP IS/ {IP=$3}
/local/ {HOST=$1}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
‘ /Scripts/sub_report_$FILE>/Scripts/sub_final_report_.txt

the output is coming as below

IP

HTML Code:

HOST_NAME SUB
10.238.48.1 [local]bgl-ras-bng-bge-01#cont 12853
10.238.48.33 [local]bgl-ras-bng-bge-02#cont 9527
10.238.48.193 [local]bgl-ras-bng-jnr-03#cont 13475
10.238.48.225 [local]bgl-ras-bng-jnr-04#cont 10753


but I need script to get below output

HTML Code:

IP HOST_NAME SUB
10.238.48.1 bgl-ras-bng-bge-01 12853
10.238.48.33 bgl-ras-bng-bge-02 9527
10.238.48.193 bgl-ras-bng-jnr-03 13475
10.238.48.225 bgl-ras-bng-jnr-04 10753


can anybody help

tnx in advance.

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Size calculation MB to GB

By Daniel Gate

PHP Code:


pcmpath query device |awk 'BEGIN{print "TYPEtDEVICE NAMEtSERIALtSIZEtHOSTNAME"}
/DEVICE/ {
disk=$5
printf "%st", $7
printf "%st", disk
getline; printf "%st", substr($2, length($2)-3)
("bootinfo -s " disk) | getline; printf "%st",$0; sum+=$0
printf "%sn", hostname
} END {print "TOTAL SIZE:", sum}'
hostname=`uname -n`




I have this query working, but it returns SIZE in MB. I want the SIZE output in GB. I tried

HTML Code:

("bootinfo -s " disk)/1024


, but not working.

Please advise.

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Adding hostname on the output

By Daniel Gate

// AIX 6.1

HTML Code:

pcmpath query device |awk 'BEGIN{print "TYPEtDEVICE NAMEtSERIALtSIZE"} /DEVICE/ {disk=$5
printf "%st", $7
printf "%st", disk
getline; printf "%st", substr($2, length($2)-3)
("bootinfo -s " disk) | getline; print; sum+=$0
}
END {print "TOTAL SIZE:", sum}'


I have this syntax working, and need to add the hostname of AIX node on each row on the output so that the output will like:

HTML Code:

TYPE DEVICE NAME SERIAL SIZE HOSTNAME
1750500 hdisk2 100A 16384 rpsprod1
1750500 hdisk3 1010 73728 rpsprod1
1750500 hdisk4 1014 36864 rpsprod1
1750500 hdisk5 1015 36864 rpsprod1
1750500 hdisk6 101E 36864 rpsprod1
1750500 hdisk7 101F 36864 rpsprod1


Please advise.

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

How to pass nawk variable to shell within the same script?

By Optimus81

Hi All,
I tried googling but so far no luck, can someone tell me how pass the variable value used inside the
nawk command to shell. In the below script i get the value of $c (without color: Total Executed: ” c “)
but the printf which is outside the nawk command doesn’t print the value or it always prints $c as null.


nawk 'BEGIN {
print "|========================================|"
print "| SI No CHECKS STATUS |"
print "|========================================|"
} /^[0-9]/ {
c++;
print "|------------------------------------------|"
printf "| %s ",$0; getline; s=$0; sub(/:.*/,"",s);
if(s=="Passed") ++p; if(s=="Failed") ++f; if((s!="Passed")&& (s!="Failed")||(s==" ") ) ++n;
printf " : %s |n", s;
print "|------------------------------------------|"
} END {
print "|========================================|"
print "| Total Executed: " c " |"
print "|========================================|"
}' $1

printf "| Total e[1;35;40m Executed: $c |n"


Source: FULL ARTICLE at The UNIX and Linux Forums