- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Shell script.
Operating System - HP-UX
1753912
Members
8573
Online
108810
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО07-27-2000 10:45 PM
тАО07-27-2000 10:45 PM
Re: Shell script.
Hi Curt !
I read your script closely. I find it a little more complicated than the Andreas and Alan's scripts. I'm afraid your script would be more time and cpu consuming than the Andreas and Alan's ones.
Thank you very much for your contribution!
Pat.
I read your script closely. I find it a little more complicated than the Andreas and Alan's scripts. I'm afraid your script would be more time and cpu consuming than the Andreas and Alan's ones.
Thank you very much for your contribution!
Pat.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2000 02:43 AM
тАО07-28-2000 02:43 AM
Re: Shell script.
Well, my script is more complicated for a couple of reasons:
1) I extract only the line before the first occurance of the pattern as specified, not the line before every occurance of the pattern as alan's does
2) my script handles the situation if the pattern occurs in the first line. Alan's will output an empty line being prev will have no value at that time and will later remove all the blank lines from the file. I don't think this was your intention.
As far as time and cpu, my script would only do 3 greps and an awk on the file worst case (double that if you want to count creating the file of the deleted lines which could be removed if it isn't necessary), where if there is only 2 matches in the file alan's script will do 4 greps and an awk on the file. And with more matches alan's would do even more greps increasing by 2 greps for every line with a pattern match.
But, if you'd like to do it even faster:
#!/usr/bin/ksh
line_num=$(grep -n $pattern $mfile | awk -F: '{print $1;exit;}')
if [[ -n $line_num ]] ;then
if (( $line_num > 1 )) ;then
(( line_num = $line_num - 1 ))
sed -e '$line_num d'
-e '/$pattern/d' $mfile > $tmpfile
else
sed -e '/$pattern/d' $mfile > $tmpfile
fi
mv $tmpfile $mfile
fi
sorry, but I have a different opinion then you
1) I extract only the line before the first occurance of the pattern as specified, not the line before every occurance of the pattern as alan's does
2) my script handles the situation if the pattern occurs in the first line. Alan's will output an empty line being prev will have no value at that time and will later remove all the blank lines from the file. I don't think this was your intention.
As far as time and cpu, my script would only do 3 greps and an awk on the file worst case (double that if you want to count creating the file of the deleted lines which could be removed if it isn't necessary), where if there is only 2 matches in the file alan's script will do 4 greps and an awk on the file. And with more matches alan's would do even more greps increasing by 2 greps for every line with a pattern match.
But, if you'd like to do it even faster:
#!/usr/bin/ksh
line_num=$(grep -n $pattern $mfile | awk -F: '{print $1;exit;}')
if [[ -n $line_num ]] ;then
if (( $line_num > 1 )) ;then
(( line_num = $line_num - 1 ))
sed -e '$line_num d'
-e '/$pattern/d' $mfile > $tmpfile
else
sed -e '/$pattern/d' $mfile > $tmpfile
fi
mv $tmpfile $mfile
fi
sorry, but I have a different opinion then you
nobody else has this problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-28-2000 04:30 AM
тАО07-28-2000 04:30 AM
Re: Shell script.
and if you really want to spend the time maintaining an awk script:
#!/usr/bin/ksh
cat infile | awk '
BEGIN { First = "no";
Match = "no";
getline;
if ( $0 ~ /pattern/ ) {
First = "yes";
Match = "yes";
}
prev = $0;
}
{
if ( $0 ~ /pattern/ ) {
if ( First == "no" ) {
First = "yes";
}
else {
if ( Match == "no" ) {
print prev; }
}
Match = "yes";
}
else {
if ( Match == "no" ) {
print prev; }
Match = "no";
}
prev = $0
}
END
{ if ( Match == "no" ) print prev; }
' > outfile
mv outfile infile
myself the greps, seds, and eds are simplier to understand.
#!/usr/bin/ksh
cat infile | awk '
BEGIN { First = "no";
Match = "no";
getline;
if ( $0 ~ /pattern/ ) {
First = "yes";
Match = "yes";
}
prev = $0;
}
{
if ( $0 ~ /pattern/ ) {
if ( First == "no" ) {
First = "yes";
}
else {
if ( Match == "no" ) {
print prev; }
}
Match = "yes";
}
else {
if ( Match == "no" ) {
print prev; }
Match = "no";
}
prev = $0
}
END
{ if ( Match == "no" ) print prev; }
' > outfile
mv outfile infile
myself the greps, seds, and eds are simplier to understand.
nobody else has this problem
- « Previous
-
- 1
- 2
- Next »
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP