How to print results from two lines using awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to print results from two lines using awk?
# 1  
Old 08-14-2013
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:
Mod Comment Use code tags please, see PM.

Last edited by zaxxon; 08-14-2013 at 09:32 AM.. Reason: code tags
# 2  
Old 08-14-2013
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

This User Gave Thanks to in2nix4life For This Post:
# 3  
Old 08-14-2013
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

This User Gave Thanks to itkamaraj For This Post:
# 4  
Old 08-14-2013
Quote:
Originally Posted by in2nix4life
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
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

# 5  
Old 08-14-2013
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

# 6  
Old 08-16-2013
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.
# 7  
Old 08-16-2013
How that? Post a sample file and the resulting error...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing OSX UNIX command results which print in multiple lines

from the CLI on a Mac, if you type networksetup -listallnetworkservices then you get results in a multi-line paragraph that look something like this: networksetup -listallnetworkservices An asterisk (*) denotes that a network service is disabled. Wi-Fi Display Ethernet Bluetooth DUN... (7 Replies)
Discussion started by: hungryd
7 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

Removing certain lines from results - awk

im using the code below to monitor a file: gawk '{ a += gsub("(^| )accepted( |$)", "&") a += gsub("(^| )open database( |$)", "&") } END { for (i in a) printf("%s=%s\n", i, a) }' /var/log/syslog the code is searching the syslog file for the string "accepted" and "open... (2 Replies)
Discussion started by: SkySmart
2 Replies

4. Shell Programming and Scripting

AWK and print next lines #3 thru #10

I have a output log file, that I want to extract some temperature measurement data. I want to AWK on the words "show chassis environment" in the original file, and extract that entire line, and then the 3rd to 10th lines after the one I AWK'd, into a seperate output file. Here is an example... (3 Replies)
Discussion started by: HikerLT
3 Replies

5. Shell Programming and Scripting

Awk find in columns with "if then" statement and print results

I have a file1.txt file1.txt F-120009210","Felix","U-M-F-F-F-","white","yes","no","U-M-F-F-F-","Bristol","RI","true" F-120009213","Fluffy","U-F-","white","yes","no","M-F-","Warwick","RI","true" U-120009217","Lity","U-M-","grey","yes","yes","","Fall River","MA","true"... (4 Replies)
Discussion started by: charles33
4 Replies

6. Shell Programming and Scripting

awk print lines in a file

Dear All, a.txt A 1 Z A 1 ZZ B 2 Y B 2 AA how can i use awk one line to achieve the result: A Z|ZZ B Y|AA Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

7. UNIX for Dummies Questions & Answers

Awk print all lines on match?

Ok so I can use awk to match a pattern and print the whole line with print $0. Is there any way to just tell awk to print every line of output when the pattern matches? I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following... (9 Replies)
Discussion started by: MrEddy
9 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. Shell Programming and Scripting

How to print only lines in between two strings using awk

Hi, I want to print only lines in between two strings and not the strings using awk. Eg: OUTPUT top 2 bottom 1 left 0 right 0 page 66 END I want to print into a new file only top 2 bottom 1 left 0... (4 Replies)
Discussion started by: jisha
4 Replies

10. Shell Programming and Scripting

How to print number of lines with awk ?

Can some body tell me how to print number of line from a particular file, with sed. ? Input file format AAAA BBBB CCCC SDFFF DDDD DDDD Command to print line 2 and 3 ? BBBB CCCC And also please tell me how to assign column sum to variable. I user the following command it... (1 Reply)
Discussion started by: maheshsri
1 Replies
Login or Register to Ask a Question