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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
10-06-2020, 09:57 AM
#1
LQ Newbie
Registered: Oct 2018
Location: Halifax
Distribution: Peppermint 10
Posts: 15
Rep:
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
10-06-2020, 10:23 AM
#2
Member
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634
Code:
tr "n" " " < abc.txt > xyz.txt
Code:
tr "\ n" ", " < abc.txt > xyz.txt
This will leave a trailing ,
10-06-2020, 10:38 AM
#3
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881
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.
10-06-2020, 12:09 PM
#4
LQ Newbie
Registered: Oct 2018
Location: Halifax
Distribution: Peppermint 10
Posts: 15
Original Poster
Rep:
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
10-06-2020, 12:39 PM
#5
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881
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.
10-06-2020, 12:56 PM
#6
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842
Code:
awk '{ ORS=","; printf $NF } '
there are still ways to improve
1 members found this post helpful.
10-06-2020, 01:06 PM
#7
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599
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.
10-06-2020, 07:10 PM
#8
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881
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.
10-06-2020, 11:06 PM
#9
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
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.
10-07-2020, 01:22 AM
#10
Senior Member
Registered: Dec 2011
Location: Simplicity
Posts: 2,791
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.
10-07-2020, 02:19 AM
#11
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842
I think post #6 solves the separator issue "automatically".
10-07-2020, 06:29 AM
#12
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881
Quote:
Originally Posted by
astrogeek
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
.
10-07-2020, 08:01 AM
#13
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599
Quote:
Originally Posted by
pan64
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.
10-07-2020, 08:09 AM
#14
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,599
Quote:
Originally Posted by
MadeInGermany
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.
10-07-2020, 08:14 AM
#15
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842
Quote:
Originally Posted by
boughtonp
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.
All times are GMT -5. The time now is 10:29 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News