# Append lines for a specific pattern

**URL:** <https://community.unix.com/t/append-lines-for-a-specific-pattern/305201>\
**Category:** Shell Programming and Scripting\
**Created:** [February 23, 2012, 4:55am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201 "2012-02-23T04:55:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chitech](https://community.unix.com/letter_avatar/chitech/32/5_5575768a8748004e209b776fc1b2916d.png) [@chitech](https://community.unix.com/u/chitech)\
**Post date:** [February 23, 2012, 4:55am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/1 "2012-02-23T04:55:31Z")

</div>

Input:

```nohighlight
09:43:46,538 INFO first text
10:45:46,538 INFO second
 text
11:00:46,538 INFO third
 more
 text

```

Output:

```nohighlight
09:43:46,538 INFO first text
10:45:46,538 INFO second text
11:00:46,538 INFO third more text

```

The rule is to append all lines so each line contains this format

```nohighlight
\d{2}:\d{2}:\d{2},\d{3}

```

Thx in advance.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 23, 2012, 5:35am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/2 "2012-02-23T05:35:01Z")

</div>

Try:

```nohighlight
awk 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= infile

```

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [February 23, 2012, 5:42am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/3 "2012-02-23T05:42:49Z")

</div>

A lazy way ...

```nohighlight
sed '2,$s/^[0-9]/#&/g' input | xargs | tr '#' '\n'

```

---------- Post updated at 11:42 AM ---------- Previous update was at 11:35 AM ----------

@Scruti

I tried your code on a linux machine : the result is still printed in only one line

```nohighlight
$ cat input
09:43:46,538 INFO first text
10:45:46,538 INFO second
 text
11:00:46,538 INFO third
 more
 text
$ awk 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= input
09:43:46,538 INFO first text10:45:46,538 INFO second text11:00:46,538 INFO third more text

```

---

<div class="post-metadata">

**Author:** ![guruprasadpr](https://community.unix.com/user_avatar/community.unix.com/guruprasadpr/32/1542_2.png) [@guruprasadpr](https://community.unix.com/u/guruprasadpr)\
**Post date:** [February 23, 2012, 5:46am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/4 "2012-02-23T05:46:59Z")

</div>

```nohighlight
awk '/INFO/{if(NR!=1)print "";}{printf $0;}END{print "";}' input

```

Or

```nohighlight
awk '/^[0-9]/{if(NR!=1)print "";}{printf $0;}END{print "";}' input

```

Guru.

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 23, 2012, 6:37am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/5 "2012-02-23T06:37:36Z")

</div>

@ctsgnb: good point. Try:

```nohighlight
awk --posix 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= infile

```

or use:

```nohighlight
awk 'END{print RS} /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/&& NR>1{print RS}1' ORS= infile

```

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [February 23, 2012, 7:36am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/6 "2012-02-23T07:36:36Z")

</div>

@Scruti :

The second one works fine (on a Ubuntu 11.04) :

```nohighlight
$ awk --posix 'END{print RS} /^[0-9]{2}:[0-9]{2}:[0-9]{2}/&& NR>1{print RS}1' ORS= input
awk: not an option: --posix
$ awk 'END{print RS} /^[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/&& NR>1{print RS}1' ORS= input
09:43:46,538 INFO first text
10:45:46,538 INFO second text
11:00:46,538 INFO third more text

```

---------- Post updated at 01:36 PM ---------- Previous update was at 01:20 PM ----------

```nohighlight
awk '/INFO/&&NR>1{print z}{printf $0}END{print z}' input

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [February 23, 2012, 7:56am UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/7 "2012-02-23T07:56:40Z")

</div>

@ctsgnb, it is best to disable the format field in printf to avoid unintended interpretation of possible formatting characters in the input.

```nohighlight
awk '/INFO/&&NR>1{print z}{printf "%s",$0}END{print z}' input

```

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/5_5575768a8748004e209b776fc1b2916d.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [February 23, 2012, 5:46pm UTC](https://community.unix.com/t/append-lines-for-a-specific-pattern/305201/8 "2012-02-23T17:46:11Z")

</div>

In case you don't need to handle a stream...

```nohighlight
printf 'v/INFO/-,j\nx\n' | ex file

```

Regards,  
Alister
