Question: I got a ListView and an ArrayList. How to add new items dynamically without lose the existing?
For example I add 'John' using `addChatv2("John")`, but actually delete the exisitng ones.

**Edit:**
The name should be added from another function.

**Edit 2:**
I made chat\_name and arrayAdapter global.

```
private List<String> chat_name;
private ArrayAdapter<String> arrayAdapter;

private String addChatv2(String name) {
lv = (ListView) findViewById(R.id.chatlist);

chat_name = new ArrayList<String>();

arrayAdapter = new ArrayAdapter<String>(
        this,
        android.R.layout.simple_list_item_1,
        chat_name);

lv.setAdapter(arrayAdapter);
//chat_name.add(name);
//arrayAdapter.notifyDataSetChanged();

return name;
}

```

Answer: 
Given:

```
$ cat file
1!
2!
3!
4!
5!
6!
7!
8!
9!
10!
11!
12!

```

You can use awk:

```
$ awk 'NR%4==0{sub(/!$/,"")}1' file
1!
2!
3!
4
5!
6!
7!
8
9!
10!
11!
12

```

And if you have `gawk` you can change in place:

```
$ gawk -i inplace 'NR%4==0{sub(/!$/,"")}1' file
$ cat file
1!
2!
3!
4
5!
6!
7!
8
9!
10!
11!
12

```

If you only have POSIX awk, you can effectively get an inplace replacement by using a temp file:

```
$ awk 'NR%4==0{sub(/!$/,"")}1' file >tmp_file && mv tmp_file file

```

(Which is what GNU sed or GNU awk or perl or ruby is doing under the covers anyway with 'inplace' replacement...)
Perl to the rescue!

```
perl -lpe 'chop if 0 == $. % 4'

```

* `-p` reads the input line by line and prints it after processing
* `-l` removes a newline from the input line and adds it back to output
* [chop](http://p3rl.org/chop) removes the last character
* `$.` is a special [perlvar](http://p3rl.org/perlvar) that contains the input line number, `%` is the modulo [operator](http://p3rl.org/perlop)