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.
|
|
08-21-2010, 03:22 AM
|
#1
|
Member
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 411
Rep:
|
Add comma to end of lines in text file
I have a plain text file with 360 lines of varying length text. How do I add a comma or other symbol to the end of each line so that I can convert the file to csv format that I can open in a spreadsheet (45 rows, 8 columns). That means each 8 lines of text forms 8 columns, with 45 rows.
|
|
|
08-21-2010, 03:45 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
Adding a comma to the end of a line (all lines in a file):
sed -i.bak 's/$/,/' infile
This will make the changes in place, the original file will be saved with a .bak extension.
Hope this helps.
|
|
|
08-21-2010, 03:57 AM
|
#3
|
Member
Registered: Jul 2004
Location: Chennai, India
Posts: 952
|
Quote:
That means each 8 lines of text forms 8 columns, with 45 rows.
|
This actually makes me think that the OP has file where each row contains:
Row1Line1 Row1Line2 Row1Line3 Row1Line4 Row1Line5 Row1Line6 Row1Line7 Row1Line8
Row2Line1 Row2Line2 Row2Line3 Row2Line4 Row2Line5 Row2Line6 Row2Line7 Row2Line8
and so on.
If the OP clarifies what separates a "line" which is actually a column, then we can help further. For instance, if the separator is a blank space, then we would use sed to substitute blanks with commas.
OK
|
|
|
08-21-2010, 04:08 AM
|
#4
|
Member
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 411
Original Poster
Rep:
|
Thank you druuna and AnanthaP.
Adding the comma works, which is step one. However the spreadsheet still sees the modified file as only one column, which is what AnanthaP has indicated. So it appears that I now need to add lines 2,3,4,5,6,7,8 to the end of line 1, lines 10,11,12,13,14,15,16 to the end of line 9 etc, ending up with a file with only 45 lines?
|
|
|
08-21-2010, 04:16 AM
|
#5
|
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126
Rep:
|
What is your field separator? A comma? Are commas found as field separator and not in fields?
If the answer is yes to all of the above questions, then this should work:
cat textfile.txt | sed 's/$/,/' | tr -d '\n' | sed 's/\(\([^,]*,\)\{8,8\}\)/\1\n/g' | sed -e 's/^ //' -e 's/\([ ]*,[ ]*\)/,/g' -e 's/,$//g'
(first sed command adds comma at the end of each line, then tr command merges all lines, the second sed command breaks up the lines after each 8th commas, and the last sed command just doeas some "cleanup": deletes spaces from the beginning and end of rows and fields, and deletes comma from the end of each row)
Last edited by J_Szucs; 08-21-2010 at 04:19 AM.
|
|
|
08-21-2010, 04:28 AM
|
#6
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
in bash
Code:
#!/bin/bash
I=0
while read LINE;do
(( ++I, I %= 8 ))
if [[ I -eq 0 ]]; then
echo "$LINE"
else
echo -n "$LINE,"
fi
done
bash script.sh < input_file
Last edited by konsolebox; 08-21-2010 at 04:30 AM.
|
|
|
08-21-2010, 04:31 AM
|
#7
|
Member
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 411
Original Poster
Rep:
|
Thank you J_Szucs, that did the job.
I was confused for a while - it wrote the modified output to the terminal, not modifying the original file.
|
|
|
08-21-2010, 04:32 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
OP - generally, the way to stop all of us guessing and coming up with ways that don't actually help / work with your situation is to provide us
with a sample of the text file. Then we can actually see what unusual type characters there might be that need to be addressed in solving your problem.
|
|
|
08-21-2010, 04:40 AM
|
#9
|
Senior Member
Registered: Nov 2001
Location: Budapest, Hungary
Distribution: SuSE 6.4-11.3, Dsl linux, FreeBSD 4.3-6.2, Mandrake 8.2, Redhat, UHU, Debian Etch
Posts: 1,126
Rep:
|
Sry, I do not really like overwriting the input file when testing a new function.
When it has proven to work, just add to the end of the command:
> textfile.txt.new; mv textfile.txt.new textfile.txt
|
|
|
08-21-2010, 05:15 AM
|
#10
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028
|
Assuming I am following:
Code:
awk '{line=line $0;f=1}!(NR % 8){print line;line="";f=0}f{line=line","}' oldfile > newfile
|
|
|
All times are GMT -5. The time now is 04:22 PM.
|
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
|
|