How to print results from two lines using awk? | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

How to print results from two lines using awk?

UNIX for Dummies Questions & Answers


Tags
awk, between strings, grep, more than 1 line, print

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-14-2013
danegon danegon is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 21 August 2013, 6:45 AM EDT
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
Linux How to print results from two lines using awk?

I need to print a specific string from an html file that's always occurring between two other known strings. Example: from the text below, I would like to print the bolded part:


Code:
<this is a lot of text before the string I want 
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows 
the string I want to print>

I'm using a grep command that greps a specific string (an error type) plus 5 lines before this error (that's where my string always occurs), plus an awk command to print the specific text after "fullpath:" and before "-cfver". Here's how my command looks like:


Code:
find -name 'my_file_name.html' -exec grep -B 5 'error type displayed' {} \; -print | more | awk -F "fullpath\:" '{print $2}' | awk -F "\-cfver" '{print $1}'

My problem is that this only prints only 1 line for each occurrence, so if the string that I want to print is happening in more than one line, the result appears truncated. Example:


Code:
abc/def/ghi/examp

How can I get printed only the strings between fullpath: and -cfver even if they occur in more than 1 line?

Thanks in advance!

Moderator's Comments:
Use code tags please, see PM.

Last edited by zaxxon; 08-14-2013 at 08:32 AM.. Reason: code tags
Sponsored Links
    #2  
Old 08-14-2013
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 9 October 2014, 3:02 PM EDT
Location: East Coast
Posts: 611
Thanks: 1
Thanked 176 Times in 162 Posts

Code:
 cat file

<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>
<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>
<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>
<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>

awk '/fullpath/{match($0, /fullpath: (.*) \-cfver/, x); print x[1]}' file

abc/def/ghi/example.xlf
abc/def/ghi/example.xlf
abc/def/ghi/example.xlf
abc/def/ghi/example.xlf

The Following User Says Thank You to in2nix4life For This Useful Post:
danegon (08-14-2013)
Sponsored Links
    #3  
Old 08-14-2013
itkamaraj's Avatar
itkamaraj itkamaraj is offline Forum Advisor  
^Kamaraj^
 
Join Date: Apr 2010
Last Activity: 3 October 2014, 8:50 AM EDT
Posts: 3,059
Thanks: 33
Thanked 659 Times in 638 Posts

Code:
$ cat a.txt
<this is a lot of text before the string I want 
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows 
the string I want to print>

<this is a lot of text before the string I want 
to print> fullpath: abc/def/ghi
/example.xlf -cfver. <sample text that follows 
the string I want to print>

<this is a lot of text before the string I want 
to print> fullpath: abc/def
/ghi/
example.xlf -cfver. <sample text that follows 
the string I want to print>

$ awk '/fullpath/,/\-cfver/' a.txt | sed 's/.*fullpath://;s/\-cfver.*//'
 abc/def/ghi/example.xlf 
 abc/def/ghi
/example.xlf 
 abc/def
/ghi/
example.xlf

The Following User Says Thank You to itkamaraj For This Useful Post:
danegon (08-14-2013)
    #4  
Old 08-14-2013
danegon danegon is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 21 August 2013, 6:45 AM EDT
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by in2nix4life View Post
Code:
 cat file

<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>
<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>
<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>
<this is a lot of text before the string I want
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows
the string I want to print>

awk '/fullpath/{match($0, /fullpath: (.*) \-cfver/, x); print x[1]}' file

abc/def/ghi/example.xlf
abc/def/ghi/example.xlf
abc/def/ghi/example.xlf
abc/def/ghi/example.xlf

Thanks for your help, but your command does not return any results. It just prints empty lines.

---------- Post updated at 09:54 AM ---------- Previous update was at 09:50 AM ----------

Quote:
Originally Posted by itkamaraj View Post
Code:
$ cat a.txt
<this is a lot of text before the string I want 
to print> fullpath: abc/def/ghi/example.xlf -cfver. <sample text that follows 
the string I want to print>

<this is a lot of text before the string I want 
to print> fullpath: abc/def/ghi
/example.xlf -cfver. <sample text that follows 
the string I want to print>

<this is a lot of text before the string I want 
to print> fullpath: abc/def
/ghi/
example.xlf -cfver. <sample text that follows 
the string I want to print>

$ awk '/fullpath/,/\-cfver/' a.txt | sed 's/.*fullpath://;s/\-cfver.*//'
 abc/def/ghi/example.xlf 
 abc/def/ghi
/example.xlf 
 abc/def
/ghi/
example.xlf

Hi itkamaraj,

Awesome! Your solution works fine. Sorry to insist, but is there a way to have carriage returns displayed in the same lines instead of in different ones? Example:


Code:
 
abc/def/ghi/example.xlf 
abc/def/ghi/example.xlf 
abc/def/ghi/example.xlf

Sponsored Links
    #5  
Old 08-14-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 23 October 2014, 9:45 AM EDT
Location: Aachen, Germany
Posts: 4,420
Thanks: 73
Thanked 1,079 Times in 1,018 Posts
Try
Code:
awk '/fullpath/{while ($0!~/-cfver/) {getline x; $0=$0x};gsub (/^.*fullpath: | -cfver.*$/,""); print} ' file 
abc/def/ghi/example.xlf
abc/def/ghi/example.xlf
abc/def/ghi/example.xlf

Sponsored Links
    #6  
Old 08-16-2013
danegon danegon is offline
Registered User
 
Join Date: Aug 2013
Last Activity: 21 August 2013, 6:45 AM EDT
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
Hi RudiC,

Thanks for your help, but your command returns also text that's between the required strings. Somehow it does pick strings that are not required.
Sponsored Links
    #7  
Old 08-16-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 23 October 2014, 9:45 AM EDT
Location: Aachen, Germany
Posts: 4,420
Thanks: 73
Thanked 1,079 Times in 1,018 Posts
How that? Post a sample file and the resulting error...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK and print next lines #3 thru #10 HikerLT Shell Programming and Scripting 3 08-16-2012 03:54 PM
Awk find in columns with "if then" statement and print results charles33 Shell Programming and Scripting 4 01-09-2012 04:41 AM
print first few lines, then apply regex on a specific column to print results. kchinnam Shell Programming and Scripting 4 08-24-2010 03:24 PM
print multiple lines with awk djcsabus UNIX for Dummies Questions & Answers 6 06-18-2009 08:27 AM
How to print only lines in between two strings using awk jisha Shell Programming and Scripting 4 01-11-2008 03:13 AM



All times are GMT -4. The time now is 02:32 PM.