Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-07-2010, 12:55 PM   #1
Registered: Feb 2009
Posts: 68

Rep: Reputation: 10
Replace Characters in txt file

I have txt file with list of ID's and I need to insert comma in every line and then remove new line character so it'll become one long string.
So to clarify, I have txt file content that looks like this.
... and so on.

but I would like this to change to 234,5466,2356,...
I looked at sed and tried to wrap my head around the commands but I guess my brain isn't smart enough. its really confusing for me.

I've managed to add commas to end of line (sed "s/$/,/g" filename) but somehow I can't seem to remove new line character from each line.

sed 's/[ \n]*$//' doesn't seem to work. can anyone tell me what I'm doing wrong?

Last edited by struct; 11-07-2010 at 01:00 PM.
Old 11-07-2010, 12:59 PM   #2
Registered: Feb 2009
Posts: 68

Original Poster
Rep: Reputation: 10
I found a post that does this with perl.
perl -e 'while (<>) { if (! /\|$/ ) { chomp; } print ;}'

this worked for me. but I would like to use sed to achieve this. so I'm still interested learning about sed if anyone is willing to point out my error.

Thank you.
Old 11-07-2010, 01:19 PM   #3
Senior Member
Registered: Jan 2010
Posts: 1,668

Rep: Reputation: 494Reputation: 494Reputation: 494Reputation: 494Reputation: 494
Try this:

sed -n -i.bak 'H; $ {x;s/\n/,/g;p}' file
Old 11-07-2010, 01:46 PM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
awk 'NR==1{s=$0;next}{s=s","$0}END{print s}' file
Old 11-07-2010, 02:28 PM   #5
Registered: Feb 2009
Posts: 68

Original Poster
Rep: Reputation: 10
Thank you to both crts and colucix. both solution worked really well and very elegant one liners. I've taken crts's method since I want to stick with one command (sed) and I like the way it creates a back up of original file before modifying it.
Thanks to both of you. you guys rock.

Last edited by struct; 11-07-2010 at 02:30 PM.


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
Help Strip / Remove attachments (Replace with TXT file if Possible) Vince0000 Linux - General 2 09-17-2010 07:46 PM
html2text >> a.txt creates the file but has extra characters in it Kakarot_Rathish Linux - General 4 03-08-2010 06:01 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM
How do i change spaces inside a txt file, and replace space with backslash \? repolona Linux - Newbie 1 05-24-2006 06:08 AM
strange characters when routing man page to txt file DJOtaku Linux - General 3 05-15-2005 02:03 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:41 PM.

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