Using ? with Sed

How to UNCOMMENT a line that contains a specific string using Sed?

Try this sed command,

sed -i '/^#.* 2001 /s/^#//' file

Using sed to split a string with a delimiter

To split a string with a delimiter with GNU sed you say:

sed 's/delimiter/\n/g'     # GNU sed

For example, to split using : as a delimiter:

$ sed 's/:/\n/g' <<< "he:llo:you"
he
llo
you

Or with a non-GNU sed:

$ sed $'s/:/\\\n/g' <<< "he:llo:you"
he
llo
you

In this particular case, you missed the g after the substitution. Hence, it is just done once. See:

$ echo "string1:string2:string3:string4:string5" | sed s/:/\\n/g
string1
string2
string3
string4
string5

g stands for global and means that the substitution has to be done globally, that is, for any occurrence. See that the default is 1 and if you put for example 2, it is done 2 times, etc.

All together, in your case you would need to use:

sed 's/:/\\n/g' ~/Desktop/myfile.txt

Note that you can directly use the sed ... file syntax, instead of unnecessary piping: cat file | sed.

Using sed command replace in the input text file all occurrences of characters '&', '', '' with their HTML entities

From info sed:

3.3 The 's' Command
[...]
The 's' command (as in substitute) is probably the most important in
'sed' [...]. The syntax of the 's' command is 's/REGEXP/REPLACEMENT/FLAGS'.
[...]
The REPLACEMENT can contain [...] unescaped '&' characters which reference the
whole matched portion of the pattern space.

Escape & with \ to \&.

sed edit file in place

The -i option streams the edited content into a new file and then renames it behind the scenes, anyway.

Example:

sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename

while on macOS you need:

sed -i '' 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename

Using sed with multiple conditions

Use an address specification at the beginning of the command to match the line. Then use a regexp in the s command to replace just the number at the end.

sed -i '/^Requires: rpm_name/s/[0-9.]*$/2.0.0/'

Replace whole line containing a string using Sed

You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed:

sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo

Use sed to replace values in a csv column if a condition is met in another column

Using sed

$ sed '/^[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,[^,]*,shooting\|judo,/s/,[^,]*/\U&/' input_file
id,name,nationality,sex,date_of_birth,height,weight,sport,gold,silver,bronze,info
736041664,A JESUS GARCIA,ESP,male,1969-10-17,1.72,64,shooting,0,0,0,

How to use SED to find and replace URL strings with the / character in the targeted strings?

/ is not the delimiter in sed commands, it's just one of the possible ones. For this example, you can for example use , instead since it does not conflict with your strings;

echo 'I think http://www.find.com/page is my favorite' | 
sed 's,http://www.find.com/page,http://www.replace.com/page,g'

Using sed on a long line including conditions keywords and specific characters

The [ ] have special meaning in regular expressions (and also . and in extended regular expression also +, but that doesn't affect here). Read about regular expressions. Escape special characters with backslash.

Do:

sed 's!something \[ something \] something\.something!!'

Adding a line to a file using sed in a shell script

Jonathan already mentioned the potential issues with using sed -i (non-standard, behaves in different ways when supported depending on implementation, etc.). Avoid them by using ed to edit files:

ed -s Test.txt <<EOF
109a
This is the string
.
74d
w
EOF

Note how this appends, and then deletes. Because ed acts on entire files, not a stream of lines, commands to act on specific lines can be in any order.



Related Topics



Leave a reply



Submit