In Bash How to Split a Column in Several Column of Fixed Dimension

In bash how can I split a column in several column of fixed dimension

Using awk:

awk '
BEGIN {
# Numbers of rows to print
n=4;
}
{
# Add to array with key = 0, 1, 2, 3, 0, 1, 2, ..
l[(NR-1)%n] = l[(NR-1)%n] " " $0
};
END {
# print the array
for (i = 0; i < length(l); i++) {
print l[i];
}
}
' file

Splitting a large, complex one column file into several columns with awk

With GNU awk for multi-char RS and true multi dimensional arrays:

$ cat tst.awk
BEGIN {
RS = "(\\s*[()]\\s*)+"
OFS = ";"
}
NR>1 {
cell[NR][1]
split($0,cell[NR])
}
END {
for (rowNr=1; rowNr<=NF; rowNr++) {
for (colNr=2; colNr<=NR; colNr++) {
printf "%6s%s", cell[colNr][rowNr], (colNr<NR ? OFS : ORS)
}
}
}

$ awk -f tst.awk file
1; 11; 111
2; 22; 222
3; 33; 333
...; ...; ...

How to split one long columns into multiple columns shell script

I finally manager to get rid of the limit by adding -w LIMIT, I will put 5000 just to be on the safe side.

So in my case that would be :

NUMBER=$(cat HBS-A | cut -d"," -f1 | sed '/\--/d' | uniq | wc -l)
pr -ts" " --columns $NUMBER HBS-value -w 5000 | tr -s " " "," | sed 's/^,//' > HBS-table

Pandas split column into multiple columns by comma

In case someone else wants to split a single column (deliminated by a value) into multiple columns - try this:

series.str.split(',', expand=True)

This answered the question I came here looking for.

Credit to EdChum's code that includes adding the split columns back to the dataframe.

pd.concat([df[[0]], df[1].str.split(', ', expand=True)], axis=1)

Note: The first argument df[[0]] is DataFrame.

The second argument df[1].str.split is the series that you want to split.

split Documentation

concat Documentation

Split a string containing fixed length columns


This can be done with RegEx matching, and creating an array of custom objects. Something like this:

$AllRecords = Get-Content C:\Path\To\File.txt | Where{$_ -match "^(.)(.{3})(.{9})"} | ForEach{
[PSCustomObject]@{
'Col1' = $Matches[1]
'Col2' = $Matches[2]
'Col3' = $Matches[3]
}
}

That will take each line, match by how many characters are specified, and then create an object based off those matches. It collects all objects in an array and could be exported to CSV or whatever. The 'Col1', 'Col2' etc are just generic column headers I suggested due to a lack of better information, and could be anything you wanted.

Edit: Thank you iCodez for showing me, perhaps inadvertantly, that you can specify a language for your code samples!

How to split a file with 250k columns vertically?

Using awk, you can adjust n to the number you expect.

awk '{for (i=1;i<=NF;i++)
printf (i%n==0||i==NF)?$i RS:$i FS > "File" int((i-1)/n+1) ".txt"
}' n=5 file

How can I make a multiple-column output with 'awk' command?

You can do it with awk itself, but I prefer pr for this

$ # -2 is number of columns needed
$ # -s option specifies delimiter, default is tab
$ seq 6 | pr -2ts','
1,4
2,5
3,6
$ seq 6 | pr -3ts','
1,3,5
2,4,6

$ # you can also change horizontal/vertical order
$ seq 6 | pr -3ats','
1,2,3
4,5,6


Related Topics



Leave a reply



Submit