Extract and replace string with bash [closed]

 Clash Royale CLAN TAG#URR8PPP
Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {
From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
Expected output:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.
bash shell-script sed grep regular-expression
 closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan
add a comment |Â
up vote
-1
down vote
favorite
I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {
From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
Expected output:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.
bash shell-script sed grep regular-expression
 closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan
 
 
 
 
 
 
 Cross-posted: stackoverflow.com/questions/50382726/â¦
 â Kusalananda
 May 17 at 5:36
 
 
 
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {
From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
Expected output:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.
bash shell-script sed grep regular-expression
I have a string like: func showFetchNextSliceFailed(result: UserResult<[Product]>) {
From this, I would like to 1). extract just showFetchNextSliceFailed 2). Insert it right above the given line, as #Mark showFetchNextSliceFailed.
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
Expected output:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
I tried with grep and sed, but still can't figure out correctly how to extract and replace strings.
bash shell-script sed grep regular-expression
asked May 17 at 2:50


Mehul Parmar
1014
1014
 closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan
 closed as off-topic by Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan May 17 at 16:58
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." â Kusalananda, Jeff Schaller, vonbrand, X Tian, Anthony Geoghegan
 
 
 
 
 
 
 Cross-posted: stackoverflow.com/questions/50382726/â¦
 â Kusalananda
 May 17 at 5:36
 
 
 
add a comment |Â
 
 
 
 
 
 
 Cross-posted: stackoverflow.com/questions/50382726/â¦
 â Kusalananda
 May 17 at 5:36
 
 
 
Cross-posted: stackoverflow.com/questions/50382726/â¦
â Kusalananda
May 17 at 5:36
Cross-posted: stackoverflow.com/questions/50382726/â¦
â Kusalananda
May 17 at 5:36
add a comment |Â
 2 Answers
 2
 
active
oldest
votes
up vote
3
down vote
accepted
With sed
- copy the matching pattern space to hold space
- modify the pattern space
- recover the original from hold space and append it
Ex.
$ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
add a comment |Â
up vote
0
down vote
If you have the following code file:
INPUT:
 $more code
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
COMMAND:
perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code
OUTPUT:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
EXPLANATIONS:
- -i.bakin-place mode for- perlwith backup of the input file
- s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/gfind and replace mode detailed at demo
add a comment |Â
 2 Answers
 2
 
active
oldest
votes
 2 Answers
 2
 
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
With sed
- copy the matching pattern space to hold space
- modify the pattern space
- recover the original from hold space and append it
Ex.
$ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
add a comment |Â
up vote
3
down vote
accepted
With sed
- copy the matching pattern space to hold space
- modify the pattern space
- recover the original from hold space and append it
Ex.
$ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
With sed
- copy the matching pattern space to hold space
- modify the pattern space
- recover the original from hold space and append it
Ex.
$ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
With sed
- copy the matching pattern space to hold space
- modify the pattern space
- recover the original from hold space and append it
Ex.
$ sed '/func show/ h; s/func (show[^(]*).*/#Mark 1/; G;' file
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
answered May 17 at 3:21
steeldriver
31.2k34978
31.2k34978
add a comment |Â
add a comment |Â
up vote
0
down vote
If you have the following code file:
INPUT:
 $more code
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
COMMAND:
perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code
OUTPUT:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
EXPLANATIONS:
- -i.bakin-place mode for- perlwith backup of the input file
- s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/gfind and replace mode detailed at demo
add a comment |Â
up vote
0
down vote
If you have the following code file:
INPUT:
 $more code
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
COMMAND:
perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code
OUTPUT:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
EXPLANATIONS:
- -i.bakin-place mode for- perlwith backup of the input file
- s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/gfind and replace mode detailed at demo
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you have the following code file:
INPUT:
 $more code
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
COMMAND:
perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code
OUTPUT:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
EXPLANATIONS:
- -i.bakin-place mode for- perlwith backup of the input file
- s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/gfind and replace mode detailed at demo
If you have the following code file:
INPUT:
 $more code
 func showProductsList(result: UserResult<[Product]>) 
 
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
COMMAND:
perl -i.bak -pe "s/^^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/g" code
OUTPUT:
 #Mark showProductsList
 func showProductsList(result: UserResult<[Product]>) 
 
 #Mark showFetchNextSliceFailed
 func showFetchNextSliceFailed(result: UserResult<[Product]>) 
 
EXPLANATIONS:
- -i.bakin-place mode for- perlwith backup of the input file
- s/^( *|t*)funcs(w+)funcs(w+)/1#Mark 2n1func 2/gfind and replace mode detailed at demo
answered May 17 at 5:33
Allan
1012
1012
add a comment |Â
add a comment |Â
Cross-posted: stackoverflow.com/questions/50382726/â¦
â Kusalananda
May 17 at 5:36