join two lines

I want to join this two lines but only when after him
I have nothing or a comma

Yes, I know Jonesy, and I'll give him
about one more minute.

this two lines must become
Yes, I know Jonesy, and I'll give him about one more minute.

thank you very much

cat infilke | xargs -d '\n'
1 Like
sed '/[^[:punct:]],*$/{N;s/\n/ /;}' infile
awk '/[^[:punct:]],*$/{getline $(NF+1)}1' infile
1 Like

the input is like this

This is my cat
my cat's name is betty

This is my dog
my dog's name is frank

This is my fish
my fish's name is george

This is my goat
my goat's name is adam

when I use xargs then the output is
my goat's name is adamge

when I use sed or awk then the output is
my cat's name is betty
This is my dog
y dog's name is frank
my fish's name is george
This is my goat
y goat's name is adam

but the output must be this
--------------------------------------
This is my cat my cat's name is betty

This is my dog my dog's name is frank

This is my fish my fish's name is george

This is my goat my goat's name is adam

thank you

I presume you mean the other way around.. See if this works:

awk '/[^[:punct:]],*$/{getline $(NF+1)}NF' infile

or perhaps even this (if every sentence is a paragraph that ends with an empty line)..

awk '$1=$1' RS= infile
1 Like

try this

xargs -L 2 < filename.txt
2 Likes
perl -ne 'if (/^\s+/){print "\n\n"}else{chomp;print "$_ "}' inputfile
1 Like

The problem is that I louse always the first line and that can't.
I look again to the internet for to find a solution.
But thanks anyway.

What do you mean, you always lose the first line? Can you give an input and output sample?

I try now over thirthy solutions that I find on the internet and nothing do what I want, so I believe I do something very wrong and I don't see it.
This is the input

This is my cat,
her name is betty

this must be the output
This is my cat, her name is betty

When I use awk '/[^[:punct:]],*$/{getline $(NF+1)}1' input
than the output is this

her name is betty

when I use awk '/[^[:punct:]],*$/{getline $(NF+1)}1' input >output
than the output is this

This is my cat,
her name is betty

Here is only a space in the beginning of the second line.
I don't know it anymore.
But thanks anyway

Can it be that your file is in dos format? Try converting it to unix format first:

tr -d '\r' < oldfile > newfile
1 Like

thank you very much Scrutinizer

I don't understand why my other scripts works without converthing but I learn again.