LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-08-2021, 06:06 PM   #1
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
vi - joining consecutive lines


I map a key in vi to help me join two lines together and then move to the next line. Rinse and repeat. This is part of the process to help me shuffle some #EXTM3U style playlists for my portable music player.

I map the key [ to do the following:-
1. Go to the end of the line ($)
2. Join two lines together (J)
3. Down arrow key to go to next line.

Like this in vi:-

:map [ $J(CTRLV)(DOWN ARROW KEY)(PRESS RETURN KEY)

This works well except for the fact that some playlists may have 6000 lines in them. In which case, I sit there for a minute or so holding my finger down on the [ key.

My question is this. Is there a simple command in vi that I can use to achieve the same result? Or is there a more elegant way to do this, perhaps with sed or awk etc?

Thanks for your help.
 
Old 12-08-2021, 07:22 PM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
Quick and dirty.
Code:
paste <(awk 'NR%2==1' input.txt) <(awk 'NR%2==0' input.txt)

Last edited by allend; 12-08-2021 at 07:29 PM.
 
1 members found this post helpful.
Old 12-08-2021, 11:25 PM   #3
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Original Poster
Rep: Reputation: 70
allend, Thanks for that. This works well if I first convert the playlist to unix format from dos format using dos2unix. I added --delimiters="" to the paste command so that the output did not have a TAB at the joined point.
 
1 members found this post helpful.
Old 12-09-2021, 12:22 AM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
Thanks for reporting back on your success. I did think of the delimiter after posting, but I did not consider the DOS style line endings.
 
Old 12-09-2021, 02:36 AM   #5
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
What is the point of using awk here?
Code:
paste -d '' - - <input.txt
will do the same.
 
2 members found this post helpful.
Old 12-09-2021, 04:17 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,142

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
cute - I woulda used sed. Silly me.
 
Old 12-09-2021, 04:28 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
Quote:
What is the point of using awk here?
Code:
paste -d '' - - <input.txt
Obviously none, when you put it that way!
 
1 members found this post helpful.
Old 12-09-2021, 06:05 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
cute - I woulda used sed. Silly me.
Not silly at all. If I were to edit the file in place, I'd use sed as well
Code:
sed -i 'N;s/\n//' input.txt
@OP. From inside vi, the command would be
Code:
:g/^/j
or
Code:
:g/^/j!
if you want to join lines without spaces.

Last edited by shruggy; 12-09-2021 at 06:19 AM.
 
2 members found this post helpful.
Old 12-09-2021, 09:36 AM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,377

Rep: Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757Reputation: 2757
@shruggy - Sheeite! Smash it out of the park with 'paste', then smash it out of the park with 'sed' then smash it out of the park with 'vi'.
Way-cool.
 
1 members found this post helpful.
Old 12-09-2021, 04:55 PM   #10
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Original Poster
Rep: Reputation: 70
Thank you to everyone who replied. I will now sit down and write a script that does everything I need to do to automatically massage these playlists so I don't have to do it all manually.
 
  


Reply

Tags
vi



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Joining Three Lines of Text into One Line with Delimiters Between the Original Lines tronayne Slackware 25 03-18-2016 09:46 AM
[SOLVED] script to read and compare two consecutive lines in a file smritisingh03 Linux - Newbie 16 08-28-2012 04:36 AM
[SOLVED] Sed -> Search on 2 consecutive lines and then replace lebrocoli Programming 3 04-27-2012 02:20 PM
[SOLVED] Shell script to convert values on successive lines into consecutive ranges? kmkocot Programming 5 07-09-2010 10:59 AM
sed/awk: Three consecutive blank lines in a file, how to delete two of them? recomboDNA Programming 8 06-17-2010 09:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration