LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-21-2010, 03:22 AM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 411

Rep: Reputation: 31
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.
 
Old 08-21-2010, 03:45 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 08-21-2010, 03:57 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
Old 08-21-2010, 04:08 AM   #4
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 411

Original Poster
Rep: Reputation: 31
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?
 
Old 08-21-2010, 04:16 AM   #5
J_Szucs
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: Reputation: 58
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.
 
Old 08-21-2010, 04:28 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 08-21-2010, 04:31 AM   #7
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 411

Original Poster
Rep: Reputation: 31
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.
 
Old 08-21-2010, 04:32 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
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.
 
Old 08-21-2010, 04:40 AM   #9
J_Szucs
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: Reputation: 58
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
 
Old 08-21-2010, 05:15 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
Assuming I am following:
Code:
awk '{line=line $0;f=1}!(NR % 8){print line;line="";f=0}f{line=line","}' oldfile > newfile
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
add text to end of line? tatoosh Linux - Newbie 8 07-31-2009 04:28 AM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 06:50 PM
Remove lines in a text file based on another text file asiandude Programming 10 01-29-2009 11:59 AM
shelll script to replace and add lines in text cmontr Programming 11 02-29-2008 08:27 AM

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

All times are GMT -5. The time now is 04:22 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