# Combine multiple lines in single line

**URL:** <https://community.unix.com/t/combine-multiple-lines-in-single-line/276146>\
**Category:** Shell Programming and Scripting\
**Created:** [October 26, 2010, 10:24am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146 "2010-10-26T10:24:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_One](https://community.unix.com/letter_avatar/the_one/32/5_5575768a8748004e209b776fc1b2916d.png) [@The\_One](https://community.unix.com/u/The_One)\
**Post date:** [October 26, 2010, 10:24am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/1 "2010-10-26T10:24:58Z")

</div>

This is related to one of my previous post but now with a slight difference: I need the "Updated:" to be in one line as well as the "Information:" on one line as well. These are in multiple lines right now as seen below. These can have 2 or more lines that needs to be in one line.

```nohighlight
System name: xxx_yyy
Information: this is just a sample
file for this testing
today
Updated: This is not
updated
System name: testing_123
Information: another testing
Updated: No new update for this 
system name.
System name: abc_12333
Information: another testing again
and again
Updated: This has been updated
with new information

```

There are hundred lines with the above pattern.

Result:

```nohighlight
System name: xxx_yyy
Information: this is just a sample file for this testing today
Updated: This is not updated
System name: testing_123
Information: another testing again
Updated: No new update for this system name.
System name: abc_12333
Information: another testing again and again
Updated: This has been updated with new information

```

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [October 26, 2010, 10:55am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/2 "2010-10-26T10:55:12Z")

</div>

```nohighlight
sed '
  :loop
  $b
  N
  /: .*\n.*: /{
    P
    s/.*\n//
    b loop
   }
  s/ *\n */ /
  b loop
 '

```

Narrative: This is what I call a sed looper, reading it's own lies after the first rather than exiting the script at the end to get the next line:

1. declare a branch target 'loop', 
2. if last line, print and exit,
3. If the two lines in the buffer each have ': ', print out the first line and go back to 'loop' to get another line.
4. Remove the linefeed and any adjacent spaces in favor of a single space.
5. Go back to 'loop' to get another line.

---

<div class="post-metadata">

**Author:** ![JerryHone](https://community.unix.com/letter_avatar/jerryhone/32/5_5575768a8748004e209b776fc1b2916d.png) [@JerryHone](https://community.unix.com/u/JerryHone)\
**Post date:** [October 26, 2010, 11:10am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/3 "2010-10-26T11:10:03Z")

</div>

Or with awk...

nawk -F : -f my.awk my.txt

my.awk

```nohighlight
BEGIN {
        line=""
}

{

        if ( $1 == "System name" || $1 == "Information" || $1 == "Updated" ) {
                if ( length(line) > 0 ) { print line }
                line = $0
        } else {
                line = sprintf("%s %s", line, $0)
        }

}

END {
        print line
}

```

---

<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:** [October 26, 2010, 11:19am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/4 "2010-10-26T11:19:58Z")

</div>

```nohighlight
awk '/: /{if(NR>1)print p;p=$0;next}{p=p FS $0}END{print p}' infile

```

---

<div class="post-metadata">

**Author:** ![The\_One](https://community.unix.com/letter_avatar/the_one/32/5_5575768a8748004e209b776fc1b2916d.png) [@The\_One](https://community.unix.com/u/The_One)\
**Post date:** [October 26, 2010, 11:24am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/5 "2010-10-26T11:24:00Z")

</div>

> [@dgpickett](#):
>
> ```plaintext
> sed '
> :loop
> $b
> N
> /: .*\n.*: /{
> P
> s/.*\n//
> b loop
> }
> s/ *\n */ /
> b loop
> '
> 
> ```
> 
> Narrative: This is what I call a sed looper, reading it's own lies after the first rather than exiting the script at the end to get the next line:
> 
> 1. declare a branch target 'loop', 
> 2. if last line, print and exit,
> 3. If the two lines in the buffer each have ': ', print out the first line and go back to 'loop' to get another line.
> 4. Remove the linefeed and any adjacent spaces in favor of a single space.
> 5. Go back to 'loop' to get another line.

you really are a genius! I wanted to learn more in this as im using this kind of text editing frequently. Do you know any reference on this kind of sed function where i can learn more?

---

<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:** [October 26, 2010, 11:37am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/6 "2010-10-26T11:37:50Z")

</div>

```nohighlight
sed -n '/:/{x;1!s/\n/ /g;1!p;d;};H;${g;s/\n/ /g;p;}' 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:** [October 26, 2010, 11:48am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/7 "2010-10-26T11:48:07Z")

</div>

```nohighlight
echo `sed 's/^\(.*\):/|\1:/' input | tr -d '\n'` | sed 's/^|//' | tr '|' \\n

```

---

<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:** [October 26, 2010, 11:58am UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/8 "2010-10-26T11:58:36Z")

</div>

```nohighlight
sed -n '1h;1d;/:/ba;H;$!d;:a;x;s/\n/ /g;p;' infile

```

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [October 26, 2010, 12:15pm UTC](https://community.unix.com/t/combine-multiple-lines-in-single-line/276146/9 "2010-10-26T12:15:30Z")

</div>

I believe everyone is entitled to beautified code -- it prevents mistakes. A sed looper should not try to filter-transform, because lines arrive in two or more places. You can just pipe one sed to the next. The big trick to sed is regex, learning how to tame the hungry wild card .\* by either tying it to the end of line to find the first: ' ._$' or by using a value-restricted list: '^[^]_ '. Then you can delineate the area you need to substring with \(\) which become \1-\9. The regex skills are useful in vi, ex, C/C++, PERL, awk, ksh command line editing. The regex meta-character set varies by situation and age/source of library, expanding in sed in one direction and in egrep in another direction. You can find the basics here with man regex or search regex. The more complicated regex extensions fill many web age articles. Here is an advanced regex dedicated site:

[Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns](http://www.regular-expressions.info/)
