3

I have a file that holds output from a test.

test 1  
42  
test 2  
69  
test 3  
420  
test 4  
55378008  

I would like to make the test output appear on the same line as the test name. like so:

test 1:    42  
test 2:    69  
test 3:    420  
test 4:    55378008  

I am sure there is some fancy sed, awk or perl way to do this but I am stuck.

Lenna
  • 1,220
  • 6
  • 22
  • The file is not double spaced! – user3439894 Mar 24 '20 at 04:27
  • You are right, the title was misleading. I think "Merge file lines" may be a better title – Lenna Mar 24 '20 at 04:30
  • It's not just the (old) title, it's also "I am aware of how sed G double spaces a file, I was hoping for a possible inverse of the sed G command to un-double space a file" really has nothing to do with what you want done. – user3439894 Mar 24 '20 at 04:32
  • @user3439894 see https://stackoverflow.com/questions/45549402/how-to-find-continuous-blank-lines-and-convert-them-to-one – Sundeep Mar 24 '20 at 09:03

6 Answers6

5

And here is another one in sed flavor to complete the offer :

sed 'N ; s/\n/: /' input_file

For each (odd) line starting from the first, append the next (even) one in pattern space separated by a LF, then just replace this LF by :.

luciole75w
  • 1,109
  • 6
  • 12
3
awk 'FNR%2{printf "%s: ", $0; next}1' file

This prints odd lines with suffix : and without newline and even lines with a newline.

Freddy
  • 4,548
  • 1
  • 7
  • 17
3

pr has this built-in, but if you need whitespace adjustment as well, then sed/awk/perl solutions suggested in other answers will suit you better

$ pr -2ats': ' ip.txt 
test 1: 42
test 2: 69
test 3: 420
test 4: 55378008

This combines 2 lines at a time with : as the separator.

Sundeep
  • 23,246
  • 2
  • 28
  • 103
2

Just replace the line feed of odd lines with :␠.

perl -pe's/\n/: / if $. % 2'

You have mentioned that you want to removing leading and trailing whitespace as well. For that, you can use the following:

perl -pe's/^\h+|\h+$/g; s/\n/: / if $. % 2'

Specifying file to process to Perl one-liner

ikegami
  • 367,544
  • 15
  • 269
  • 518
1

A shell solution, which is very slow on large set of data/files.

while IFS= read -r odd_line; do
  IFS= read -r even_line
  printf '%s: %s\n' "$odd_line" "$even_line"
done < file.txt

On the other hand if the colon is not a requirement paste can do the job.

paste - - < file.txt
Jetchisel
  • 7,493
  • 2
  • 19
  • 18
0

Bash solution

skips empty lines

process both UNIX/DOS format 'end of line'

accepts filename as argument or otherwise reads data from STDIN

#!/bin/bash

while read p1
do
  [[ -z $p1 ]] && continue
  # p1=`echo -n $p1 | tr -d "\r"`  # replaced with following line
  p1=${p1//$'\r'/}
  read p2
  echo -n "$p1: $p2"
done < ${1:-/dev/stdin}

Output

test 1: 42
test 2: 69
test 3: 420
test 4: 55378008

NOTE: no empty lines allowed between lines for join

Community
  • 1
  • 1
Polar Bear
  • 6,762
  • 1
  • 5
  • 12
  • you can try `p1=${p1//$'\r'/}` instead. – Jetchisel Mar 24 '20 at 06:35
  • @Jetchisel -- hmm, it does work. Can you refer me where this 'construction' documented? I never seen it before. – Polar Bear Mar 24 '20 at 06:50
  • Parameter Expansion and `$' '` is a bash specific shell quoting. – Jetchisel Mar 24 '20 at 06:52
  • @Jetchisel -- thank you, now it is a question how to memorize this construction or where I can find it. It is nice feature which very useful at some situations. I will change my answer to use your approach -- at least it will be a reference for future. – Polar Bear Mar 24 '20 at 07:08
  • @Jetchisel -- Ok, I found where this construction is [described](https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html). – Polar Bear Mar 24 '20 at 07:16
  • It has more `/` that it should, should be `p1=${p1//$'\r'}` less the last `/` my bad sorry. – Jetchisel Mar 24 '20 at 07:27
  • @Jetchisel -- double `//` has meaning more than one, `/` has meaning only one, both should work fine in this case (although `//` is excessive in this particular case). – Polar Bear Mar 24 '20 at 17:46
  • I'm not talking about the `//` but the last `/`. I just showed you. – Jetchisel Mar 25 '20 at 20:53