Columns in textfile merged, how to add space inbetween? (coordinaates)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Columns in textfile merged, how to add space inbetween? (coordinaates)
# 1  
Old 10-07-2017
Columns in textfile merged, how to add space inbetween? (coordinaates)

Hello!

I have a problem with a text file containing atom coordinates. After a converting step using some software, the structure of the textfile got a little messed up. Some columns merged, because the numbers got too large. My text file has 119’000 lines like:
Code:
 POPC  [number]  [x-coordinate]  [y-coordinate]  [z-coordinate]  1.00  0.00  C

If the textfile looks normal (see below), I can use “awk” to extract each column with coordinates (column 3, 4 and 5) into a new text file to manipulate them further using excel. (I calculate the mass center of all atoms)


Ordinary part of the text file (note that there are spaces between the coordinates, that’s what I need):


Code:
 POPC  204     -40.966  -5.183  -0.747  1.00  0.00           C   
 POPC  205      -0.942  15.531  -1.471  1.00  0.00           C   
 POPC  206      -3.101   9.362   0.404  1.00  0.00           C   
 POPC  207     -54.098 -18.036  -0.198  1.00  0.00           C   
 POPC  208     -19.860  -6.746  -0.441  1.00  0.00           C   
 POPC  209      18.190   6.699  -2.129  1.00  0.00           C   
 POPC  210     -34.468  12.623  -0.444  1.00  0.00           C   
 POPC  211     -49.057  16.806   3.756  1.00  0.00           C   
 POPC  212     -48.038   0.885   1.784  1.00  0.00           C   
 POPC  213     -19.421   4.976   1.459  1.00  0.00           C

As I already mentioned, some lines merged and can look like this:


Code:
 POPC    1      24.021  43.473-103.486  1.00  0.00           C   
 POPC    2      36.132 -62.075 -97.909  1.00  0.00           C   
 POPC    3      90.078  38.715-103.089  1.00  0.00           C   
 POPC    4      14.122  41.935-101.416  1.00  0.00           C   
 POPC    5      24.143 -49.821-100.995  1.00  0.00           C

It can also have different numbers of digits/letters between the messed up lines (simple positional approach does not work).



Code:
 POPC   78      91.105   5.477-100.390  1.00  0.00           C   
 POPC    1      24.021  43.473-103.486  1.00  0.00           C   
 POPC  203     -21.043 104.805-100.465  1.00  0.00           C

Does anyone know, how to either get a space at the right place or still extract the numbers, even though they are not separated into columns anymore?
I am new to UNIX and I am already seeing me entering spaces for 5-10h.


Additional Information:
-The numbers after “POPC” go from 1-239 for 501 times.
-sample_coordinates.txt attached
-The order of the listings can not change (only within the POPC1-239 section that repeats, the order does not matter)
-This is no homework or course assignment.



Thank you very much!!Smilie

Last edited by chem_wizard; 10-07-2017 at 12:29 PM.. Reason: Added code tags
# 2  
Old 10-07-2017
Hi, try:
Code:
sed 's/\([0-9]\)-/\1 -/g' file

or simply:
Code:
sed 's/-/ -/g' file

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-07-2017
Thank you so much, it looks like it worked! I really need to learn the sed command, it's so powerful!
Do you just add a space before the last "-" in the line? I'm curious on how this worked.
# 4  
Old 10-07-2017
Hi, you're welcome Smilie

The first one replaces a digit followed by a minus sign with that same digit (using back reference \1), a space and a minus sign.

The second one just puts a space before any minus sign in the file..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

2. Shell Programming and Scripting

How to separate sorte different characters from one textfile and copy them in a new textfile?

My first post, so don't kill me :) Say i open some textfile with some example like this. on the table are handy, bread and wine Now i know exactly what is in and i want to separate and sorted it in terminal to an existing file with another 2 existing lines in like this: table plane ... (3 Replies)
Discussion started by: schwatter
3 Replies

3. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

4. Shell Programming and Scripting

Columns to rows with space

Hi, My input is in the following way a b c d e f I would like to have it printed as a b c d e f Any awk scripts are appreciated. (1 Reply)
Discussion started by: jacobs.smith
1 Replies

5. UNIX for Dummies Questions & Answers

Script to add text before the first word on a line in a textfile.

How can i make a script to add text before the first word on a line in a textfile : Example: Old line: is my place New line: this is my place Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (3 Replies)
Discussion started by: mjanssen
3 Replies

6. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

7. Shell Programming and Scripting

help with delimiting columns with a space

Hi all, i am writting a script to fix some problems we have with data that we need that contains 1000s of records. I have a text file with 3 columns of data. The problem is there should be a space between the end of the first column and the start of the second column. The majority of the data is... (7 Replies)
Discussion started by: borderblaster
7 Replies

8. Shell Programming and Scripting

Check if certain files exist in a directory, if not add name to a textfile

We recieve some logs on our windows box via FTP on a daily basis, in the same directory. I would like to check for missing logs files and add their name to a text file. Something like... Check if C:\logs\file1_currentdate exists (if not, add file1_currentdate to... (1 Reply)
Discussion started by: SunnyK
1 Replies

9. HP-UX

How to add space in between columns in VI editor

Hi, I am trying to add space after 3rd and 6th column for editing a file in special format. How should I achieve in VI?. So, is it possible to do in it in sed ? thanks vipa (1 Reply)
Discussion started by: vipas
1 Replies

10. Shell Programming and Scripting

Trying to space columns in a report

I have a program that calculates shipping cost for an item and prints the information to a file. I cannot seem to get the lines to print with equal spacing in columns. I need data like this to be aligned into columns: Optical Mouse 5 A $25 Compaq Presario 3... (2 Replies)
Discussion started by: turbulence
2 Replies
Login or Register to Ask a Question