Operating System - HP-UX
1753692 Members
6322 Online
108799 Solutions
New Discussion юеВ

Re: extract a special string sequence

 
SOLVED
Go to solution
Jose Mosquera
Honored Contributor

extract a special string sequence

Hi all,

I need extract this string sequence from a file:
:124::

Where:
1.- Characters ":" will be assumed as field separator.
2.- have variable lenght.
3.- This string sequence begins at any column position inside the file.

Please try use grep command for this.

Rgds.
9 REPLIES 9
G V R Shankar
Valued Contributor

Re: extract a special string sequence

Hi,

Please post example data and requireed o/p.

Cheers,
Ravi.
James R. Ferguson
Acclaimed Contributor

Re: extract a special string sequence

Hi:

Perhaps:

# grep ":124:[0-9]*:" file

Regards!

...JRF...
Matti_Kurkela
Honored Contributor

Re: extract a special string sequence

Why must it be "grep", as "sed" might be more suitable for the job?

Off the top of my head:

sed -ne 's/^.*:124:\([0-9]*\):.*$/\1/gp'
This monstrous regexp might be more understandable when broken into parts:

s/something/other/gp
Replaces every instance of "something" with "other", and printing the output only if we found something to replace. We are using it a bit creatively here to replace an entire line with a little bit picked from the middle of the line.

^.*:124:\[0-9]*\):.*$

This defines what we're looking for.
^ = starting from the beginning of the line
.* = here can be any number of any characters
:124: = the first string to match
\( = signals the beginning of the interesting part...
[0-9]* = ...which contains a variable number of digits and nothing else...
\) = then the interesting part ends.
: = there must be one colon immediately after the interesting part
.* = then again any number of any characters...
$ = until the end of the line.

The replacement string will be simply:
\1 = the first (and in this case, only) interesting part indicated in the search string.

MK
MK
Matti_Kurkela
Honored Contributor

Re: extract a special string sequence

Oops, I assumed you'd want only the content of the numeric field. If you want the entire
:123::, then:

sed -ne 's/^.*\(:124:[0-9]*:\).*$/\1/gp'
I.e. just move the "start & end of the interesting part" indicators to the correct spot.

MK
MK
Jose Mosquera
Honored Contributor

Re: extract a special string sequence

Hi,

James your hint does't works.

An example, I have this entries inside a file:
admjmm:*:124:120::/home/admjmm:/sbin/sh
testv201:*:136:124::/wh/leuv2:/usr/bin/ksh
ftp4kel:*:151:124::/wh/kel/./:/usr/bin/ftponly

I just need extract the first one:
admjmm:*:124:120::/home/admjmm:/sbin/sh

Rgds,
Matti_Kurkela
Honored Contributor
Solution

Re: extract a special string sequence

If you are parsing a Unix /etc/passwd file, then you can use "cut" with a much simpler syntax:

cut -d : -f 3-4 < /etc/passwd | grep "124:"

The cut command gives fields 3 and 4 of the password file (UID and primary GID) alone, then you can use grep to pick the line you want.

MK
MK
Fredrik.eriksson
Valued Contributor

Re: extract a special string sequence

If you have a variable position for your numeric_field this is hard to extract without using a regexp. If you know that lets say the 3 column (with colon as delimiter) is the number you want something like this could be used:

grep ":124:" file.txt | cut -d':' -f3

If you don't know the regexp in the previous post should be more efficient find it.

grep ":124:" | sed "s/^.*:124:\([^:]\+\).*$/\1/"

[^:]\+ is just a better way of stopping at the first colon then using ".*".
[^:]\+ tells sed to search from the position it's initiated from until it runs into a character matching the one behind circumflex (^).

Best regards
Fredrik Eriksson
James R. Ferguson
Acclaimed Contributor

Re: extract a special string sequence

Hi:

# grep -E :124:[0-9]+: file

Regards!

...JRF...
Jose Mosquera
Honored Contributor

Re: extract a special string sequence

Matti, good and easy way to do this!
THX a lot to everybody for you sooner answers.