Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I print all $Organs words (from the third field) with one space between each Organ to another? My problem is that each $Organ param have different Organs number so I cant to guess the last field number. I need awk syntax that print the $Organs param from the third field until the last field with one space between the word.

Organs="a bb c ddd ee ff rr ff"
Organs="1 2    3 4 5   6 7 8 9 10 11 12 13 14 15 16 20 21 22"
Organs="I need to lern awk I hope I will do the best"
Organs="I need help"


echo $Organs| awk '{print $3" "$4" "$5" "$6 .......?}'
share|improve this question
add comment

3 Answers

up vote 4 down vote accepted
% echo $Organs | awk '{              \
       for (i = 3; i <= NF; i++) {   \
          printf("%s ", $i);         \
       }                             \
       printf("\n") }'
share|improve this answer
add comment

Try something like this:

for (i = 3; i <= NF; i++)
    printf("%s ", $i);
print ""

NF gives you the number of fields in the line. Take a look at this FAQ:

Agree better suited for stackoverflow.com.

share|improve this answer
    
This outputs a field per line, use printf instead of print. –  Dennis Williamson Nov 16 '10 at 15:36
    
Thanks, corrected! –  icyrock.com Nov 16 '10 at 15:57
add comment
awk '{
  $1=$2=""  # suppress first two fields
  out=substr($0, 3)  # suppress the two leading occurrences of OFS that remain
  print out
}'

This assumes that OFS evaluates to a single space, which is its default value.

This solution may be a bit faster than the others. It conceivably could be slower, too: it depends on whether it's faster to reassign fields and reparse $0, as this solution does, or to implement an awk loop, as the other solutions do. But you're not going to notice the speed difference, either way.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.