1758630 Members
2211 Online
108874 Solutions
New Discussion юеВ

Re: AWK difficulties

 
SOLVED
Go to solution
ROSS HANSON
Regular Advisor

AWK difficulties

On a string, how do I extract the string at field $9 to the end of the line.

It's the "end of the line" I do not understand
Ross Hanson
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: AWK difficulties

Hi Ross:

# X="a b c d e f g h i j k"

# echo $X|awk '{for (i=9;ii j

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: AWK difficulties

Hi (again):

Sorry, the last post has a fence-post error. The code should have been:

# echo $X|awk '{for (i=9;i<=NF;i++) {printf "%s ",$i};printf "\n"
i j k

Now, this said, if you wanted to preserve the exact whitespace spaceing between fields you could use:

# awk '{print substr($0,index($0,$9))}' myfile

...as for example

# cat myfile
a b c d e f g h i j k
a b c d e f g h i j k

awk '{print substr($0,index($0,$9))}' myfile
i j k
i j k

Regards!

...JRF...
ROSS HANSON
Regular Advisor

Re: AWK difficulties

which part tells me from "i" to the end of the line?
Ross Hanson
Hein van den Heuvel
Honored Contributor

Re: AWK difficulties

>>> which part tells me from "i" to the end of the line?

In the first method JRF shows that would be : i=9;i<=NF;i++

The NF is the Number-of-Field = "the end"



In the second method JRF proposes to just look for the byte offset where field 9 starts, and use that as the start of a string to extract. I like that, notably to preserve spacing.
But be careful that field 9 is indeed unique.
I typically 'help' that by adding separators (spaces) around the target string.


$ X="a b c d e if g h i j k l m"
$ echo $X| awk '{print substr($0,index($0,$9))}'
if g h i j k l m
$ echo $X| awk '{print substr($0,index($0," " $9 " ") + 1)}'
i j k l m

Obligatory PERL variant....

$ echo $X| perl -lane 'print join " ",@F[8..@F]'
i j k l m

Print the join of a slice of the array @F, set up by -a, using a space to join.

The slice starts at element 8 ( Perl arrays are 0 based).
It ends at the scalar value for @F which is the number of elements in @F (but in a pinch 999 will work fine as well)

hth,
Hein

James R. Ferguson
Acclaimed Contributor

Re: AWK difficulties

Hi (again):

Your profile shows that you have assigned points to 60 of 436 responses to your questions. When you are satisfied with the help you have received, please assign points as a way of saying "thanks" and as a breadcrumb for future trollers:

http://forums.itrc.hp.com/service/forums/pageList.do?userId=CA238734&listType=unassigned&forumId=1

...JRF...

Laurent Menase
Honored Contributor

Re: AWK difficulties

while read a
do
set -- $a
shift 6
echo $*
done
Hein van den Heuvel
Honored Contributor

Re: AWK difficulties

Ross, thanks for the feedback.

It suggest that we interpreted the question(s) different from the way you intended them.

Can you perhaps provide concrete input and output examples and show what you tried so far?

Can you humor me and explain why you believe that JRF did not really answer the question asked with the limit information available in the question?

Ditto for my further explanation?
I don't need no points, but would like to understand the large disconnect.

Personally I thought Laurent's suggestion was great although admittedly it is not awk as per subject line.

Cheers,
Hein


Laurent Menase
Honored Contributor

Re: AWK difficulties

I mean "shift 8" not 6 to get $9 to end of line

else

cut -d " " -f 9-


to the "end of the line" means usually for instance:
if the line is
1 2 3 4 5 6 7 8 9 10 11 12
$1 is 1
$2 is 2
....
$9 is 9
$9 to the end of the line is
9 10 11 12
ROSS HANSON
Regular Advisor

Re: AWK difficulties

I am just trying to compare two systems. So, I thought by using the awk command to extract only the fields that I wanted would be better than the cut command
Ross Hanson