- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell Script with files
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 07:59 AM
10-07-2002 07:59 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 08:19 AM
10-07-2002 08:19 AM
Re: Shell Script with files
For (useful) suggested solutions, it would be helpful if you could post samples from file1 and file2.
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 10:12 AM
10-07-2002 10:12 AM
Re: Shell Script with files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 10:50 AM
10-07-2002 10:50 AM
Re: Shell Script with files
I am a COBOL programmer, and I don't know how to do this in the Unix environment. Here is the attachment.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 11:14 AM
10-07-2002 11:14 AM
Re: Shell Script with files
'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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 11:27 AM
10-07-2002 11:27 AM
Re: Shell Script with files
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 11:35 AM
10-07-2002 11:35 AM
SolutionOOPS...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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 11:51 AM
10-07-2002 11:51 AM
Re: Shell Script with files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 12:21 PM
10-07-2002 12:21 PM
Re: Shell Script with files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 12:24 PM
10-07-2002 12:24 PM
Re: Shell Script with files
Here is a short perl program that may do what you want.
open(INP,"
if (substr($_,0,3) ne "455") { $hold{substr($_,19,4)}=1; }
}
close(INP);
open(INP,"
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 12:48 PM
10-07-2002 12:48 PM
Re: Shell Script with files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 01:04 PM
10-07-2002 01:04 PM
Re: Shell Script with files
How do I get the rabbit to appear?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 01:18 PM
10-07-2002 01:18 PM
Re: Shell Script with files
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2002 01:49 PM
10-07-2002 01:49 PM
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