- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- String search returning offset by fixed number of ...
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
тАО12-13-2007 02:47 PM
тАО12-13-2007 02:47 PM
I am seeking a simple way to perform a ksh string search of a file in the first 3 lines. Once found return the 8 characters offset to the left of the pattern matched.
Line 3 looks like this:
stuffstuffstuff reportid: xyz123456
I want to return the 8 characters after the semicolon, in this case 'xyz12345'.
I have been messing around with various incantations of head and cut to minimal success.
Thanks in advance,
Doug
P.S. I am generous with points!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2007 03:05 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2007 03:13 PM
тАО12-13-2007 03:13 PM
Re: String search returning offset by fixed number of characters
$> cat junk
stuffstuffstuff junkid: xyz123456
nostuffy reportid: xyz123456
imstuffy noid: xyz123456
yourstuff something: xyz123456
snuffysmith nothing: xyz123456
$> head junk|grep reportid| cut -f2 -d:
xyz123456
Well, there's a space there so you can get rid of it if you prefer with one more cut...
$> head junk| grep reportid | cut -f2 -d: | cut -f2 -d" "
xyz123456
Or, if you don't particularly like using a second cut, echo is a handy space eater:
$> echo `head junk| grep reportid | cut -f2 -d:`
xyz123456
Also, people don't like the old "`" backslash anymore, so you can use the string from subshell approach (it's what I call it anyways):
#> echo $(head junk| grep reportid | cut -f2 -d:)
xyz123456
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2007 03:49 PM
тАО12-13-2007 03:49 PM
Re: String search returning offset by fixed number of characters
# sed -n '1,3 s/^.*: \([a-zA-Z0-9]\{8\}\).*$/\1/p' file
...above cmd scans first 3 lines and prints the 8 characters after the semicolon.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2007 03:57 PM
тАО12-13-2007 03:57 PM
Re: String search returning offset by fixed number of characters
FRI, NOV 02, 2007, 2:42 PM --req: BSI-------leg: GL ----loc: BI-TECH---job: 1087547 #S022---prog: CK200
<1.40>--report id: CKREG---
In other words CKREG--- has been padded with dashes --- to fill in the 8 characters after report id:
It might be PY240AAA to be returned with out any dashes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2007 04:02 PM
тАО12-13-2007 04:02 PM
Re: String search returning offset by fixed number of characters
> semicolon, in this case 'xyz12345'.
What semicolon? I see a colon. And the
first character after this colon is a space,
not "x".
> [...] the old "`" backslash [...]
That's not a backslash, "\", either.
As the spread of suggested solutions might
suggest, a more precise (and/or accurate)
description of the actual problem to be
solved might help. Actual sample data with
expected results might be nice, too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2007 04:08 PM
тАО12-13-2007 04:08 PM
Re: String search returning offset by fixed number of characters
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-13-2007 04:17 PM
тАО12-13-2007 04:17 PM
Re: String search returning offset by fixed number of characters
# sed -n '1,3 s/^.*: \([a-zA-Z0-9]\{6,8\}\).*$/\1/p' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-14-2007 08:19 AM
тАО12-14-2007 08:19 AM
Re: String search returning offset by fixed number of characters
Sheesh dude, I was looking for and got quick and dirty advice. If I'd known I'd be flamed for poking my head up over the electronic cube wall and asking folks for help I'll post somewhere else.
But specifically, to rebut you a bit, as you can see from the original post I DID provide sample data. I also provided a sample solution. As far as the issue with the semicolon vs colon, well, it's kinda irrelevant isn't it? I wanted to pattern match, so the code shouldn't care what the pattern is :; or $, just match it.
I have been treating this site as a place to run ideas down, get quick advice, and solve problems. Perhaps it has changed over time and it is now a place where perfect spec's and exact documentation is necessary before the poster isn't partially flamed. I was just trying so solve a problem by electronically standing up and leaning over the cube wall and asking a question. I wasn't trying to c++ a multi-variate wind tunnel algorithm.
Hope your Friday goes well. I am just thankful for the super fast and efficient help that was generated by the post, thanks to James, Sandman and TwoProc!!!!!!!!!
Regards, Doug
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-15-2007 12:06 AM
тАО12-15-2007 12:06 AM
Re: String search returning offset by fixed number of characters
(You could assign him 0 points. :-)
>it's kinda irrelevant isn't it? I wanted to pattern match, so the code shouldn't care what the pattern is :; or $, just match it.
That won't work. The solutions given rely on the ":" being there. Of course you could simply replace it by what you have, once you get the idea.
>where perfect spec's and exact documentation is necessary before the poster isn't partially flamed.
Accurate info is always needed. Especially in a forum like sys admin where the wrong info will crash your box. languages and scripting is more appropriate to your question and may be more user friendly.
(I would probably have pointed out your problems but given a solution anyway.)