LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2020, 09:57 AM   #1
cogiz
LQ Newbie
 
Registered: Oct 2018
Location: Halifax
Distribution: Peppermint 10
Posts: 15

Rep: Reputation: Disabled
converting multiple lines of text to single line of text with comma separting them


I have a file called abc.txt

00:00:00:00
00:02:59:90
00:09:08:50

I would like it to read as follows:

00:00:00:00,00:02:59:90,00:09:08:50

I have tried
Code:
tr "n" " " < abc.txt > xyz.txt
but to no avail.
Any help or assistance is greatly appreciated.
Thank you.

Cogiz
 
Old 10-06-2020, 10:23 AM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Code:
tr "n" " " < abc.txt > xyz.txt
Code:
tr "\n" "," < abc.txt > xyz.txt
This will leave a trailing ,
 
Old 10-06-2020, 10:38 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
00:00:00:00
00:02:59:90
00:09:08:50
... this paste ...
Code:
paste -sd, <$InFile >$OutFile
... produced this OutFile ...
Code:
00:00:00:00,00:02:59:90,00:09:08:50
No trailing comma.

Daniel B. Martin

.
 
1 members found this post helpful.
Old 10-06-2020, 12:09 PM   #4
cogiz
LQ Newbie
 
Registered: Oct 2018
Location: Halifax
Distribution: Peppermint 10
Posts: 15

Original Poster
Rep: Reputation: Disabled
converting text file from column to single line with comma separator

perhaps I should give you more information regarding my problem.

I have a file named chapters.txt (see below):

Chapter 1 = 00:00:00:00
Chapter 2 = 00:02:59:90
Chapter 3 = 00:09:08:50
Chapter 4 = 00:16:40:23
Chapter 5 = 00:24:22:66
Chapter 6 = 00:26:53:56
Chapter 7 = 00:34:57:30
Chapter 8 = 00:39:11:40
Chapter 9 = 00:43:44:86
Chapter 10 = 00:50:24:33
Chapter 11 = 00:53:23:66
Chapter 12 = 00:58:28:23
Chapter 13 = 01:04:48:50
Chapter 14 = 01:15:07:30
Chapter 15 = 01:24:48:23
Chapter 16 = 01:26:59:73
Chapter 17 = 01:28:53:73
Chapter 18 = 01:32:56:40
Chapter 19 = 01:37:13:90
Chapter 20 = 01:43:03:36

so far I have done this
Code:
cat chapters.txt | cut -c13- | sed -e 's/^[ \t]*//' > newchapters.txt
which produces this:

00:00:00:00
00:02:59:90
00:09:08:50
00:16:40:23
00:24:22:66
00:26:53:56
00:34:57:30
00:39:11:40
00:50:24:33
00:53:23:66
00:58:28:23
01:04:48:50
01:15:07:30
01:24:48:23
01:26:59:73
01:28:53:73
01:32:56:40
01:37:13:90
01:43:03:36

what I need to do now is instead of printing each one on a new line is to have it print as a single line with comma separating between chapters as follows:

00:00:00:00,00:02:59:90,00:09:08:50,00:16:40:23,00:24:22:66,00:26:53:56,00:34:57:30,00:39:11:40,00:3 9:11:40,00:50:24:33,00:53:23:66,
00:58:28:23,01:04:48:50,01:15:07:30,01:24:48:23,01:26:59:73,01:28:53:73,01:32:56:40,01:37:13:90,01:4 3:03:36

I have tried
Code:
paste -sd, <$infile >$outfile
also
Code:
tr "\n" "," < infile > outfile
to no avail.

any further assistance will be greatly appreciated.

thank you very much for all your replies.

Cogiz
 
Old 10-06-2020, 12:39 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
Chapter 1 = 00:00:00:00
Chapter 2 = 00:02:59:90
Chapter 3 = 00:09:08:50
Chapter 4 = 00:16:40:23
Chapter 5 = 00:24:22:66
Chapter 6 = 00:26:53:56
Chapter 7 = 00:34:57:30
Chapter 8 = 00:39:11:40
Chapter 9 = 00:43:44:86
Chapter 10 = 00:50:24:33
Chapter 11 = 00:53:23:66
Chapter 12 = 00:58:28:23
Chapter 13 = 01:04:48:50
Chapter 14 = 01:15:07:30
Chapter 15 = 01:24:48:23
Chapter 16 = 01:26:59:73
Chapter 17 = 01:28:53:73
Chapter 18 = 01:32:56:40
Chapter 19 = 01:37:13:90
Chapter 20 = 01:43:03:36
... this code ...
Code:
 cut -c13- <$InFile    \
|sed -e 's/^[ \t]*//'  \
|paste -sd,            \
>$OutFile
... produced this OutFile ...
Code:
00:00:00:00,00:02:59:90,00:09:08:50,00:16:40:23,00:24:22:66,00:26:53:56,00:34:57:30,00:39:11:40,00:43:44:86,00:50:24:33,00:53:23:66,00:58:28:23,01:04:48:50,01:15:07:30,01:24:48:23,01:26:59:73,01:28:53:73,01:32:56:40,01:37:13:90,01:43:03:36

Daniel B. Martin

.
 
1 members found this post helpful.
Old 10-06-2020, 12:56 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Code:
awk '{ ORS=","; printf $NF } '
there are still ways to improve
 
1 members found this post helpful.
Old 10-06-2020, 01:06 PM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
This produces a trailing comma, but is only a single command:
Code:
awk '{printf $4 ","}' chapters.txt
If the comma is a problem, there's a couple of ways to deal with it:
Code:
awk '{printf $4 ","}' chapters.txt | head -c-1
awk '{printf "," $4}' chapters.txt | tail -c+2
(As noted later in the thread, using printf like this will cause errors/incorrect results if the input contains percent signs. That's not the case for the given input, but if it were, using printf "%s," , $4 would solve it.)


Although if we can't avoid multiple commands, we can use awk to extract the last field in the row ($NF) then paste to replace newlines with comma:
Code:
awk '{print $NF}' chapters.txt  | paste -sd,

Last edited by boughtonp; 10-07-2020 at 05:16 PM.
 
1 members found this post helpful.
Old 10-06-2020, 07:10 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Perhaps I will be accused of torturing sed.

With this InFile ...
Code:
Chapter 1 = 00:00:00:00
Chapter 2 = 00:02:59:90
Chapter 3 = 00:09:08:50
Chapter 4 = 00:16:40:23
Chapter 5 = 00:24:22:66
Chapter 6 = 00:26:53:56
Chapter 7 = 00:34:57:30
Chapter 8 = 00:39:11:40
Chapter 9 = 00:43:44:86
Chapter 10 = 00:50:24:33
Chapter 11 = 00:53:23:66
Chapter 12 = 00:58:28:23
Chapter 13 = 01:04:48:50
Chapter 14 = 01:15:07:30
Chapter 15 = 01:24:48:23
Chapter 16 = 01:26:59:73
Chapter 17 = 01:28:53:73
Chapter 18 = 01:32:56:40
Chapter 19 = 01:37:13:90
Chapter 20 = 01:43:03:36
... this sed ...
Code:
sed -e :a -e '$!N;s/\n/,/;ta s/\(Chapter [0-9]* = \)//'g $InFile >$OutFile
... produced this OutFile ...
Code:
00:00:00:00,00:02:59:90,00:09:08:50,00:16:40:23,00:24:22:66,00:26:53:56,00:34:57:30,00:39:11:40,00:43:44:86,00:50:24:33,00:53:23:66,00:58:28:23,01:04:48:50,01:15:07:30,01:24:48:23,01:26:59:73,01:28:53:73,01:32:56:40,01:37:13:90,01:43:03:36

Daniel B. Martin

.
 
1 members found this post helpful.
Old 10-06-2020, 11:06 PM   #9
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Here is a simple awk which does not add the trailing comma and also ignores empty lines or anything that does not begin with "Chap":

Code:
awk '/^Chap/{if(n++){printf ","} printf "%s", $4}' chapters.txt
And another sed|tr|sed pipeline to do the same thing:

Code:
sed -r 's/^ch.*= ([0-9:]+)/\1,/i' chapters.txt | tr -d '\n' | sed -r 's/,$//'
UPDATE: Added correct printf specifier per post #10

Last edited by astrogeek; 10-07-2020 at 12:20 PM. Reason: Corrected ptintf w/specifier
 
1 members found this post helpful.
Old 10-07-2020, 01:22 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Always printf unkown contents with a format string like "%s,"

With a "delayed" separator:
Code:
awk 'NF>=4 {printf (sep "%s"), $4; sep=","}' chapters.txt
The separator sep is known.
 
Old 10-07-2020, 02:19 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I think post #6 solves the separator issue "automatically".
 
Old 10-07-2020, 06:29 AM   #12
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by astrogeek View Post
Here is a simple awk which does not add the trailing comma and also ignores empty lines or anything that does not begin with "Chap":

Code:
awk '/^Chap/{if(n++){printf ","} printf $4}' chapters.txt
And another sed|tr|sed pipeline to do the same thing:

Code:
sed -r 's/^ch.*= ([0-9:]+)/\1,/i' chapters.txt | tr -d '\n' | sed -r 's/,$//'
Is there something missing from these solutions?

This one ...
Code:
sed -e :a -e '$!N;s/\n/,/;ta s/\(Chapter [0-9]* = \)//g' $InFile >$OutFile
cat $OutFile; echo "EOF"
... produced this ...
Code:
00:00:00:00,00:02:59:90,00:09:08:50,00:16:40:23,00:24:22:66,00:26:53:56,00:34:57:30,00:39:11:40,00:43:44:86,00:50:24:33,00:53:23:66,00:58:28:23,01:04:48:50,01:15:07:30,01:24:48:23,01:26:59:73,01:28:53:73,01:32:56:40,01:37:13:90,01:43:03:36
EOF
... but this one ...
Code:
awk '/^Chap/{if(n++){printf ","} printf $4}' $InFile >$OutFile
cat $OutFile; echo "EOF"
... produced this ...
Code:
00:00:00:00,00:02:59:90,00:09:08:50,00:16:40:23,00:24:22:66,00:26:53:56,00:34:57:30,00:39:11:40,00:43:44:86,00:50:24:33,00:53:23:66,00:58:28:23,01:04:48:50,01:15:07:30,01:24:48:23,01:26:59:73,01:28:53:73,01:32:56:40,01:37:13:90,01:43:03:36EOF
... and this one ...
Code:
sed -r 's/^ch.*= ([0-9:]+)/\1,/i' $InFile | tr -d '\n' | sed -r 's/,$//' >$OutFile
cat $OutFile; echo "EOF"
... did the same thing.


Daniel B. Martin

.
 
Old 10-07-2020, 08:01 AM   #13
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by pan64 View Post
I think post #6 solves the separator issue "automatically".
What version did you test it with? Here's the output from GNU Awk versions 4.1.3 and 4.2.1:

Code:
$ awk '{ ORS=","; printf $NF } ' chapters.txt
00:00:00:0000:02:59:9000:09:08:5000:16:40:2300:24:22:6600:26:53:5600:34:57:3000:39:11:4000:43:44:8600:50:24:3300:53:23:6600:58:28:2301:04:48:5001:15:07:3001:24:48:2301:26:59:7301:28:53:7301:32:56:4001:37:13:9001:43:03:36
You did avoid the trailing comma, but that's because printf doesn't output field or record separators, and thus there are no commas at all.

 
Old 10-07-2020, 08:09 AM   #14
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by MadeInGermany View Post
Always printf unkown contents with a format string like "%s,"
That's good advice for arbitrary input, but not necessary if it's known not to contain percent signs?

 
1 members found this post helpful.
Old 10-07-2020, 08:14 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by boughtonp View Post
What version did you test it with? Here's the output from GNU Awk versions 4.1.3 and 4.2.1:

Code:
$ awk '{ ORS=","; printf $NF } ' chapters.txt
00:00:00:0000:02:59:9000:09:08:5000:16:40:2300:24:22:6600:26:53:5600:34:57:3000:39:11:4000:43:44:8600:50:24:3300:53:23:6600:58:28:2301:04:48:5001:15:07:3001:24:48:2301:26:59:7301:28:53:7301:32:56:4001:37:13:9001:43:03:36
You did avoid the trailing comma, but that's because printf doesn't output field or record separators, and thus there are no commas at all.

obviously that was wrong (solution was incomplete), works without f:

Code:
$ awk 'BEGIN { ORS="," } { print $NF } ' chapters.txt
 
1 members found this post helpful.
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
remove a line from a comma delimited file that contains a single digit in position 2 j-me Linux - General 6 05-30-2013 08:25 AM
[SOLVED] bash/sed/awk to convert comma's not in quotes in a line with many comma's oly_r Programming 23 01-25-2012 08:53 AM
[SOLVED] How to remove line breaks from lines ending with a comma jasonws Linux - General 1 11-10-2010 12:03 PM
Add comma to end of lines in text file Johng Programming 9 08-21-2010 04:15 AM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:29 AM.

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