Script bash to split columns
2
0
Entering edit mode
4.0 years ago
mel22 ▴ 100

Hello, I have a data file with 4 values in same column. How to split each column in 4 labelled columns v1, v2,v3 and v4 with bash or R or python ?

example of my file :

Barcode        "FD1133"                              "FD1138"                
102            "-0.0570 0.0113 1.035 0.061"          " -0.3631 0.0065 0.842 0.045"
104            "-0.0334 1.0000 0.013 0.813"          "-0.0604 0.9639 0.052 0.764"

Thank you very much

bash script split • 3.2k views
ADD COMMENT
1
Entering edit mode

melania 2282 : Please don't delete posts once they have received an answer or comments.

ADD REPLY
0
Entering edit mode

if you can handle headers, try column -t

ADD REPLY
0
Entering edit mode

How does this help OP? column formats content for display, it does nothing to split or manipulate content in any way that awk can.

ADD REPLY
3
Entering edit mode
4.0 years ago
Mensur Dlakic ★ 28k

Assuming that your data is saved in a file named file.txt:

echo "v1\tv2\tv3\tv4" > FD1133_columns.txt
echo "v1\tv2\tv3\tv4" > FD1138_columns.txt
grep -v Barcode file.txt | awk -F '\"' '{print $2}' | awk '{print $1"\t"$2"\t"$3"\t"$4}' >> FD1133_columns.txt
grep -v Barcode file.txt | awk -F '\"' '{print $4}' | awk '{print $1"\t"$2"\t"$3"\t"$4}' >> FD1138_columns.txt
ADD COMMENT
0
Entering edit mode

thank you very much

ADD REPLY
0
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they work. This will help future users that might find this post find the right answer.

Upvote|Bookmark|Accept

ADD REPLY
1
Entering edit mode
4.0 years ago

Using the separate function from tidyr, assuming your data.frame is named df.

if(!require("tidyr")) install.packages("tidyr")
library(tidyr)
df <- separate(df, col = "FD1133", into = c("col_1","col_2","col_3","col_4"), sep = " ")

Do this for every column you want to split (just change the col = part of the code.)

ADD COMMENT
1
Entering edit mode

separate is part of tidyr. Why install the whole tidyverse set of packages? Plus, OP wants v[0-9]+ column names, not col_[0-9]+ column names.

ADD REPLY
1
Entering edit mode

Loading up tidyverse is my personal preference. However, I edited my initial point in case OP wants to know the exact package this function is coming from. As for the latter part of your question, I would hope OP would understand from my code that they can name the column to whatever they desire.

ADD REPLY
2
Entering edit mode

I feel like it's an unnecessary twist to give the exact code that OP needs with one simple needless difference. We should either give pseudocode/pointers or exact code with logic-based missing steps or exact functioning code. Exact code with a radnom semantic difference achieves next to nothing wrt educating the user.

ADD REPLY
3
Entering edit mode

Yes, I think you’re right here. I think as we progress into our programming, we tend to overlook things like this. I’ll keep this logic as I move forward with future response.

ADD REPLY
0
Entering edit mode

thank you very much

ADD REPLY

Login before adding your answer.

Traffic: 1743 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6