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/'
Related
\#Add another new line of text to hosts and send the output to
hosts_update.sh
sed '/localhost/a\
# Gateway
10.0.0.1 it20.it.cs.umb.edu it20
# Addresses for the Windows PCs
10.0.0.240 it21.it.cs.umb.edu it21\
10.0.0.241 it22.it.cs.umb.edu it22\
10.0.0.242 it23.it.cs.umb.edu it23\
10.0.0.243 it24.it.cs.umb.edu it24\
10.0.0.244 it25.it.cs.umb.edu it25\
10.0.0.245 it26.it.cs.umb.edu it26\
10.0.0.246 it27.it.cs.umb.edu it27\
10.0.0.247 it28.it.cs.umb.edu it28\
' hosts > hosts_update.sh
First things first, your initial couple of lines look way off for a shell script. It looks like your hosts_update.sh line should be part of the comments (and the comment shouldn't start with a \ anyway):
# Add another new line of text to hosts and send the output
# to hosts_update.sh
Secondly, you need a \ at the end of each line that you're appending with sed, at the moment you only have it on certain select lines. With that in mind, this script is probably what you wanted:
# Add another new line of text to hosts and send the output
# to hosts_update.sh
sed '/localhost/a\
\
# Gateway\
10.0.0.1 it20.it.cs.umb.edu it20\
\
# Addresses for the Windows PCs\
10.0.0.240 it21.it.cs.umb.edu it21\
10.0.0.241 it22.it.cs.umb.edu it22\
10.0.0.242 it23.it.cs.umb.edu it23\
10.0.0.243 it24.it.cs.umb.edu it24\
10.0.0.244 it25.it.cs.umb.edu it25\
10.0.0.245 it26.it.cs.umb.edu it26\
10.0.0.246 it27.it.cs.umb.edu it27\
10.0.0.247 it28.it.cs.umb.edu it28\
' hosts > hosts_update.sh
What's actually happening in your case (without the \ continuation characters):
sed '/localhost/a\
# Gateway
10.0.0.1 it20.it.cs.umb.edu it20
is that:
you append a single blank line after localhost;
then you have a sed comment line;
then you tell sed to execute . on line number ten.
At that point, sed rightly complains it has no idea what to do with the . command :-)
I'd say, based on experience, an earlier (working) iteration of the script had only the it21-28 lines and someone added (badly) the it20 and comment/blank lines. That's based on the fact only those lines are the errant ones. However, that's just (informed) speculation and doesn't affect the answer.
And, finally, you probably don't want to call the resultant file hosts_update.sh, people will almost certainly think it's a shell script rather than the hosts file it actually is.
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.
I would like to do a sed command in Linux to uncomment the "#auth"
Original file
#%PAM-1.0
auth sufficient pam_rootok.so
# Uncomment the following line to implicitly trust users in the "wheel" group.
#auth sufficient pam_wheel.so trust use_uid
I can write this command to do it:
sed 's/#auth.*sufficient.*pam_wheel.so trust use_uid/auth\t sufficient\t pam_wheel.so trust use_uid/' /etc/pam.d/su
But I think it is too long. Is there any better way to do this (more generic)?
I don't want to specific the line number to replace it, because if someone changed the file, the script will not run normally.
For example:
Search keyword "#auth.*sufficient.*pam_wheel.so trust use_uid", if found, replace this the word "#auth" to "auth", and then append the later wording in the line
With GNU sed, look up the -i option that allows in-place modification and then anchor the regular expression. For instance:
sed -i '/^#auth.*pam_wheel/s/^#//' INPUTFILE
will look for lines beginning with "#auth" that include "pam_wheel" later on the line and replace the "#" at the beginning with nothing.
In bash, how do I search for the following string in a file ~/.netrc and delete that line plus the next two lines if found:
machine api.mydomain.com
Example is:
machine api.mydomain.com
user foo
password bar
It should delete all three lines, but I can't match user and password since those are unknown. The only fixed value is machine api.mydomain.com.
Try:
sed -i '' '/^machine api.mydomain.com$/{N;N;d;}' ~/.netrc
When this finds the line machine api.mydomain.com, it reads in two more lines and then deletes them all. Other lines pass through unchanged.
For GNU sed, the argument to -i is optional. For OSX (BSD) sed, the argument is required but is allowed to be empty as shown above.
Let's google it together - sed or awk: delete n lines following a pattern
So, the answer is sed -e '/machine api.mydomain.com/,+2d' ~/.netrc. Add -i flag if changes need to be done in place.
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