Printing from field 14 to end of record

  • Follow


Hi,

How do I print from from a certain field to the end of the record? 
For example, I have an input record that has data I want starting at
field 14 ($14). From that point until the end of the record, I'd like
to print it all out.  The input records could be exactly 14 fields in
length or several more.

TIA,
-Tennis
0
Reply tennis_smith 10/10/2003 4:26:16 AM


On 10/9/2003 11:26 PM, Tennis Smith wrote:
> Hi,
> 
> How do I print from from a certain field to the end of the record? 
> For example, I have an input record that has data I want starting at
> field 14 ($14). From that point until the end of the record, I'd like
> to print it all out.  The input records could be exactly 14 fields in
> length or several more.
> 
> TIA,
> -Tennis

awk '{ for (i=14; i<=NF; i++) print $i }'

Regards,

	Ed.

0
Reply Ed 10/10/2003 5:03:08 AM


In article <7f477f72.0310092026.b692cf5@posting.google.com>,
Tennis Smith <tennis_smith@yahoo.com> wrote:

% How do I print from from a certain field to the end of the record? 
% For example, I have an input record that has data I want starting at
% field 14 ($14). From that point until the end of the record, I'd like
% to print it all out.  The input records could be exactly 14 fields in
% length or several more.

If the FS is always a single character, you can simply print from
fields 15 to NF in a loop. If you need to preserve, for instance,
variable whitespace, then you need to locate the end of field 14
and print the rest using substr. My preferred way of doing this
is to construct an RE which matches the first 14 fields, then
use match() to get their offsets. For the default field separator,
this is something like
 BEGIN {
  invre = "^[ \t]*"
  for (i = 1; i <= 14; i++)
     invre = invre "[^ \t]+[ \t]+"
 }

 {
  if (match($0, invre))
     print substr($0, RSTART+RLENGTH)
  else
     print ""
 }
-- 

Patrick TJ McPhee
East York  Canada
ptjm@interlog.com
0
Reply ptjm 10/10/2003 5:30:20 PM

On Fri, 10 Oct 2003 00:03:08 -0500, Ed Morton 
  <mortonAVOIDINGSPAM@Lucent.com> wrote:
> 
> 
> On 10/9/2003 11:26 PM, Tennis Smith wrote:
>> Hi,
>> 
>> How do I print from from a certain field to the end of the record? 
>> For example, I have an input record that has data I want starting at
>> field 14 ($14). From that point until the end of the record, I'd like
>> to print it all out.  The input records could be exactly 14 fields in
>> length or several more.
>> 
>> TIA,
>> -Tennis
> 
> awk '{ for (i=14; i<=NF; i++) print $i }'
> 
That would print a new line for each field.  Perhaps printf "%s ",$i 
would be better, with printf "\n" after the loop.

-- 
Cheops' Law:
	Nothing ever gets built on schedule or within budget.
0
Reply Bill 10/10/2003 7:18:16 PM

Tennis Smith wrote:
> Hi,
> 
> How do I print from from a certain field to the end of the record? 
> For example, I have an input record that has data I want starting at
> field 14 ($14). From that point until the end of the record, I'd like
> to print it all out.  The input records could be exactly 14 fields in
> length or several more.
> 
> TIA,
> -Tennis

I had the exact requirement except at field 5 so heres what I did:

print substr($0, match( $0, $5 ));

should print all fields after the 4th. My requirements were that the 5th 
field was the last field (a comment field) and contained spaces.

Caio-caio bambino,
Lisa
0
Reply Lisa 2/4/2006 8:41:18 PM

Lisa wrote:
> Tennis Smith wrote:
> > Hi,
> >
> > How do I print from from a certain field to the end of the record?
> > For example, I have an input record that has data I want starting at
> > field 14 ($14). From that point until the end of the record, I'd like
> > to print it all out.  The input records could be exactly 14 fields in
> > length or several more.
> >
> > TIA,
> > -Tennis
>
> I had the exact requirement except at field 5 so heres what I did:
>
> print substr($0, match( $0, $5 ));
>
> should print all fields after the 4th. My requirements were that the 5th
> field was the last field (a comment field) and contained spaces.

What if $0 is
the sloppy thinking is the problem

0
Reply William 2/4/2006 9:48:51 PM

William James wrote:
> Lisa wrote:
> 
>>Tennis Smith wrote:
>>
>>>Hi,
>>>
>>>How do I print from from a certain field to the end of the record?
>>>For example, I have an input record that has data I want starting at
>>>field 14 ($14). From that point until the end of the record, I'd like
>>>to print it all out.  The input records could be exactly 14 fields in
>>>length or several more.
>>>
>>>TIA,
>>>-Tennis
>>
>>I had the exact requirement except at field 5 so heres what I did:
>>
>>print substr($0, match( $0, $5 ));
>>
>>should print all fields after the 4th. My requirements were that the 5th
>>field was the last field (a comment field) and contained spaces.
> 
> 
> What if $0 is
> the sloppy thinking is the problem
> 
In my case I already matched field 1, using that to load the values from 
fields 2 through 4.  And, as I stated before, 5 is a comment field (is 
multiple fields since there will most likely be spaces in the comments)

Sorry I didn't context that here, my apologies for the sloppy suggestion!

Caio,
Lisa
0
Reply Lisa 2/5/2006 1:44:08 AM

William James wrote:

> Lisa wrote:
> 
>>Tennis Smith wrote:
>>
>>>Hi,
>>>
>>>How do I print from from a certain field to the end of the record?
>>>For example, I have an input record that has data I want starting at
>>>field 14 ($14). From that point until the end of the record, I'd like
>>>to print it all out.  The input records could be exactly 14 fields in
>>>length or several more.
>>>
>>>TIA,
>>>-Tennis
>>
>>I had the exact requirement except at field 5 so heres what I did:
>>
>>print substr($0, match( $0, $5 ));
>>
>>should print all fields after the 4th. My requirements were that the 5th
>>field was the last field (a comment field) and contained spaces.

I'd be worried about the text of $5 appearing earlier in $0:

$ echo "a b c d e f g" | awk '{print substr($0, match( $0, $5 ))}'
e f g
$ echo "a b c d c f g" | awk '{print substr($0, match( $0, $5 ))}'
c d c f g

Do this instead (as I just posted in your other thread):

$ echo "a b c d c f g" | gawk --re-interval 
'sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){4}/,"")'
c f g

Regards,

	Ed.
0
Reply Ed 2/5/2006 1:45:10 AM

Ed Morton wrote:
> William James wrote:
> 
>> Lisa wrote:
>>
>>> Tennis Smith wrote:
>>>
>>>> Hi,
>>>>
>>>> How do I print from from a certain field to the end of the record?
>>>> For example, I have an input record that has data I want starting at
>>>> field 14 ($14). From that point until the end of the record, I'd like
>>>> to print it all out.  The input records could be exactly 14 fields in
>>>> length or several more.
>>>>
>>>> TIA,
>>>> -Tennis
>>>
>>>
>>> I had the exact requirement except at field 5 so heres what I did:
>>>
>>> print substr($0, match( $0, $5 ));
>>>
>>> should print all fields after the 4th. My requirements were that the 5th
>>> field was the last field (a comment field) and contained spaces.
> 
> 
> I'd be worried about the text of $5 appearing earlier in $0:
> 
> $ echo "a b c d e f g" | awk '{print substr($0, match( $0, $5 ))}'
> e f g
> $ echo "a b c d c f g" | awk '{print substr($0, match( $0, $5 ))}'
> c d c f g
> 
> Do this instead (as I just posted in your other thread):
> 
> $ echo "a b c d c f g" | gawk --re-interval 
> 'sub(/^[[:space:]]*([^[:space:]]*[[:space:]]*){4}/,"")'
> c f g
You're right, I had missed that.  Since I was controlling the config 
file, I assumed something that later could have broken and been a pain 
to debug.  In my case, I think I'll drop the whole awk code and go back 
to finding a shell version since I went way off on a tanget working on 
this one.

Thanks!
Lisa
0
Reply Lisa 2/5/2006 6:40:50 PM

8 Replies
274 Views

(page loaded in 0.343 seconds)

Similiar Articles:













7/24/2012 11:38:18 AM


Reply: