I have a data frame that such as below. 

```
df <- data.frame(mnth = c("jan", "feb", "feb", "mar", "mar",
                          "mar", "apr", "apr", "apr", "apr", 
                          "may", "may", "may", "may", "may"),
                 n = c(1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5),
                 value = c(5, 1, 3, 2, 8, 0, 6, 0, 2, 7, 2, 1, 4, 2, 6))

```

I want to add the corresponding number in the `value` field for each value of the `n` field. 

In this case, the answer should be:  

16, 12, 6, 9, 6 

```
16 = 5 + 1 + 2 + 6 + 2  # all rows where 'n' = 1
12 = 3 + 8 + 0 + 1      # all rows where 'n' = 2
6  = 0 + 2 + 4          # all rows where 'n' = 3
9  = 7 + 2              # all rows where 'n' = 4
6                       # all rows where 'n' = 5

```

How can I write the for loop to add the numbers in R?
['https://Stackoverflow.com/questions/50744257', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5323536/']
Use a regular expression that matches the specified string followed by anything, and the `-o` option to `grep` so it only returns the part of the line that matched:

```
grep -o 'how.*' file.txt

```
You could use sed for this

```
$ cat file.txt
Hello, how are you?
$ sed -r "s/^.+(how.+)$/\1/" file.txt
how are you?
$

```

this used sed regex to anchor the text you want to start with - in this case, the word "how" and terminate when it finds the end of the line.