UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old January 14th, 2005, 05:56 AM
BlueCrush BlueCrush is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 3 BlueCrush User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 31 sec
Reputation Power: 0
Replace New Lines in Unix (Beginner)

Hi, I was wondering if anyone could help me with this problem. I have a file that looks like this, for example:

John
Smith
Ann
Smith

I want to get the text to be in 2 columns, :

John Smith
Ann Smith

Would I be best using SED to replace the new line with a space? (How would I do this?) Or would it be better to use pr?
Sorry for the mundane-ness of this Q but I've just starting on Unix!
Many Thanks,
Jessica

Reply With Quote
  #2  
Old January 14th, 2005, 07:43 AM
zby zby is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Location: Prague, Czech Republic
Posts: 825 zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level)zby User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 6 Days 16 h 4 m 55 sec
Reputation Power: 65
I am primarily a perl programmer thus I use perl:
Code:
[zby@montana SCANDIR]$ cat ~/tstfile
John
Smith
Ann
Smith
William J.
Goat
O.J.
Simple
[zby@montana SCANDIR]$
[zby@montana SCANDIR]$ cat ~/tstfile | perl -e '{while(<STDIN>) { chomp; $jm.=" $_"; if(($.%2)==0) { $jm=~s/\s+//; print "$jm\n"; $jm=""; } } }'
John Smith
Ann Smith
William J. Goat
O.J. Simple
[zby@montana SCANDIR]$

Reply With Quote
  #3  
Old January 16th, 2005, 05:54 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
Quote:
Originally Posted by zby
I am primarily a perl programmer thus I use perl:
Code:
[zby@montana SCANDIR]$ cat ~/tstfile
John
Smith
Ann
Smith
William J.
Goat
O.J.
Simple
[zby@montana SCANDIR]$
[zby@montana SCANDIR]$ cat ~/tstfile | perl -e '{while(<STDIN>) { chomp; $jm.=" $_"; if(($.%2)==0) { $jm=~s/\s+//; print "$jm\n"; $jm=""; } } }'
John Smith
Ann Smith
William J. Goat
O.J. Simple
[zby@montana SCANDIR]$



ignore that stupid 'cat'
Quote:
cat ~/tstfile | perl -e '{while(<STDIN>) { chomp; $jm.=" $_"; if(($.%2)==0) { $jm=~s/\s+//; print "$jm\n"; $jm=""; } } }'


perl -e '{while(<STDIN>) { chomp; $jm.=" $_"; if(($.%2)==0) { $jm=~s/\s+//; print "$jm\n"; $jm=""; } } }' ~/tstfile
should also work (not tested)

nota: this does the same

sed '{N;s/\n//;}' filename

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Replace New Lines in Unix (Beginner)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway