Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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! – Nov 16 '10 at 15:57
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .