Linux remove lines contain certain ip addresses from log file (66.249) - apache

i have a 6gb httpd log file and i want to remove lines beginging in
66.249 (ip block of googlebot) i did have a
SetEnvIf Remote_Addr "66\.249\.\." dontlog
entry in my httpd.conf file but it didnt seem to work
so is there a linux command like
grep -removelines-starting "66.49" acessslog

Using sed: Use -i flag if you make changes in file directly.
sed '/^66\.49/d' logfile
Using grep: This will print lines apart from lines starting with 66.49
grep -v '^66\.49' logfile
Using awk:This will print lines apart from lines starting with 66.49
awk '!/^66\.49/' logfile

I can imagine sed is a better fit for this task.
sed -i '/66\.249/d' ./acessslog
"d" is for deleting matched pattern, while -i is for overwriting input file.

Related

Replace string containing _ and double quotes inside a file

Might be a repeat question, but I am kind of stuck:
I have a file test.txt with the following contents:
# To push each commit to a remote, put the name of the remote here.
# (eg, "origin" for git). Space-separated lists of multiple remotes
# also work (eg, "origin gitlab github" for git).
PUSH_REMOTE=""
I simply want to replace the line in the file that contains PUSH_REMOTE="" with PUSH_REMOTE="origin". Was looking for a sed or awk syntax to achieve the same. I tried escaping the double quotes using sed -i 's#PUSH_REMOTE=""#"PUSH_REMOTE="origin"#g' test.txt, But I keep getting the error sed: 1: "test.txt": undefined label 'est.txt'.
The desired output (file contents of test.txt) is as follows:
# To push each commit to a remote, put the name of the remote here.
# (eg, "origin" for git). Space-separated lists of multiple remotes
# also work (eg, "origin gitlab github" for git).
PUSH_REMOTE="origin"
Thanks again in advance for your time to address this somewhat basic issue of mine!
Here are my os-release details, in case it helps:
NAME="Red Hat Enterprise Linux"
VERSION="8.3 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.3"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.3 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8.3:GA"
HOME_URL="https://www.redhat.com/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.3
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.3"
The error message sed: 1: "test.txt": undefined label 'est.txt' probably means you're using a sed that doesn't have a -i option or, more likely, whose -i option requires a temp file name after it and so it's treating your script as that temp file name and then treating your file name as the script. Either that or you just have a plain old syntax error in your sed script.
This will do what you want using any sed in any shell on every Unix box:
$ sed 's/\(PUSH_REMOTE=\)""/\1"origin"/' file
# To push each commit to a remote, put the name of the remote here.
# (eg, "origin" for git). Space-separated lists of multiple remotes
# also work (eg, "origin gitlab github" for git).
PUSH_REMOTE="origin"
Do whatever you like to update the input file with that output, e.g. either of these:
sed -i '...' file, or
sed -i '' '...' file, or
sed '...' file > tmp && mv tmp file
or whatever.
Could you check whether this awk-command works:
awk '$0~/^PUSH_REMOTE=""$/{$0="PUSH_REMOTE=\"origin\""}1' test.txt

Find httpd.conf file location after it's been changed by -f flag

Httpd processes use a non-default configuration file if they are run with the -f flag.
For example
/home/myuser/apache/httpd-2.4.8/bin/httpd -f /confFiles/apache/2.4.8/apache.conf -k start
will use this configuration file: /confFiles/apache/2.4.8/apache.conf
I need to get this location and would rather not have to check for possible -f flags used to start httpd.
The answer here says to run /path/to/httpd -V and concatenate
-D SERVER_CONFIG_FILE="conf/httpd.conf"
with
-D HTTPD_ROOT="/etc/httpd"
to get the final path to the config file.
However, this path will not be the correct one if the -f flag is used to start the httpd process.
Is there a command that can get the config file that is actually being used by the process?
The answer you refer to mentions the paths httpd was compiled with, but as you say those can be manually changed with parameters.
The simple way to check is the command line, if process is called "httpd" (standard name), a simple ps will reveal the config file being used:
ps auxw | grep httpd
Or querying the server if server has mod_info loaded, in command line or with your favourite browser:
curl "http://yourserver.example.com/server-info?server" | grep -i "config file"
Note: mod_info should not be publicaly available for everyone to see.

SSH - Grep with special chars and *

I'm looking to search some files via SSH with the grep command but I have some special chars.
The string I'm looking for is:
"$GLOBALS['....'];"
I tried this one
grep -r -H "\$GLOBALS\\['*'\\]\;" /var/www/
but nothing happens. Any help will be welcome.
Your RE actually matches "$GLOBALS['''''''];" with one or more ' there.
try this one:
grep -rHP "[$]GLOBALS\['.*?']\;" file
I use [$] instead of \$, is because ESCAPE IS SOMEHOW TRICKY, some environment you need use \\\$.
Update, less than 10 chars inside the []:
grep -rHP "[$]GLOBALS\['.{0,10}']\;" file

How to escape $ in sed over ssh command?

I am trying to create a patch that users can use to remotely edit a file in a pre-defined way using sed, and I could do this manually on each computer, but it would take a long time.
The line I am struggling with is as follows:
host=[hostname]
port=[portnum]
ssh -t $host -p $port "cp ~/file1 ~/file1.bak ; sed -i \"s/fcn1('param1', $2)\n/fcn2('param2'):$zoom\n/g\" ~/file1"
This makes a backup of file1 and then edits a line in the file. I actually want to edit more than one line, but this line demonstrates the problems:
The command works, provided no $ signs are used within the sed command.
I have tried a number of ways of escaping these $ signs but cannot seem to find one that works.
I can use a . wildcard in the find, but obviously not in the replace string.
I would use single quotes for the sed command, in order to avoid expanding the $2, but single quotes are already used inside the command.
Does anyone have any ideas of how to overcome this problem? Thanks in advance for any suggestions!
This should work as well:
ssh -t $host -p $port "cp ~/file1 ~/file1.bak && sed -i \"s/fcn1('param1', \\\$2)/fcn2('param2'):\\\$zoom/g\" file1"
You need 3 back slashes as you have to escape the $ sign in the string passed in the remote bash to sed. And you have to escape that back slash and the $ sign when sending it over via ssh.

Optimizing a sed/tr shell script

I wrote a script to automatically add a ServerAlias to an Apache configuration file, then restart Apache, with a rather remedial understanding of sed and tr and probably shell scripting in general. This is what I came up with:
cp /etc/httpd/sites/site.conf tmphost &&
sed s/ServerName\ site.com/ServerName\ site.com^#ServerAlias\ sub.site.com/ \
tmphost |
tr '^#' '\n\t' >/etc/httpd/sites/site.conf &&
rm -f tmphost &&
apachectl restart
Basically I'm creating a copy, replacing the ServerName line with itself + the new alias, using tr to put in the newline and tab (sed was being weird about that?), overwriting the old configuration file, deleting the copy, then restarting Apache.
It works, but doesn't really make mama proud, if you know what I mean. Any ideas on how to clean that up?
If you want to add a ServerAlias line after ServerName line, why not use the 'append' command to add a line:
sed '/ServerName site.com/a\
ServerAlias sub.site.com' tmphost >/etc/httpd/sites/site.conf
There's a 'tab' at the start of the extra line, which you seem to need.
If you have GNU sed, you can do the edit in-place with the -i option and without the temporary file (but don't use the -i option if the file you are editing has multiple hard links, and probably not it if it is a symlink, either; see the comments below from William Pursell).
Note that you should wrap the code in:
trap "rm -f tmphost; exit 1" 0 1 2 3 13 15
...script manipulating tmphost...
rm -f tmphost
trap 0
so that the temporary file is not left around if the shell exits because of a signal. Of course, if you have an in-place alter, you don't need the temporary file at all.
I like the way with direct ServerAilas addition using sed :
sed -i "0,/^ServerName.\+/s//\0\nServerAlias sub.site.com/" /etc/httpd/sites/site.conf
apachectl reload