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 .......?}'
% echo $Organs | awk '{              \
       for (i = 3; i <= NF; i++) {   \
          printf("%s ", $i);         \
       }                             \
       printf("\n") }'

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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