Text to column for one column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Text to column for one column
# 1  
Old 10-09-2013
Text to column for one column

Looking for some help.

I have text file with data like this -
Code:
car red cobra 1.098 427
truck black f150 0 302

And I need to split apart the data to look like this.
Code:
car red cobra 1 . 0 9 8 427
truck black f150 0 0 0 0 0 302

Can I split that one column to 5 separate columns like that?

Thanks

Last edited by Scott; 10-09-2013 at 07:36 PM.. Reason: Code tags
# 2  
Old 10-09-2013
Quote:
Originally Posted by mikez104
Looking for some help.

I have text file with data like this -
car red cobra 1.098 427
truck black f150 0 302

And I need to split apart the data to look like this.

car red cobra 1 . 0 9 8 427
truck black f150 0 0 0 0 0 302

Can I split that one column to 5 separate columns like that?

Thanks
Post Clearly and also use codetag
# 3  
Old 10-09-2013
Please use code tags as required by forum rules!

That's a bit difficult as you want NO decimal dot for zero values but one otherwise. Try this as a starting poitn:
Code:
awk '{x=sprintf ("%.3f", $4); $4=""; for (i=1; i<=length(x); i++) $4=$4 substr(x, i, 1) " "}1' file
car red cobra 1 . 0 9 8  427
truck black f150 0 . 0 0 0  302

EDIT: use this to eliminate the dot for zero $4:
Code:
awk '{x=sprintf ("%.3f", $4); if (!$4) x="00000"; $4=""; for (i=1; i<=length(x); i++) $4=$4 substr(x, i, 1) " "}1' file

or even
Code:
awk '{x=$4?sprintf ("%.3f", $4):"00000"; $4=""; for (i=1; i<=length(x); i++) $4=$4 substr(x, i, 1) " "}1' file


Last edited by RudiC; 10-09-2013 at 06:04 PM.. Reason: code tag notice
# 4  
Old 10-09-2013
Another idea:

Code:
awk '{x=$4?sprintf("%.3f", $4):"00000"; gsub(/./, "& ",x); $4=x}1' file

# 5  
Old 10-09-2013
Hi,
Another way with sed:
Code:
$ sed 's/\(\([^ ]* \)\{3\}\)\([^ ]*\)/\1|0000\300|/;:ll;s/\(|\([^ ] \)*\)\([^ ]\)\([^ ]\)\([^|]\)/\1\3 \4\5/;tll;s/|.*\(.\{10\}\)00| */\1/' filo
car red cobra 1 . 0 9 8 427
truck black f150 0 0 0 0 0 302
train orange baboon 0 8 . 1 2 197

Regards.

Last edited by disedorgue; 10-09-2013 at 07:10 PM.. Reason: Fixe regular expression to work fine for 4 digit.
# 6  
Old 10-09-2013
@Chubler_XL: very good! But then get rid of x as well:
Code:
awk '{$4=$4?sprintf("%.3f", $4):"00000"; gsub(/./, "& ", $4);}1' file

This User Gave Thanks to RudiC For This Post:
# 7  
Old 10-09-2013
@disedorgue your solution will fail if value #4 has less that 4 digits of precision eg:

Code:
car red cobra 1.098 427
truck black f150 0 302
train orange baboon 8.12 197

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

Converting Single Column into Multiple rows, but with strings to specific tab column

Dear fellows, I need your help. I'm trying to write a script to convert a single column into multiple rows. But it need to recognize the beginning of the string and set it to its specific Column number. Each Line (loop) begins with digit (RANGE). At this moment it's kind of working, but it... (6 Replies)
Discussion started by: AK47
6 Replies

4. Shell Programming and Scripting

Difference of the same column when two other column matches and one column differs less than 1 hour

This is my input file : # cat list 20130430121600, cucm, location,76,2 20130430121600,cucm1,location1,76,4 20130430122000,cucm,location,80,8 20130430122000,cucm1,location1,90,8 20130430140000,cucm1,location1,87,11 20130430140000, cucm,location,67,9 This is the required output ... (1 Reply)
Discussion started by: Lakshmikumari
1 Replies

5. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

6. UNIX for Dummies Questions & Answers

Rename a header column by adding another column entry to the header column name

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (1 Reply)
Discussion started by: Vavad
1 Replies

7. Shell Programming and Scripting

Rename a header column by adding another column entry to the header column name URGENT!!

Hi All, I have a file example.csv which looks like this GrpID,TargetID,Signal,Avg_Num CSCH74_1_1,2007,61,256 CSCH74_1_1,212007,647,679 CSCH74_1_1,12007,3,32 CSCH74_1_1,207,299,777 I want the output as GrpID,TragetID,Signal-CSCH74_1_1,Avg_Num CSCH74_1_1,2007,61,256... (4 Replies)
Discussion started by: Vavad
4 Replies

8. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

9. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

10. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies
Login or Register to Ask a Question