Answer the question
In order to leave comments, you need to log in
How to do substring replacement in Linux console?
Good afternoon!
I don't know how to write a sed command to insert a slash at the beginning of line 2.
there is a line like:
int i;
cout << "hello!" << endl;
printf("%s", "hello");
printf("%s", "world");
int i;
// cout << "hello!" << endl;
// printf("%s", "hello");
printf("%s", "world");
Answer the question
In order to leave comments, you need to log in
Suppose you have a test.txt file with the specified text, then
Decryption:
take the text between 2.3 lines - .*
Enclose it in brackets (.*) - now we can call it via back reference \1
add two escaped slashes and backreference \/\/\1
if you need to change the file itself - add sed -i to the sed call
Быстро не смог найти как это можно сделать. пришлось удалять.
было:
int i;
cout << "hello!" << endl;
printf("%s", "hello");
printf("%s", "world");
int i;
printf("%s", "world");
func()
{
cat <<EOF
int i;
cout << "hello!" << endl;
printf("%s", "hello");
printf("%s", "world");
EOF
}
func
func | sed '/hello/ s%^%//%'
[[email protected] sh]$ func()
> {
> cat <<EOF
> int i;
> cout << "hello!" << endl;
> printf("%s", "hello");
> printf("%s", "world");
> EOF
> }
[[email protected] sh]$
[[email protected] sh]$ func
int i;
cout << "hello!" << endl;
printf("%s", "hello");
printf("%s", "world");
[[email protected] sh]$
[[email protected] sh]$ func | sed '/hello/ s%^%//%'
int i;
// cout << "hello!" << endl;
// printf("%s", "hello");
printf("%s", "world");
[[email protected] sh]$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question