|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Dear all,
I am new to sed. Assume I have a file which contain follwings 1. A.B.C des a/03/111 2. X.Y.Z pqr a/04/114 if i use the command $ sed s/'a\/0[0-9]\/[0-9][0-9][0-9]'//g t the last column will be deleted. & output will be 1. A.B.C des 2. X.Y.Z pqr But I want the inverse of this. That means deleting other column & extract last column Is it possible with sed. I know it can be done with cut. But in my actual file data contain like this. 1. A.B.C des a/03/111 2. a/04/114 X.Y.Z pqr So what i want is to keep matching pattern & deleting remaining. Can you please give me a . Thank you !!! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
chani wrote:
> Dear all, > > I am new to sed. Assume I have a file which contain follwings > > > 1. A.B.C des a/03/111 > 2. X.Y.Z pqr a/04/114 > > if i use the command > > $ sed s/'a\/0[0-9]\/[0-9][0-9][0-9]'//g t > > the last column will be deleted. & output will be > > 1. A.B.C des > 2. X.Y.Z pqr It doesn't have to be that complicated. Look: $ cat file 1. A.B.C des a/03/111 2. X.Y.Z pqr a/04/114 $ sed 's/[^ ]*$//' file 1. A.B.C des 2. X.Y.Z pqr or 's/ *[^ ]*$//' if you want to get rid of the trailing spaces too. > But I want the inverse of this. That means deleting other column & > extract last column > Is it possible with sed. Well, yes: $ sed 's/.* //' file a/03/111 a/04/114 > I know it can be done with cut. > But in my actual file data contain like this. > > > > 1. A.B.C des a/03/111 > 2. a/04/114 X.Y.Z pqr > > So what i want is to keep matching pattern & deleting remaining. > Can you please give me a . You don't show what you want your output to be so it's hard to guess what you want, but in general to match on a pattern and only print that you'd do: sed 's/.*\(pattern\).*/\\1/' file You may want to post some better sample input, expected output and explanation. Ed. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"chani" <chanakam2000@gmail.com> wrote in message news:1189189997.794622.125040@r29g2000hsg.googlegr oups.com... > But in my actual file data contain like this. > > > > 1. A.B.C des a/03/111 > 2. a/04/114 X.Y.Z pqr > > So what i want is to keep matching pattern & deleting remaining. > Yes. Use tagged regular expressions. First, since there are /s in the expression, let's use : as delimiter. Let us also move the quotes to surround the whole expression. sed 's:.*\(a/0[0-9]/[0-9][0-9][0-9]\).*:\1:' The \(...\) surround the regular expression you are interested in, and the \1 replaces whatever was matched with the 1st matched expression (likewise \2, \3 ... \9 if there are more than one). Or, using /s which need to be escaped: sed 's/.*\(a\/0[0-9]\/[0-9][0-9][0-9]\).*/\1/' You might not need the first .* -- it depends whether the line numbers are part of the file. -- John. |
|
![]() |
| Outils de la discussion | |
|
|