P
P
pixik2016-12-01 13:35:03
bash
pixik, 2016-12-01 13:35:03

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");

How to get this result:
int i;
//   cout << "hello!" << endl;
//   printf("%s", "hello");
   printf("%s", "world");

I don’t understand the moment how to insert exactly at the beginning of the line that matches the regexp.
a pattern like this
[any character]*SPECIFIC_SUBTSTRING[any character up to the end of the string]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Saboteur, 2016-12-01
@pixik

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

P
pixik, 2016-12-01
@pixik

Быстро не смог найти как это можно сделать. пришлось удалять.
было:

int i;
cout << "hello!" << endl;
printf("%s", "hello");
printf("%s", "world");

cтало:
int i;
printf("%s", "world");

A
abcd0x00, 2016-12-03
@abcd0x00

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 question

Ask a Question

731 491 924 answers to any question