- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Find and print string in textfile
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-15-2007 09:27 PM
тАО04-15-2007 09:27 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-16-2007 01:41 AM
тАО04-16-2007 01:41 AM
Solution# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-16-2007 06:37 AM
тАО04-16-2007 06:37 AM
Re: Find and print string in textfile
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-16-2007 09:28 AM
тАО04-16-2007 09:28 AM
Re: Find and print string in textfile
> 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 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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-16-2007 06:46 PM
тАО04-16-2007 06:46 PM
Re: Find and print string in textfile
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО04-16-2007 09:25 PM
тАО04-16-2007 09:25 PM