Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
General
cancel
Showing results for 
Search instead for 
Did you mean: 

Find and print string in textfile

SOLVED
Go to solution
Joakim Brosten
Frequent Advisor

Find and print string in textfile

Hi,

I'm trying to find a neat way of finding a string in a file, then print the string and a number of following characters (or groups of characters/fields).
I've tried the "awk '{print $8,$9,$10}'" but the problem is that the number of fields before the ones I want, changes.
Something like: Search för the string "ABC" inside the file, the print the string/field and following 3 fields.

Any suggestions anyone?

Thanks in advance, /Joakim
5 REPLIES
Wouter Jagers
Honored Contributor
Solution

Re: Find and print string in textfile

I would try something like this:

# cat myfile.txt | sed -n 's/.*\(searchstring.*\)/\1/p' | awk '{print $1,$2,$3}'

This example would first use sed to return the line(s) containing 'searchstring' and cut off everything preceding 'searchstring'.

Then awk will take these lines, where $1 is 'searchstring', $2 is the next field, and so on. Hence, this example would return the searchstring and the two next fields.

Cheers,
Wout
an engineer's aim in a discussion is not to persuade, but to clarify.
James R. Ferguson
Acclaimed Contributor

Re: Find and print string in textfile

Hi Joakim:

Here's one way using 'awk'. For any line that matches your pattern, the pattern and whatever follows to the line's end will be printed. Consider teh example where "HERE" is the pattern for which you want to match:

# X="the pattern I want is HERE to the end!"

# echo ${X} | awk '{OFF=match($0,/HERE/);if (OFF>0) {print substr($0,OFF)}}'

HERE to the end!

Regards!

...JRF...
drb_1
Occasional Advisor

Re: Find and print string in textfile

Hi,

> I'm trying to find a neat way of finding a string in a file,
> then print the string and a number of following characters
> (or groups of characters/fields).

> I've tried the [awk ...]

Any suggestions anyone?

I often use perl (Portable Extraction and Report Language) for such.
Perl is an effective combination of awk, lex, yacc, and lisp.


ksh> cat <> This line should be ignored
> This line should appear ABC foo bar fie ignored
> This line should be ignored
> And this line CAB morefoo
> This line should be ignored
>EOF
> perl -pe '$_="" unless m:^.*([ABC][ABC][ABC])(\s+(\S+))?(\s+(\S+))?(\s+(\S+))?:;$_="$1:$3:$5:$7\n" unless ""eq"$_";'
ABC:foo:bar:fie
CAB:morefoo::


I don't know your level of comfort with perl.
More details are available on further request.

perl -pe = run the given command(s) on each line and print
$_="" unless = throw away any lines not matching the sentinel
m:^... = define what matches
([ABC][ABC][ABC]) = sentinel is ABC, BAC, ACB, CAB, BCA, CBA, assign to $1
(\s+ = match whitespace, assign to $2(, $4, $6)
(\S+ = match non-whitespace, assign to $3(, $5, $7)
))? = close the regular expressions
$_= = assign what to print
"$1:$3:$5:$7" = concatenate match values that begin at 1st, 3rd, 5th, and 7th left parentheses.

See "ksh> perldoc perlre" for further details on the ([:+?]) usage.
See "ksh> perldoc perlrun" for further details on -pe.

Hopefully helpful,
Joakim Brosten
Frequent Advisor

Re: Find and print string in textfile

Thank you fellows,

Wout's solution is just what i need, many thanks.

JRF, this can be very helpful in a other challenge I have, thank you for your contribution.

drb, perl is something that is very interesting, I will look into this in the future. You have done a very good instruction and for me, an introduction, to the world of perl. Thank you.

/Joakim
Joakim Brosten
Frequent Advisor

Re: Find and print string in textfile

Thanks again. /Joakim