[SOLVED] Merge some lines

Need help with C, C++, perl, python, etc?

[SOLVED] Merge some lines

Postby gaijin » 2016-11-22 12:25

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.
gaijin
 
Posts: 12
Joined: 2016-11-22 12:08

Re: Merge some lines

Postby emrah » 2016-11-22 14:45

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"
emrah
 
Posts: 5
Joined: 2016-11-08 08:41

Re: Merge some lines

Postby dasein » 2016-11-22 15:51

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

Re: Merge some lines

Postby kiyop » 2016-11-23 03:39

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/
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

Postby gaijin » 2016-11-24 08:33

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...
gaijin
 
Posts: 12
Joined: 2016-11-22 12:08

Re: Merge some lines

Postby pylkko » 2016-11-24 11:09

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: 1213
Joined: 2014-11-06 19:02

Re: Merge some lines

Postby pylkko » 2016-11-24 11:09

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
pylkko
 
Posts: 1213
Joined: 2014-11-06 19:02

Re: Merge some lines

Postby kiyop » 2016-11-24 15:16

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/
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

Postby gaijin » 2016-11-29 13:32

Thank you for help
gaijin
 
Posts: 12
Joined: 2016-11-22 12:08


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests

fashionable