Discussion:
grep sed replace
MOKRANI Rachid rachid.mokrani@ifpen.fr [sed-users]
2016-11-10 14:03:10 UTC
Permalink
Hi, (It's been a long time)

I have some files.

cat A.txt
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4


cat B.txt
Bill White
thusday
0.8
Manu Purple
monday
0.8
William Grey
thuesday
0.7
Nathalie Golden
monday
0.8
Marie Pink
sunday
0.6
Susan Navy
sunday
0.6


I want to search in this files the string "Tom Red" and change ONLY the 3 third lines after the result. In this example the string is in the file "A.txt" and need to change "0.8" by "MY_NEW_STRING"

grep -A 3 "Tom Red" *.txt (it's OK)

But How to replace ONLY the third string after ""Tom Red" ?

The result should be.

cat A.txt
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
MY_NEW_STRING
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4


Regards.


__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________


[Non-text portions of this message have been removed]
Thierry Blanc Thierry.Blanc@gmx.ch [sed-users]
2016-11-10 14:09:28 UTC
Permalink
sed -r '/Tom/{n;n;n;s|.*|MY NEW STRING|}' yourfile


find Tom, go 3 lines down, change line
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4
MOKRANI Rachid rachid.mokrani@ifpen.fr [sed-users]
2016-11-10 16:08:59 UTC
Permalink
Hi Thierry,

If I don't make a mistake, It seem that your exemple add a line with " MY NEW STRING " after the third line "0.8".
I need to REPLACE 0.8 by "MY NEW STRING"




-----Message d'origine-----
De : sed-***@yahoogroups.com [mailto:sed-***@yahoogroups.com]
Envoyé : jeudi 10 novembre 2016 15:35
À : sed-***@yahoogroups.com
Objet : Re: grep sed replace



sed -r '/Tom/{n;n;n;s|.*|MY NEW STRING|}' yourfile


find Tom, go 3 lines down, change line
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4
------------------------------------
Posted by: Thierry Blanc <***@gmx.ch>
------------------------------------

--

------------------------------------

Yahoo Groups Links



__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
MOKRANI Rachid rachid.mokrani@ifpen.fr [sed-users]
2016-11-10 16:29:35 UTC
Permalink
It's OK. Just need 2 n.

Thanks.

-----Message d'origine-----
De : MOKRANI Rachid
Envoyé : jeudi 10 novembre 2016 17:09
À : 'sed-***@yahoogroups.com'
Objet : RE: grep sed replace

Hi Thierry,

If I don't make a mistake, It seem that your exemple add a line with " MY NEW STRING " after the third line "0.8".
I need to REPLACE 0.8 by "MY NEW STRING"




-----Message d'origine-----
De : sed-***@yahoogroups.com [mailto:sed-***@yahoogroups.com]
Envoyé : jeudi 10 novembre 2016 15:35
À : sed-***@yahoogroups.com
Objet : Re: grep sed replace



sed -r '/Tom/{n;n;n;s|.*|MY NEW STRING|}' yourfile


find Tom, go 3 lines down, change line
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4
------------------------------------
Posted by: Thierry Blanc <***@gmx.ch>
------------------------------------

--

------------------------------------

Yahoo Groups Links



__________________________
Avant d'imprimer, pensez à l'environnement ! Please consider the environment before printing !
Ce message et toutes ses piÚces jointes sont confidentiels et établis à l'intention exclusive de ses destinataires. Toute utilisation non conforme à sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. IFP Energies nouvelles décline toute responsabilité au titre de ce message. This message and any attachments are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited. IFP Energies nouvelles should not be liable for this message.
__________________________
Daniel Goldman dgoldman@ehdp.com [sed-users]
2016-11-10 17:03:46 UTC
Permalink
Right. It should be one less n command. And the -r option does not hurt
to be included, but is not needed here.

$ cat c.txt
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8

$ sed '/Tom Red/ {n;n;s/.*/NEW STRING/}' c.txt
Mike Blue
monday
0.8
Tom Red
friday
NEW STRING
Virginie Rose
saturday
0.8
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
It's OK. Just need 2 n.
Thanks.
-----Message d'origine-----
De : MOKRANI Rachid
Envoyé : jeudi 10 novembre 2016 17:09
Objet : RE: grep sed replace
Hi Thierry,
If I don't make a mistake, It seem that your exemple add a line with " MY NEW STRING " after the third line "0.8".
I need to REPLACE 0.8 by "MY NEW STRING"
-----Message d'origine-----
Envoyé : jeudi 10 novembre 2016 15:35
Objet : Re: grep sed replace
sed -r '/Tom/{n;n;n;s|.*|MY NEW STRING|}' yourfile
find Tom, go 3 lines down, change line
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4
------------------------------------
------------------------------------
Thierry Blanc Thierry.Blanc@gmx.ch [sed-users]
2016-11-10 14:45:10 UTC
Permalink
sed -r '/Tom/{n;n;s|.*|MY NEW STRING|}' yourfile

sorry, only 2 n!
Post by Thierry Blanc ***@gmx.ch [sed-users]
sed -r '/Tom/{n;n;n;s|.*|MY NEW STRING|}' yourfile
find Tom, go 3 lines down, change line
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4
------------------------------------
------------------------------------
Jim Hill gjthill@gmail.com [sed-users]
2016-11-10 16:21:44 UTC
Permalink
Slurp up line gaggles and have your way with them:

sed 'N;N;/^Tom Red\n/s/[^\n]*/New Third Line/3'


[Non-text portions of this message have been removed]
Davide Brini dave_br@gmx.com [sed-users]
2016-11-10 14:15:32 UTC
Permalink
Post by MOKRANI Rachid ***@ifpen.fr [sed-users]
Hi, (It's been a long time)
I have some files.
cat A.txt
Paul Brown
monday
0.8
Mike Blue
monday
0.8
Tom Red
friday
0.8
Virginie Rose
saturday
0.8
David Brown
saturday
0.7
Brian Yellow
monday
0.55
Tom Black
thuesday
0.4
cat B.txt
Bill White
thusday
0.8
Manu Purple
monday
0.8
William Grey
thuesday
0.7
Nathalie Golden
monday
0.8
Marie Pink
sunday
0.6
Susan Navy
sunday
0.6
I want to search in this files the string "Tom Red" and change ONLY the 3
third lines after the result. In this example the string is in the file
"A.txt" and need to change "0.8" by "MY_NEW_STRING"
grep -A 3 "Tom Red" *.txt (it's OK)
But How to replace ONLY the third string after ""Tom Red" ?
You can do

sed '/Tom Red/{ n; n; s/.*/MY_NEW_STRING/; }'
--
D.
Loading...