LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-24-2013, 12:40 PM   #1
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Rep: Reputation: Disabled
Separating 1 column into multiple columns


Hi,

I currently have a file with a column that looks like the following:

1:12:0:1:1
2:0:11:2:2
22:1:0:0:0
.
.
.
etc.

I need this to be separated into 5 columns with the ":" marking the separation points.

Any suggestions would be greatly appreciated,

-m

Last edited by mphillips67; 07-24-2013 at 12:42 PM.
 
Old 07-24-2013, 12:54 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
The "tr" program will zap this for you.
Code:
tr : ' ' < myfilename
 
1 members found this post helpful.
Old 07-24-2013, 01:05 PM   #3
mphillips67
LQ Newbie
 
Registered: Jul 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
That did the trick, thanks.
 
Old 07-25-2013, 09:24 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
You might also consider giving us bit of wider context for the request. There may be other, better techniques available depending on exactly what you intend to do with the data.

For example, if you are going to process each line in a shell loop, then you could break up each line into separate variables (or an array) with read.

Code:
while IFS=':' read -r f1 f2 f3 f4 f5 ; do
    echo "field one is: $f1"
    echo "field two is: $f2"
    #...etc...
done <infile.txt
How can I read a file (data stream, variable) line-by-line (and/or field-by-field)?
http://mywiki.wooledge.org/BashFAQ/001

Similarly, complex work on field-based data is just what awk was designed to do. You may be able to do everything you want in it alone.

http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html

Of course, if you really did just want to replace colons with spaces, then carry on.

PS: Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.
 
  


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
[SOLVED] lookup from one column in three columns by awk phpshell Programming 8 04-13-2013 01:38 PM
awk split single column into multiple columns based on RS wolverene13 Programming 11 11-01-2012 06:07 PM
how to merge multiple columns into one column linuxon Linux - Newbie 6 03-14-2012 12:17 PM
sort multiple columns + replace another column cedance Linux - Newbie 1 03-29-2011 06:59 AM
How to get parts of one column and prints all columns ? nasra2002 Linux - Software 2 12-13-2010 10:36 PM


All times are GMT -5. The time now is 01:58 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration