3

I would like to change:

e·ver·y·bo·dy

to:

e·ver·y·bo·dy
e·ver·y·bo·
e·ver·y·
e·ver·
e·

A for loop will be greatly preferred.

1
  • so you want to split only the lines which has a dot? Mar 20, 2015 at 7:13

4 Answers 4

2

This also uses a for loop, albeit an awk for loop:

$ echo "e·ver·y·bo·dy" | awk -F· -v OFS=· '{print;for (i=NF;i>1;i--){$i="";print;NF--} }'
e·ver·y·bo·dy
e·ver·y·bo·
e·ver·y·
e·ver·
e·

Alternate version from short to long

$ echo 'e·ver·y·bo·dy' | awk -F· -v OFS=· '{for (i=1;i<=NF;i++){for (j=1;j<=i;j++)printf "%s%s",$j,j<NF?OFS:"";print""} }'
e·
e·ver·
e·ver·y·
e·ver·y·bo·
e·ver·y·bo·dy

Another Variation

$ echo 'e·ver·y·bo·dy' | awk -F· -v OFS=· '{s="";for (i=1;i<=NF;i++){s=s OFS $i; printf "%s",substr(s,2); print (i==NF)?"":OFS} }'
e·
e·ver·
e·ver·y·
e·ver·y·bo·
e·ver·y·bo·dy
9
  • Where is first line (Hi)? and where are dots at the ends? Mar 20, 2015 at 7:12
  • @KasiyA OK. See updated answer.
    – John1024
    Mar 20, 2015 at 7:15
  • You just printed it inside awk (print Hi). If you prefer this solution then you can print all of them by print command. So the answer is not good. Mar 20, 2015 at 7:17
  • 1
    Hey KasiyA, I believe it will work for me as intended. In fact I preferred John1024's first answer, since I will use a for loop to run the script individually for separate words. ex. echo "hi" | awk -F· -v OFS=· '{for (i=NF;i>0;i--){NF=i;print} }'
    – TuxForLife
    Mar 20, 2015 at 7:17
  • @user264974 Your question is not clear. If you prefer this solution you can use printf command to print your desired output and as I seen his first solution doesn't print the dots at the end. if you want that solution I wil l post the shortest solution after editing your QUESTION. Mar 20, 2015 at 7:20
2

You can do it by shorter command as below (long to small):

$ awk -F'·' '{while (NF){ print $0;NF--;ORS="·\n"}}' OFS='·' file
e·ver·y·bo·dy
e·ver·y·bo·
e·ver·y·
e·ver·
e·

And from small to long as following:

$ awk -F'·' '{i=1; while(i<NF){ print tmp=tmp$((i++))"·"} print $0}' file
e·
e·ver·
e·ver·y·
e·ver·y·bo·
e·ver·y·bo·dy
1
  • Got it KasiyA, thank you for your method too, it works great. I like your method a lot too!
    – TuxForLife
    Mar 21, 2015 at 20:24
1

If it has to be a for loop:

IFS=·
for l in $(echo "e·ver·y·bo·dy"); do x="$x$l·"; echo "$x"; done | tac

First we have to set the internal file separator to ·. Then the for loop runs trough each peace and prints it, but in the wrong order. That's why tac at the end reverses the order.

Gives me the output:

e·ver·y·bo·dy·
e·ver·y·bo·
e·ver·y·
e·ver·
e·
2
  • Hey chaos, thanks for a second variation. Your script is almost just what I need, however, I am not seeking the last dot on "e·ver·y·bo·dy·" I think I am going to need the tac or rev commands to reverse the order, so good call on that one
    – TuxForLife
    Mar 20, 2015 at 7:05
  • Hey chaos, thanks for the help, I wish I can give you a vote up for trying to help, but I don't have the rep for it. Cheers!
    – TuxForLife
    Mar 20, 2015 at 7:14
1

Here is a python solution:

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
input_str = 'e·ver·y·bo·dy'
input_list = input_str.split('·')
print input_str
i = len(input_list)
while i > 1:
    print '·'.join(input_list[:i-1]) + '·'
    i -= 1

Here we first split the input string using · as the delimiter to generate a list and then used list slicing and join to get the desired output.

You must log in to answer this question.

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