LinuxQuestions.org
Help answer threads with 0 replies.
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, 11:40 AM   #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 11:42 AM.
 
Old 07-24-2013, 11:54 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
The "tr" program will zap this for you.
Code:
tr : ' ' < myfilename
 
1 members found this post helpful.
Old 07-24-2013, 12: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, 08:24 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 12:38 PM
awk split single column into multiple columns based on RS wolverene13 Programming 11 11-01-2012 05:07 PM
how to merge multiple columns into one column linuxon Linux - Newbie 6 03-14-2012 11:17 AM
sort multiple columns + replace another column cedance Linux - Newbie 1 03-29-2011 05:59 AM
How to get parts of one column and prints all columns ? nasra2002 Linux - Software 2 12-13-2010 09:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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