I am creating a script that I can run and it will simply ask me the common location name...i.e SEC-DF1 and it will fetch the ip of that site from within script. My problem is taking that IP and replacing
right=IP_ADDRESS
with
right=NEW_IP_ADDRESS
I need this so I can call the script as I will be changing the value of right so often for testing.
I have been messing with sed until someone mentioned awk...this stuff has such horrid documentation I keep getting all types errors or weird results on the test file I am messing with.
Since this is a straight forward substitution, I would just use sed:
sed -e 's/^right=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/right=192.168.1.92/' filename
This will match right= at the beginning of a line followed by an IP address and replace it with the IP of your choosing.
This command will modify your script:
NEW_IP_ADDRESS=101.102.103.104 sed -i "s/^(right=).*$/\\1$NEW_IP_ADDRESS/" script
Related
I want to change my default web root folder of apache2 web server, but through command line from a script I am making.
I know to do it through nano/vim and then go to the line and change it manually, but I want to make it by a command line.
I though about some thing like (the syntax is wrong - I know - just to make my point):
vim /etc/apache2/sites-enabled/000-default.conf | find 'DocumentRoot /var/www' | replace 'DocumentRoot /var/www/myFolder'
maybe not with vim but other ??
Any Idea ?
Thanks
Use sed with argument -i.
sed -i 's-/var/www-&/MyFolder-' /etc/apache2/sites-enabled/000-default.conf
Argument -i enables in-place editing.
You should use sed with the substitute command for that kind of operation.
http://www.grymoire.com/Unix/Sed.html#uh-0
I don't have a unix machine at hand but something like that should work (using # rather than the usual / as separator):
sed 's#/var/www#/var/www/MyFolder#' /etc/apache2/sites-enabled/000-default.conf
Even if it is not your question, since your initial question mentioned Vim, you can also use substitute from inside Vim
Like
:%s #/var/war#/var/www/MyFolder#g
% means search in the whole file
g means globally : it will replace multiple instance if the string is found multiple times
I have an output file (namely a log from screen) containing several control characters. Inside the screen, I have programs running that use control characters to refresh certain lines (examples would be top or anything printing progress bars).
I would like to output a tail of this file using PHP. If I simply read in that file and echo its contents (either using PHP functions or through calling tail, the output is messy and much more than these last lines as it also includes things that have been overwritten. If I instead run tail in the command line, it returns just what I want because the terminal evaluates the control characters.
So my question is: Is there a way to evaluate the control characters, getting the output that a terminal would show me, in a way that I could then use elsewhere (e.g., write to a file)?
#5gon12eder's answer got rid of some control characters (thanks for that!) but it did not handle the carriage return part that was even more important to me.
I figured out that I could just delete anything from the beginning of a line to the last carriage return inside that line and simply keep everything after that, so here is my sed command accomplishing that:
sed 's/^.*\r\([^\r]\+\)\r\?$/\1\r/g'
The output can then be further cleaned using #5gon12eder's answer:
cat screenlog.0 | sed 's/^.*\r\([^\r]\+\)\r\?$/\1\r/g' | sed 's,\x1B\[[0-9?;]*[a-zA-Z],,g'
Combined, this looks exactly like I wanted.
I'm not sure what you mean by “evaluating” the control characters but you could remove them easily.
Here is an example using sed but if you are already using PHP, its internal regex processing functionality seems more appropriate. The command
$ sed 's,\x1B\[[0-9?;]*[a-zA-Z],,g' file.dat
will dump the contents of file.dat to standard output with all ANSI escape sequences removed. (And I'm pretty sure that nothing else is removed except if your file contains invalid escape sequences in which case the operation is ill-defined anyway.)
Here is a little demo:
$ echo -e "This is\033[31m a \033[umessy \033[46mstring.\033[0m" > file.dat
$ cat file.dat
# The output of the above command is not shown to protect small children
# that might be browsing this site.
$ reset # your terminal
$ sed 's,\x1B\[[0-9?;]*[a-zA-Z],,g' file.dat
This is a messy string.
The less program has some more advanced logic built in to selectively replace some escape sequences. Read the man page for the relevant options.
Due to processes out of my control I need run multiple SH files which contains lengthy CURL commands. Problem is that whichever process created these commands seems to have included one line of whitespace at the very end. If I call it as is - it fails. If I physically open the file and hit backspace on the first full empty line and save the file - it works perfectly.
Any way to put some kind of command into the SH file so that it removes any unnecessary stuff?
More info would be helpful, but the following might work:
If you need to put something into each of the files that contain the curl commands as you mention, you could try putting exit as the last line of the curl script (also depends on how you're calling the 'curl files'
exit
If you can run a separate script against the files that have a blank line, perhaps sed the blank lines away?
sed -i s/^\s$// $fileWithLineOfSpaces
edit:
Or (after thinking about it), perhaps simply delete the last line of the file....
sed -i '$d' $file
I'm new to sed command. I was reading the build script for some source code and I found this for loop in it.
for x in '*.la'
do
sed -i -e 's|^\(libdir=.\).*\(/opt/toolchains\)|\1\2|' x
done
I'm not able to understand the function this for loop is doing. Can anyone help.
It's iterating over a series of files in the current directory ending with ".la" and for each file found, it's editing the contents using sed to convert lines of the form:
libdir=X[zero-or-more-chars]/opt/toolchains
(where X is any character) into lines of the form:
libdir=X/opt/toolchains.
In other words, it's removing the [zero-or-more-chars] part of those lines.
Actually, this looks buggy because I would expect the sed command to reference $x, not x. I have a feeling you lost the $ somehow in the copy/paste step (or perhaps it's simply a bug).
So I have an extremely simple shell script, where it inputs an IP address and a domain name in the hosts file of a person's computer. For "development purposes only!" it works fine, it just inputs the IP address and domain name like so
192.168.53.215dev.env.os
192.168.53.215dev.source.os
Where I want it to input it like this:
192.168.53.215 dev.env.os
192.168.53.215 dev.source.os
So basically I want it to add space between the IP address and the domain name, unfortunately I can't really figure it out. Any help would be appreciated, this is a simple script though and shouldn't be cluttered with an over complicated script. Please keep it simple and explain exactly what you're doing I would like to learn it not have it done for me thanks!
Here is the code:
do shell script "/usr/bin/printf \"\\n# Add #####'s ip for Sourcebox\\n192.168.53.215\\dev.env.os\\n192.168.53.215\\dev.source.os\\n\" >> /etc/hosts; /usr/bin/dscacheutil -flushcache" with administrator privileges
Using the -e flag with echo in a bash script will allow you to output tabs.
echo -e "this\thas\ttabs"
The printf can be used as below mentioned for getting tab space
printf "\t"