IS this possible

IS this possible

am 07.09.2007 20:33:17 von chani

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 help.


Thank you !!!

Re: IS this possible

am 07.09.2007 21:42:55 von Ed Morton

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 help.

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.

Re: IS this possible

am 07.09.2007 21:44:05 von John L

"chani" wrote in message news:1189189997.794622.125040@r29g2000hsg.googlegroups.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.