0

Im trying to replace this text with this command but it doesn't work:

sed -e 's/\nnumber2/number2/g'  fffff > fffff

This is what my file contains:

number1;gAMMA
number2;gat
number1;zilla
number2;dog

This is my expected output:

number1;gAMMAnumber2;gat
number1;zillanumber2;dog
codeforester
  • 39,467
  • 16
  • 112
  • 140

3 Answers3

1

awk to the rescue!

$ awk '/^number2/ {n=1} 
       {printf "%s"(n?"\n":""), $0; n=0}' file > tmp && mv tmp file

or perhaps set ORS and use print

karakfa
  • 66,216
  • 7
  • 41
  • 56
1
sed ':a;N;$!ba;s/\nnumber2/number2/g' fffff > fffff

here is the explanation

Community
  • 1
  • 1
cur4so
  • 1,750
  • 4
  • 20
  • 28
1

As mentioned in the comments already, Perl would probably be better suited for this multiline task:

perl -i.bak -pe 's/(number1[^\n]*)\n/\1/' yourfile.txt

There is probably a more elegant way to do this with Perl, but this is fairly straight forward. It will also create a backup file with .bak extension, if you don't want that, omit the bak part.

taskalman
  • 133
  • 2
  • 9