Discussion:
text substitution in a colum
MOKRANI Rachid
2012-11-26 14:05:49 UTC
Permalink
Hi,

This is my firts port.

I have a file with colum like this.

Title1 Title2 Title3 Title4
AA B 66 D
BB DS 7 TT
VS FDSE 53 TGF
BG F 5 GF

I would like to have with a sed command the following result. Add the
character MY_ before all numbers in the colum number 3 and preserve the
title "Title3". All the colums are seaprate by a tab.

Title1 Title2 Title3 Title4
AA B MY_66 D
BB DS MY_7 TT
VS FDSE MY_53 TGF
BG F MY_5 GF



Many thanks for your help/example.

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]
d***@comcast.net
2012-11-26 19:20:02 UTC
Permalink
Assuming your file name is textfile, this should work:



sed "s/^\([^    ]*[     ][^     ]*[     ]\)\([0-9].*\)$/\1MY_\2/" textfile



Between each set of brackets is a single tab character.

It says to replace any pattern that begins the line with any number of not-tab,

followed by a single tab, followed by and number of not-tab, followed by a tab,

followed by a digit, with the match found through the 2nd tab, MY_ and the match for rest of line.



This is probab ly better solved by using awk.

----- Original Message -----
From: "MOKRANI Rachid" <***@ifpen.fr>
To: sed-***@yahoogroups.com
Sent: Monday, November 26, 2012 7:05:49 AM
Subject: text substitution in a colum

Hi,
 
This is my firts port.
 
I have a file with colum like this.
 
Title1    Title2      Title3       Title4
AA          B            66             D
BB          DS          7              TT
VS          FDSE     53             TGF
BG          F            5               GF
 
I would like to have with a sed command the following result. Add the
character MY_ before all numbers in the colum number 3 and preserve the
title "Title3". All the colums are seaprate by a tab.
 
Title1    Title2    Title3    Title4
AA         B         MY_66      D
BB         DS       MY_7       TT
VS         FDSE   MY_53      TGF
BG         F          MY_5       GF
 
 
 
Many thanks for your help/example.
 
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]



------------------------------------
--
Yahoo! Groups Links





[Non-text portions of this message have been removed]
Joel Hammer
2012-11-26 19:26:46 UTC
Permalink
sed "s/\(\t\)\([0-9]\)/\1MY_\2/g"

This seems to do what you want.

Joel
Post by MOKRANI Rachid
Hi,
This is my firts port.
I have a file with colum like this.
Title1 Title2 Title3 Title4
AA B 66 D
BB DS 7 TT
VS FDSE 53 TGF
BG F 5 GF
I would like to have with a sed command the following result. Add the
character MY_ before all numbers in the colum number 3 and preserve the
title "Title3". All the colums are seaprate by a tab.
Title1 Title2 Title3 Title4
AA B MY_66 D
BB DS MY_7 TT
VS FDSE MY_53 TGF
BG F MY_5 GF
Many thanks for your help/example.
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
2012-11-27 08:01:34 UTC
Permalink
if there are numbers in other columns than the 3rd, use that:

sed -r '1!s|(\t[^\t]*\t)([0-9]*)|\1MY_\2|' yourfile

it will only change 3rd column.
Post by Joel Hammer
sed "s/\(\t\)\([0-9]\)/\1MY_\2/g"
This seems to do what you want.
Joel
Post by MOKRANI Rachid
Hi,
This is my firts port.
I have a file with colum like this.
Title1 Title2 Title3 Title4
AA B 66 D
BB DS 7 TT
VS FDSE 53 TGF
BG F 5 GF
I would like to have with a sed command the following result. Add the
character MY_ before all numbers in the colum number 3 and preserve the
title "Title3". All the colums are seaprate by a tab.
Title1 Title2 Title3 Title4
AA B MY_66 D
BB DS MY_7 TT
VS FDSE MY_53 TGF
BG F MY_5 GF
Many thanks for your help/example.
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]
------------------------------------
MOKRANI Rachid
2012-11-27 09:09:50 UTC
Permalink
Hi,

Many thanks for all. Below how to do to change ONLY the firt numéric colum.


cat text.txt
Title1 Title2 Title3 Title4 Title5
AA B 66 D 78
B BDS 7 TT 555
VS FDSE 53 TGF 1
B GF 5 GF 12548

sed -r '2,$ s|(\t[^\t]*\t)([0-9]*)|\1MY_\2|' text.txt
Title1 Title2 Title3 Title4 Title5
AA B MY_66 D 78
B BDS MY_7 TT 555
VS FDSE MY_53 TGF 1
B GF MY_5 GF 12548



But how to do if I need to change ONLY the 5th colum.

cat text.txt
Title1 Title2 Title3 Title4 Title5 Title6 Title7
AA B 66 D 78 45 AA
B BDS 7 TT 555 6555 B
VS FDSE 53 TGF 1 2 QQ
B GF 5 GF 12548 12 WWWW



Title1 Title2 Title3 Title4 Title5 Title6 Title7
AA B 66 D MY_78 45 AA
B BDS 7 TT MY_555 6555 B
VS FDSE 53 TGF MY_1 2 QQ
B GF 5 GF MY_12548 12 WWWW
-----Message d'origine-----
Envoyé : mardi 27 novembre 2012 09:02
Objet : Re: text substitution in a colum
sed -r '1!s|(\t[^\t]*\t)([0-9]*)|\1MY_\2|' yourfile
it will only change 3rd column.
Post by Joel Hammer
sed "s/\(\t\)\([0-9]\)/\1MY_\2/g"
This seems to do what you want.
Joel
Post by MOKRANI Rachid
Hi,
This is my firts port.
I have a file with colum like this.
Title1 Title2 Title3 Title4
AA B 66 D
BB DS 7 TT
VS FDSE 53 TGF
BG F 5 GF
I would like to have with a sed command the following
result. Add the
Post by Joel Hammer
Post by MOKRANI Rachid
character MY_ before all numbers in the colum number 3 and preserve the
title "Title3". All the colums are seaprate by a tab.
Title1 Title2 Title3 Title4
AA B MY_66 D
BB DS MY_7 TT
VS FDSE MY_53 TGF
BG F MY_5 GF
Many thanks for your help/example.
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.
__________________________
Ruud H.G. van Tol
2012-11-27 09:43:36 UTC
Permalink
But how to do if I need to change ONLY the 5th column.
perl -pe's/((?:.*?\t){4})/$1MY_/ if $.>1'
--
Ruud
Thierry Blanc
2012-11-27 11:13:32 UTC
Permalink
\t[^\t]*
means look for a tab followed by anything but a tab. Every column,
except the first, is separated by this. Just repeat it twice for the
third column, 4x for the fifth, etc.
Post by MOKRANI Rachid
Hi,
Many thanks for all. Below how to do to change ONLY the firt numéric colum.
cat text.txt
Title1 Title2 Title3 Title4 Title5
AA B 66 D 78
B BDS 7 TT 555
VS FDSE 53 TGF 1
B GF 5 GF 12548
sed -r '2,$ s|(\t[^\t]*\t)([0-9]*)|\1MY_\2|' text.txt
Title1 Title2 Title3 Title4 Title5
AA B MY_66 D 78
B BDS MY_7 TT 555
VS FDSE MY_53 TGF 1
B GF MY_5 GF 12548
But how to do if I need to change ONLY the 5th colum.
cat text.txt
Title1 Title2 Title3 Title4 Title5 Title6 Title7
AA B 66 D 78 45 AA
B BDS 7 TT 555 6555 B
VS FDSE 53 TGF 1 2 QQ
B GF 5 GF 12548 12 WWWW
Title1 Title2 Title3 Title4 Title5 Title6 Title7
AA B 66 D MY_78 45 AA
B BDS 7 TT MY_555 6555 B
VS FDSE 53 TGF MY_1 2 QQ
B GF 5 GF MY_12548 12 WWWW
-----Message d'origine-----
Envoyé : mardi 27 novembre 2012 09:02
Objet : Re: text substitution in a colum
sed -r '1!s|(\t[^\t]*\t)([0-9]*)|\1MY_\2|' yourfile
it will only change 3rd column.
Post by Joel Hammer
sed "s/\(\t\)\([0-9]\)/\1MY_\2/g"
This seems to do what you want.
Joel
Post by MOKRANI Rachid
Hi,
This is my firts port.
I have a file with colum like this.
Title1 Title2 Title3 Title4
AA B 66 D
BB DS 7 TT
VS FDSE 53 TGF
BG F 5 GF
I would like to have with a sed command the following
result. Add the
Post by Joel Hammer
Post by MOKRANI Rachid
character MY_ before all numbers in the colum number 3 and preserve the
title "Title3". All the colums are seaprate by a tab.
Title1 Title2 Title3 Title4
AA B MY_66 D
BB DS MY_7 TT
VS FDSE MY_53 TGF
BG F MY_5 GF
Many thanks for your help/example.
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.
__________________________
------------------------------------
Peter Dominey
2012-11-26 17:42:43 UTC
Permalink
Hi,

You might find awk a more suitable tool for what you would like to do.

Thanks


On Mon, 26 Nov 2012 15:05:49 +0100
Post by MOKRANI Rachid
Hi,
This is my firts port.
I have a file with colum like this.
Title1 Title2 Title3 Title4
AA B 66 D
BB DS 7 TT
VS FDSE 53 TGF
BG F 5 GF
I would like to have with a sed command the following result. Add the
character MY_ before all numbers in the colum number 3 and preserve
the title "Title3". All the colums are seaprate by a tab.
Title1 Title2 Title3 Title4
AA B MY_66 D
BB DS MY_7 TT
VS FDSE MY_53 TGF
BG F MY_5 GF
Many thanks for your help/example.
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]
------------------------------------
--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
Ruud H.G. van Tol
2012-11-27 09:39:14 UTC
Permalink
Post by MOKRANI Rachid
Title1 Title2 Title3 Title4
AA B 66 D
BB DS 7 TT
VS FDSE 53 TGF
BG F 5 GF
[...] Add the
character MY_ before all numbers in the colum number 3 and preserve the
title "Title3".
perl -pe 's/(.*?\t.*?\t)/$1MY_/ if $.>1'
--
Ruud
Loading...