0

After my grep, I generated a file which has data as key and count but on separate lines:

$Value, "Some", $233kS
:2343
$AnotherCount, JunkValue
:38585
YetAnother, Nothing
:38484

I want a file like:

$Value, "Some", $233kS:2343
$AnotherCount, JunkValue:38585
YetAnother, Nothing:38484

The count pattern is fixed, it is always of form :[0-9]* Is it possible using sed or any single line command?

I looked at replace line but I want only when the count pattern is not matched.

I am interested in solution that can work for extended problem:

$Value, "Some", $233kS
$AnotherCount, JunkValue
:38585
YetAnother, Nothing
:38484

Should output:

$Value, "Some", $233kS$AnotherCount, JunkValue:38585
YetAnother, Nothing:38484

Basically, all the lines not matching the pattern should not have end line char.

impossible
  • 2,380
  • 8
  • 36
  • 60
  • `when the count pattern is not matched` No? It looks you want to match it and remove a newline in front of it. `s/\n\(:[0-9]*\)/\1/g` – KamilCuk Jul 14 '20 at 12:52
  • 1
    Does this answer your question? [How do I join pairs of consecutive lines in a large file (1 million lines) using vim, sed, or another similar tool?](https://stackoverflow.com/questions/8545538/how-do-i-join-pairs-of-consecutive-lines-in-a-large-file-1-million-lines-using) – Inian Jul 14 '20 at 12:55
  • Also [Putting Two Consecutive Lines into One Line with Perl / AWK](https://stackoverflow.com/q/10220348/5291015) – Inian Jul 14 '20 at 12:56
  • Another possibility is to use `awk` or similar instead of your previous `grep` step, for that we'd need original sample and explanation of the grep command you used to get current output – Sundeep Jul 14 '20 at 13:18
  • I agree that `Putting 2 lines..` or `joining pair of lines ` can solve my problem but I am interested in know who can I selectively remove any end line character to merge the next line with it. (Based on pattern matching) – impossible Jul 14 '20 at 13:25
  • *knowing how [ sorry for the typo ] – impossible Jul 14 '20 at 14:02
  • And realize that `awk` allows you to `grep` AND logicially test lines and manipulate them all in one language and process. `awk '/MatchThis/{if (test > that) {print "the other $NF"} else {print "not matched"}}' file` is the general idea. See [Awk Tutorial](http://grymoire.com/Unix/Awk.html) . Good luck. – shellter Jul 14 '20 at 16:07

1 Answers1

2

Any of these might help you out:

$ awk '!(FNR%2){ print b $0 }{b=$0}' file
$ paste -sd "\0\n" file

Both of these lines assume that the odd lines need to be concatenated with the even lines

note: according to POSIX paste "\0" is considered an empty string, not the <null>-character

kvantour
  • 25,269
  • 4
  • 47
  • 72