Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing range of fields $5..$NF, printing from search to end of line

Status
Not open for further replies.

keusch

Technical User
Jun 17, 2001
41
US
I have two questions:
Question 1
From the following I want to print from $5 to the end of the line.
The script searched for/ Remarks on/ but then I only want to print from FRAC....SI /15. The length of this field will change each time as will the 001 that preceeds the statement.

input:
Remarks on PT Test 001 FRAC OIL WAS GELLED-FLUSH W/UNGELLED OIL ISIP 5900, SIP /15
Remarks on IP Test 004 ADDTV-280000 SCF NITROGEN

desired output
FRAC OIL WAS GELLED-FLUSH W/UNGELLED OIL ISIP 5900, SIP /15
ADDTV-280000 SCF NITROGEN


Question 2
The script searches for /Stages:/ And I only want to print whats on the line after Remarks:

ininput:
Stages: Remarks: TP 6200-6800
Stages: 12 stage Remarks: rerun stages 4 and 8

desired output:
TP 6200-6800
rerun stages 4 and 8

Can anyone help??
 
These are not tested but they might be close to what you want.
Code:
for (j=5;j<NF;j++) printf $j &quot; &quot;
print $j

for (j=2;j<NF && $j != &quot;Remarks:&quot;; j++) ;
for (k=j;k<NF;k++) printf $k &quot; &quot;
print $k
Hope this helps. CaKiwi
 
nawk -v chop=5 '{ for(i=1; i<=chop; i++) $i=&quot;&quot;; sub(&quot;^[ ]*&quot;, &quot;&quot;); print; } filename

There used to be a more terse and graceful solution to this &quot;shifting&quot; problem dealing with NF, but I can't find it my archives.

vlad
 
nawk -v chop=5 '{ for(i=1; i<=chop; i++) $i=&quot;&quot;; sub(&quot;^[ ]*&quot;, &quot;&quot;); print; }' filename

There used to be a more terse and graceful solution to this &quot;shifting&quot; problem dealing with NF, but I can't find it my archives.

vlad
 
Here's a one-liner for question 2.

sed '/^Stages:/\!d;s/.*Remarks: //' filename

The '^' means I assumE Stages: is at the start of the line.

Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
If you version of awk has the match function then here's an easy solution for question 1. By the way, it seems that your output wants to print from the 6th field to the end.

awk 'print substr($0,match($0,$6))' filename
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Whops,

to correct, that q1 soln needs braces

awk '{print substr($0,match($0,$6))}' filename
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
ND,

nice one, but......... it breaks if &quot;$6&quot; also appears PRIOR to the 6-th position:

Remarks on PT Test 001 001 FRAC OIL WAS GELLED-FLUSH W/UNGELLED OIL ISIP 5900, SIP /15



 
Vlad -

Good catch! I also noticed that not only will a prior &quot;$6&quot; in the line change the result but if the value of field $6, in this case &quot;FRAC&quot;, appears prior then it breaks early as well. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top