Operating System - HP-UX
1752801 Members
5289 Online
108789 Solutions
New Discussion юеВ

Re: String search returning offset by fixed number of characters

 
SOLVED
Go to solution
Doug_3
Frequent Advisor

String search returning offset by fixed number of characters

Hi all,
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!
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor
Solution

Re: String search returning offset by fixed number of characters

Hi Doug:

Using your actual data suggests an a minor adjustment in counting, but:

# awk 'NR>3 {exit};{n=match($0,/:/);print substr($0,n,10)} file

Regards!

...JRF...
TwoProc
Honored Contributor

Re: String search returning offset by fixed number of characters

Let's say you want the xyzpart in the first three lines after the semicolon IF the word "reportid" is in the line (you never said tht you where searching for).
$> 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
We are the people our parents warned us about --Jimmy Buffett
Sandman!
Honored Contributor

Re: String search returning offset by fixed number of characters

Use either awk or sed instead of head and cut. What pattern needs matching?

# 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.
Doug_3
Frequent Advisor

Re: String search returning offset by fixed number of characters

Thanks, so far almoooost there. Here is the actual line 3 sample. Note the field to be returned could be one of 16 values ranging from 6 to 8 characters long.
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.
Steven Schweda
Honored Contributor

Re: String search returning offset by fixed number of characters

> I want to return the 8 characters after the
> 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.
Doug_3
Frequent Advisor

Re: String search returning offset by fixed number of characters

Thanks everyone, problem solved.
Sandman!
Honored Contributor

Re: String search returning offset by fixed number of characters

If the field width varies between 6-8 characters then modify the sed as follows:

# sed -n '1,3 s/^.*: \([a-zA-Z0-9]\{6,8\}\).*$/\1/p' file
Doug_3
Frequent Advisor

Re: String search returning offset by fixed number of characters

Steve, thanks for taking the time to respond.

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
Dennis Handly
Acclaimed Contributor

Re: String search returning offset by fixed number of characters

>Steve, thanks for taking the time to respond.

(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.)