Operating System - HP-UX
1833811 Members
3879 Online
110063 Solutions
New Discussion

Re: Shell Script with files

 
SOLVED
Go to solution
Glenn Morton_1
Occasional Advisor

Shell Script with files

I have a Korn shell script the reads a file with multiple record types. If I find rcd type =455, then I need to add 4 bytes to the end of the record.
If the recd type=455 and "abcdef" does not exist on the line, 4 spaces need to be added to the end of the line.
If the recd type=455 and has "abcdef" on the line, I need to check another file (file2 - with a sorted sequential list of IDs) to see if file1.ID=file2.ID. If I find a match in file2, the last 4 bytes in file1 should be 'XXYY'. If there is no match in file2, the last 4 bytes should be 'XXbb' (where bb means spaces).
What is the best way to tackle this problem? I am a novice at UNIX. I was told that awk may do the trick
13 REPLIES 13
Tom Maloy
Respected Contributor

Re: Shell Script with files

You can use ksh or awk or perl.

For (useful) suggested solutions, it would be helpful if you could post samples from file1 and file2.

Tom
Carpe diem!
Sachin Patel
Honored Contributor

Re: Shell Script with files

Hi Glenn,
I am not expert in scripting
As a starting point

read file till end
if type=455
then
look for "abcdef"
if not found then add 4 space at end
if found then
open another file
match then id.
done

please post sample of files and your sample script if possible.
This forum has someof great scripter.

Thanks
Sachin
Is photography a hobby or another way to spend $
Glenn Morton_1
Occasional Advisor

Re: Shell Script with files

Sorry for the confusion. It is confusing to me. I'll try to give a good example.
I am a COBOL programmer, and I don't know how to do this in the Unix environment. Here is the attachment.
James R. Ferguson
Acclaimed Contributor

Re: Shell Script with files

Hi Glenn:

'awk' is certainly one way to accomplish this task. Since your specifications are somewhat vague, I'll provide a sample of what I think you want as a pattern.

The code below assumes a reference file named "/tmp/myref" for matching to your list of "ID"s. The code assumes that the first field in your input file is matched against this reference list.

I've used "b" to donote blank characters. Change it to an actual blank (space) if you use this.

#!/usr/bin/sh
awk '
{ if ($0~/455/) {
if ($0~/abcdef/) {
COMMAND=("grep -q " $1 " /tmp/myref")
if (system(COMMAND))
{print $0 "XXbb"
}
else
{print $0 "XXYY"
}
close(COMMAND)
}
else
{print $0"bbbb"
}
}
else
{print $0
}
} ' /tmp/myinput > /tmp/myoutput

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Shell Script with files

Hi (again) Glenn:

OK, using your input (call it '/tmp/myinput' and a reference list called '/tmp/myref'), try this modified version:

#!/usr/bin/sh
awk '
{ if ($2~/455/) {
if ($0~/abcdef/) {
COMMAND=("grep -q " substr($4,4) " /tmp/myref")
if (system(COMMAND))
{print $0 "XXbb"
}
else
{print $0 "XXYY"
}
close(COMMAND)
}
else
{print $0"bbbb"
}
}
else
{print $0
}
} ' /tmp/myinput

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Shell Script with files

Hi (again) Glenn:

OOPS...use this version, please. The previous post bungled the 'substr':

#!/usr/bin/sh
awk '
{ if ($2~/455/) {
if (substr($4,17,6)=="abcdef") {
COMMAND=("grep -q " substr($4,1,4) " /tmp/myref")
if (system(COMMAND))
{print $0 "XXbb"
}
else
{print $0 "XXYY"
}
close(COMMAND)
}
else
{print $0"bbbb"
}
}
else
{print $0
}
} ' /tmp/myinput

Regards!

...JRF...
Sridhar Bhaskarla
Honored Contributor

Re: Shell Script with files

Hi Glenn,

See if this fits your requirements. It will save the white spaces at the end of each line.

-Sri

#!/usr/bin/ksh

sed 's/ /+/g' file1 > tempfile
while read LINE
do
line=`echo $LINE|sed 's/+/ /g'`
RCD=`echo $line |awk '{print $2}'`
echo $RCD
if [ "$RCD" = 455 ]
then
echo $line |grep "abcdef" > /dev/null 2>&1
if [ $? = 0 ]
then
IDENT=`echo $line |awk '{print $4}'|cut -c 1-4`
grep $IDENT file2 > /dev/null 2>&1
if [ $? = 0 ]
then
echo $LINE |sed 's/$/XXYY/g' |sed 's/+/ /g' >> output
else
echo $LINE |sed 's/$/XX##/g' |sed 's/+/ /g' >> output
fi
else
echo $LINE |sed 's/$/####/g' |sed 's/+/ /g' >> output
fi
else
echo $LINE |sed 's/+/ /g' >> output
fi
done < file1

rm tempfile

You may be disappointed if you fail, but you are doomed if you don't try
Tom Maloy
Respected Contributor

Re: Shell Script with files

Or with perl (also attached in case the white space gets goofed up):

perl -nle '
BEGIN {
open (ids, "file2");
while () {
chomp;
$ids{$_} = 1;
}
}
if (/^455 /) {
if (/ .* (....).*abcdef/) {
if (defined $ids{$1}) {
print $_, "XXYY";
}
else {
print $_, "XX ";
}
}
else {
print $_, " ";
}
}
else {print;}' file1
Carpe diem!
Rodney Hills
Honored Contributor

Re: Shell Script with files

Glen,

Here is a short perl program that may do what you want.

open(INP,"while() {
if (substr($_,0,3) ne "455") { $hold{substr($_,19,4)}=1; }
}
close(INP);
open(INP,"open(OUT,">newfile");
while() {
chomp;
undef $app;
$app=" " if substr($_,0,3) eq "455";
$app="XX " if substr($_,35,6) eq "abcdefg" and defined $app;
$app="XXYY" if $hold{substr($_,19,4)} and $app eq "XX ";
print OUT $_,$app,"\n";
}

It first reads in file2 and saves the ID for type 455. Then reading in file1 is just a matter of testing your conditions. (you may want to check the offsets I used in the substr function).

HTH

-- Rod Hills
There be dragons...
Glenn Morton_1
Occasional Advisor

Re: Shell Script with files

Thanks for your input. I believe that I have enough to work with. Special thx to James.
Glenn Morton_1
Occasional Advisor

Re: Shell Script with files

How do I show that I have the Magical Answer?
How do I get the rabbit to appear?
James R. Ferguson
Acclaimed Contributor

Re: Shell Script with files

Hi (again) Glenn:

You already have raised the rabbit. By assigning (at least) one post a score of eight (or greater), the rabbit icon appears next to the thread when you refresh the HP-UX familyhome.

BTW, to keep the hounds from your heels, I suggest that you assign 0-10 points to *every* post in this thread. In that fashion, *your* profile's statistics will show that you have a 100% point assignement rate --- something that seems to get close scrutiny these days ;-)

More importantly, *welcome* to the ITRC forum. I see that today is your first day. I'm glad to have been of help.

Regards!

...JRF...
Jordan Bean
Honored Contributor

Re: Shell Script with files


And yet another way to do it in PERL. Use it this way:

script ref-file < data-file > new-file