- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: extract a special string sequence
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 03:39 AM
тАО04-23-2009 03:39 AM
I need extract this string sequence from a file:
:124:
Where:
1.- Characters ":" will be assumed as field separator.
2.-
3.- This string sequence begins at any column position inside the file.
Please try use grep command for this.
Rgds.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 03:55 AM
тАО04-23-2009 03:55 AM
Re: extract a special string sequence
Please post example data and requireed o/p.
Cheers,
Ravi.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 03:57 AM
тАО04-23-2009 03:57 AM
Re: extract a special string sequence
Perhaps:
# grep ":124:[0-9]*:" file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 04:08 AM
тАО04-23-2009 04:08 AM
Re: extract a special string sequence
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 04:12 AM
тАО04-23-2009 04:12 AM
Re: extract a special string sequence
:123:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 04:12 AM
тАО04-23-2009 04:12 AM
Re: extract a special string sequence
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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 04:19 AM
тАО04-23-2009 04:19 AM
Solutioncut -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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 04:20 AM
тАО04-23-2009 04:20 AM
Re: extract a special string sequence
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 04:26 AM
тАО04-23-2009 04:26 AM
Re: extract a special string sequence
# grep -E :124:[0-9]+: file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-23-2009 07:10 AM
тАО04-23-2009 07:10 AM
Re: extract a special string sequence
THX a lot to everybody for you sooner answers.