[SOLVED] Merge some lines

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
gaijin
Posts: 12
Joined: 2016-11-22 12:08

[SOLVED] Merge some lines

#1 Post by gaijin »

Hello
I have file with many lines ~8000 and I want merge each line of beginning "Rec." with previous line:

Source:

Code: Select all

one test
test two
Rec. something
two test three
yes or not
another lines
Rec. something else
and another
Destination what i Want:

Code: Select all

one test
test two Rec. something
two test three 
yes or not 
another lines Rec. something else
and another
I have:

Code: Select all

sed -e ':a N' -e 's/\n\(Rec\.\)/ \1/' text.txt
But it missing some Rec. from beginnings lines and don't merg it with previus.

What should I use for it?
Last edited by gaijin on 2016-11-29 13:32, edited 1 time in total.

emrah
Posts: 5
Joined: 2016-11-08 08:41

Re: Merge some lines

#2 Post by emrah »

as a dirty solution

Code: Select all

sed "s/$/#NEWLINE#/" text.txt | sed "s/^Rec\./#DELETE#Rec\./" | tr -d "\n" | sed "s/#NEWLINE##DELETE#/ /g" | sed "s/#NEWLINE#/\n/g"

User avatar
dasein
Posts: 7775
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: Merge some lines

#3 Post by dasein »

I smell homework.

User avatar
kiyop
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan

Re: Merge some lines

#4 Post by kiyop »

spite

Code: Select all

cat <<EOF > text.txt
#NEWLINE#
#DELETE#
EOF
sed "s/$/#NEWLINE#/" text.txt | sed "s/^Rec\./#DELETE#Rec\./" | tr -d "\n" | sed "s/#NEWLINE##DELETE#/ /g" | sed "s/#NEWLINE#/\n/g"
gives 2 empty lines :mrgreen:
I know that usually, there is not such line involving
#NEWLINE#
nor such line starting with
#DELETE#

Another candidate is:

test.sh

Code: Select all

#!/bin/bash
echo -n "input file? :"
read a
echo -n "output file? :"
read b
rm -f $b
cat $a|while read line
do if [ -n "$(echo $line|grep "^Rec. ")" ]
then line2="$line2 $line"
else echo $line2 >> $b
line2=$line
fi
done
sed -i -n 2,/end/p $b
But,

Code: Select all

cat <<EOF > test2
one test
#NEWLINE#
#NEWLINE#
test two
#DELETE#
Rec. something
two test three
yes or not
another lines
Rec. something else
Rec. another
and another
and another2
EOF
bash ./test.sh
test2
test3
more test3
gives
one test
#NEWLINE#
#NEWLINE#
test two
#DELETE# Rec. something
two test three
yes or not
another lines Rec. something else Rec. another
and another
It does not output the last line :(

Thus,

Improved test.sh

Code: Select all

#!/bin/bash
echo -n "input file? :"
read a
echo -n "output file? :"
read b
rm -f $b
echo "" >> $a
cat $a|while read line
do if [ -n "$(echo $line|grep "^Rec. ")" ]
then line2="$line2 $line"
else echo $line2 >> $b
line2=$line
fi
done
sed -i -n 2,/end/p $b
sed -i '$d' $a
;)

I want gaijin to pay me for doing his/her homework ;)

But, learning and thinking and making is interesting. Enjoy yourself by making scripts by yourself.

EDIT

The improved test.sh is not perfect. If the first line(s) start with "Rec. ", the first line(s) vanishes. How to overcome the problem? There are many solutions. Enjoy thinking.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

gaijin
Posts: 12
Joined: 2016-11-22 12:08

Re: Merge some lines

#5 Post by gaijin »

Thank you.
Rather work for work :)
I have one file with many lines bibliography. I must cut some signs from every line, grep lines with special phrases to many files, next prepare to module Jumi on Joomla site.

I must modify:

Code: Select all

cat 2-rec.sh 
#!/bin/bash

#echo -n "input file? :"
#read a
#echo -n "output file? :"
#read b

a=`echo 1.txt`
b=`echo 2.txt`
rm -f $b
echo "" >> $a
cat $a|while read line
do if [ -n "$(echo $line|grep "^Rec. ")" ]
then line2="$line2 $line"
else echo $line2 >> $b
line2=$line
fi
done
#sed -i -n 2,/end/p $b
sed -i '$d' $a 
I don't understand what is "$d" from last line.
I had to #sed -i -n 2,/end/p $b
And it's works well

I wonder why my code:

Code: Select all

sed -e ':a N' -e 's/\n\(Rec\.\)/ \1/' 1.txt
doesn't work with some lines...

User avatar
pylkko
Posts: 1801
Joined: 2014-11-06 19:02

Re: Merge some lines

#6 Post by pylkko »

gaijin wrote: Rather work for work :)
Translation: "I am more well off than students and I also actually made money from this. Smile..."

User avatar
pylkko
Posts: 1801
Joined: 2014-11-06 19:02

Re: Merge some lines

#7 Post by pylkko »

gaijin wrote: Rather work for work :)
Translation: "I am significantly more well off than students and I also actually made money from this. Smile..."

User avatar
kiyop
Posts: 3984
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan

Re: Merge some lines

#8 Post by kiyop »

gaijin wrote:

Code: Select all

a=`echo 1.txt`

Code: Select all

a=1.txt
is enough, isn't it?
gaijin wrote:I don't understand what is "$d" from last line.
Search by yourself like
https://www.google.com/search?q=sed+d+dollar
Escape is important.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

gaijin
Posts: 12
Joined: 2016-11-22 12:08

Re: Merge some lines

#9 Post by gaijin »

Thank you for help

Post Reply