0

I'm looking for a way to remove carriage returns on the line prior to the sed match. For example, I'd like to remove the carriage return prior to any instance of "[".

Sample input:

MESSAGE: Location
latitude
[-Pi/4,Pi/4]
longitude
[-Pi,Pi]
altitude
[10000,120000]

Sample output:

MESSAGE: Location
latitude [-Pi/4,Pi/4]
longitude [-Pi,Pi]
altitude [10000,120000]

Any suggestion using sed, tr or awk would be appreciated. Thanks.

Karimi
  • 1,387
  • 1
  • 13
  • 19

5 Answers5

1

A quick search would have yielded the result to this post https://stackoverflow.com/a/1252191/722238.

Using the solution from that post with your problem, here is the answer:

sed ':a;N;$!ba;s/\n\[/ [/g' yourinput.txt

Community
  • 1
  • 1
fbynite
  • 2,651
  • 1
  • 21
  • 25
1

You can use Perl:

use warnings;
use strict; 

my $p; 

while (<>) {
  if (/^\[/) {
    chomp $p; 
    print "$p $_";
    undef $p;
  } else {
    print $p if defined $p; 
    $p = $_; 
  }
}

Or from the command line:

perl -lane ' if (/\[/) { print "$p $_"; undef $p} else { print $p if defined $p; $p = $_; }' input
perreal
  • 94,503
  • 21
  • 155
  • 181
  • Since I happened to be writing a perl script, this solution was most applicable to my needs. Thank you. – Karimi Dec 24 '12 at 03:15
0

This might work for you (GNU sed):

sed '$!N;s/\n\[/ [/;P;D' file
potong
  • 55,640
  • 6
  • 51
  • 83
0

Using awk:

awk 'NR == 1 {previous = $0}
     NR > 1 && $0 ~  "^[[]" {previous = previous $0}
     NR > 1 && $0 !~ "^[[]" {print previous; previous = $0}
     END{print previous}' your_file.txt
jfg956
  • 16,077
  • 4
  • 26
  • 34
0
#!/usr/bin/awk -f
BEGIN {
  RS = ""
}
{
  gsub(/\n\[/, " [")
}
1
Zombo
  • 1
  • 62
  • 391
  • 407