sed question on how to add a # to beginning of a pattern match - sed

How do you add a comment in front of this line with sed?
I'm trying to add a # sign to line in front of this but my sed is not working.
When I attempt to there is no match.
auth required pam_wheel.so use_uid
sed -i 's/auth required pam_wheel.so use_ui/#auth required pam_wheel.so use_ui/g' /etc/pam.d/su but it is not matching.

Using sed
$ sed -i.bak '/pam_wheel.so use_ui$/s/^/#/' /etc/pam.d/su
-i.bak will create a backup of the file with .bak extension (or anything you wish to name it).

Related

sed remove line if neither pattern provided don't match

I am trying to create a filter command to reduce the lines from a log file, assume each line contains partition made of date,
/iamthepath01/20200301/file01.txt
/iamthepath02/20200302/file02.txt
....
/iamthepathxx/20210619/filexx.txt
then from thousands of lines I only want to keep the ones with two string in the path
/202106
/202105
and remove any other lines
I have tried following command
sed -i -e '\(/202105\|/202106\)!d' ~/log.txt
above command threw
sed: -e expression #1, char 24: unterminated address regex
You can use
sed -i '/\/20210[56]/!d' ~/log.txt
Or, if you need to use more specific alternatives and further enhance the pattern:
sed -i -E '/\/(202105|202106)/!d' ~/log.txt
Details:
-i - GNU sed option for inline file replacement
-E - option enabling POSIX ERE regex syntax
/\/20210[56]/ - regex that matches /20210 and then either 5 or 6
\/(202105|202106) - the POSIX ERE pattern that matches / and then either 202105 or 202106
!d - removes the lines not matching the pattern.
See the online demo:
#!/bin/bash
s='/iamthepath01/20200301/file01.txt
/iamthepath02/20200302/file02.txt
/iamthepathxx/20210619/filexx.txt'
sed '/\/20210[56]/!d' <<< "$s"
Output:
/iamthepathxx/20210619/filexx.txt
sed is the wrong tool for this. If you want a script that's as fragile as the sed one then use grep as it's the tool that exists solely to do a simple g/re/p (hence the name) like you're doing:
$ grep '/20210[56]' file
/iamthepathxx/20210619/filexx.txt
or if you want a more robust solution that focuses just on the part of the line you want to match and so will avoid false matches, then use awk:
$ awk -F '/' '$3 ~ /^20210[56]/' file
/iamthepathxx/20210619/filexx.txt
This might work for you (GNU sed):
sed -ni '\#/20210[56]#p' file
This uses seds -n grep-like option to turn off implicit printing and -i option to edit the file in place.
Normally sed uses the /.../ to match but other delimiters may be used if the first is escaped e.g. \#...#.
So the above solution will filter the existing file down to lines that contain either /202105 or /202106.
N.B. grep will almost certainly be faster in finding the above lines however the use of the -i option may be the ultimate reason for choosing sed (although the same outcome can be achieved by tacking on the > tmpFile && mv tmpFile file to a grep solution).

Sed find and replace Cpp file(.C)

I am having issues of editing huge C++ file where I am using sed to convert List(something) to List<something> why I am doing this because our List has been converted to template.
Command I have written in small shell file is like this
sed -i '/List/s/(/</g' $1
sed -i '/List/s/)/>/g' $1
But this command is converting the whole line associated with List to angular braces like,
some_Fun(List(something)) to some_Fun<List<something>>
I don't want sed to change some_Fun<> , sed should keep some_Fun() and change only List() to List<>.
You can use this sed:
sed 's/\(List\)(\([^)]*\))/\1<\2>/g' file
(OR)
sed 's/List(\([^)]*\))/List<\1>/g' file

sed: can't find label for jump to `omcat-users>/d'

remove everything in a file from to in /etc/tomcat7/tomcat-users.xml
sudo sed '/<tomcat-users>/,/</tomcat-users>/d' /etc/tomcat7/tomcat-users.xml
error:
sed: can't find label for jump to omcat-users>/d
What is wrong with the syntax?
Try
sed '/<tomcat-users>/,/<\/tomcat-users>/d' /etc/tomcat7/tomcat-users.xml
---^
The / needs to be escaped
To remove that section from the input use the -i flag
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
i.e.
sed -i '/<tomcat-users>/,/<\/tomcat-users>/d' /etc/tomcat7/tomcat-users.xml
Why not just change the separator in sed
sed ':<tomcat-users>:,:</tomcat-users>:d' /etc/tomcat7/tomcat-users.xml
Then you do not need to escape the /

Replace pattern in an existing line using sed

I want to replace the
/fdasatavol/ankit
to
/fdasatavol_sata/ankit
Can anyone help me out in this?
to write to a new file (without modifying file1):
sed 's/fdasatavol/fdasatavol_sata/g' file1 > file2
or to replace in the original file:
sed -i 's/fdasatavol/fdasatavol_sata/g' file1
This will replace each occurrence of fdasatavol with fdasatavol_sata:
sed 's/fdasatavol/&_sata/g'
If your input has occurrence of fdasatavol that are not in /fdasatavol/ankit and you don't want to substitute these then use:
sed 's#/fdasatavol/ankit#/fdastatavol_sata/ankit#g'
Note: you can use any character as sed's delimilter to aviod the confusion with the parrtern contiaing /. sed prints to stdout by default, if you are happy with the changes produced by sed you can use the -i option to store back to the file.
sed -i 's/fdasatavol/&_stat/g' file

Add text at the end of each line

I'm on Linux command line and I have file with
127.0.0.1
128.0.0.0
121.121.33.111
I want
127.0.0.1:80
128.0.0.0:80
121.121.33.111:80
I remember my colleagues were using sed for that, but after reading sed manual still not clear how to do it on command line?
You could try using something like:
sed -n 's/$/:80/' ips.txt > new-ips.txt
Provided that your file format is just as you have described in your question.
The s/// substitution command matches (finds) the end of each line in your file (using the $ character) and then appends (replaces) the :80 to the end of each line. The ips.txt file is your input file... and new-ips.txt is your newly-created file (the final result of your changes.)
Also, if you have a list of IP numbers that happen to have port numbers attached already, (as noted by Vlad and as given by aragaer,) you could try using something like:
sed '/:[0-9]*$/ ! s/$/:80/' ips.txt > new-ips.txt
So, for example, if your input file looked something like this (note the :80):
127.0.0.1
128.0.0.0:80
121.121.33.111
The final result would look something like this:
127.0.0.1:80
128.0.0.0:80
121.121.33.111:80
Concise version of the sed command:
sed -i s/$/:80/ file.txt
Explanation:
sed stream editor
-i in-place (edit file in place)
s substitution command
/replacement_from_reg_exp/replacement_to_text/ statement
$ matches the end of line (replacement_from_reg_exp)
:80 text you want to add at the end of every line (replacement_to_text)
file.txt the file name
How can this be achieved without modifying the original file?
If you want to leave the original file unchanged and have the results in another file, then give up -i option and add the redirection (>) to another file:
sed s/$/:80/ file.txt > another_file.txt
sed 's/.*/&:80/' abcd.txt >abcde.txt
If you'd like to add text at the end of each line in-place (in the same file), you can use -i parameter, for example:
sed -i'.bak' 's/$/:80/' foo.txt
However -i option is non-standard Unix extension and may not be available on all operating systems.
So you can consider using ex (which is equivalent to vi -e/vim -e):
ex +"%s/$/:80/g" -cwq foo.txt
which will add :80 to each line, but sometimes it can append it to blank lines.
So better method is to check if the line actually contain any number, and then append it, for example:
ex +"g/[0-9]/s/$/:80/g" -cwq foo.txt
If the file has more complex format, consider using proper regex, instead of [0-9].
You can also achieve this using the backreference technique
sed -i.bak 's/\(.*\)/\1:80/' foo.txt
You can also use with awk like this
awk '{print $0":80"}' foo.txt > tmp && mv tmp foo.txt
Using a text editor, check for ^M (control-M, or carriage return) at the end of each line. You will need to remove them first, then append the additional text at the end of the line.
sed -i 's|^M||g' ips.txt
sed -i 's|$|:80|g' ips.txt
sed -i 's/$/,/g' foo.txt
I do this quite often to add a comma to the end of an output so I can just easily copy and paste it into a Python(or your fav lang) array