Split Output of Command by Columns Using Bash

Split output of command by columns using Bash?

One easy way is to add a pass of tr to squeeze any repeated field separators out:

$ ps | egrep 11383 | tr -s ' ' | cut -d ' ' -f 4

Bash: Split stdout from multiple concurrent commands into columns

Regrettably answering my own question.

None of the supplied solutions were exactly what I was looking for. So I developed my own command line utility: multiview. Maybe others will benefit?

It works by piping processes' stdout/stderr to a command interface and then by launching a "viewer" to see their outputs in columns:

fooProcess | multiview -s & \
barProcess | multiview -s & \
bazProcess | multiview -s & \
multiview

This will display a neatly organized column view of their outputs. You can name each process as well by adding a string after the -s flag:

fooProcess | multiview -s "foo" & \
barProcess | multiview -s "bar" & \
bazProcess | multiview -s "baz" & \
multiview

There are a few other options, but thats the gist of it.

Hope this helps!

How do I split a string on a delimiter in Bash?

You can set the internal field separator (IFS) variable, and then let it parse into an array. When this happens in a command, then the assignment to IFS only takes place to that single command's environment (to read ). It then parses the input according to the IFS variable value into an array, which we can then iterate over.

This example will parse one line of items separated by ;, pushing it into an array:

IFS=';' read -ra ADDR <<< "$IN"
for i in "${ADDR[@]}"; do
# process "$i"
done

This other example is for processing the whole content of $IN, each time one line of input separated by ;:

while IFS=';' read -ra ADDR; do
for i in "${ADDR[@]}"; do
# process "$i"
done
done <<< "$IN"

Bash Split by column printf output

I found an other method with the loop with | column.

I share it:

for i in ${!HOSTTAB[@]}; do
printf "%3d%s) %s\n" $((i+1)) "${choices[i]:- }" "${HOSTTAB[i]}"
echo ""
done | column

bash: split output of command by columns without specific delimiter

You should use diskutil list -plist and parse the property list instead. The human-readable output is not meant to be machine readable, and might look different than you expect (column boundaries could move to accommodate long outputs or a wide terminal window, etc).

Bash: Split a single line of output with spaces into multiple lines of one word each

With GNU awk:

echo 'foo bar baz are your words' | awk '{NF-=3; OFS="\n"; $1=$1}1' | sort

NF-=3: Remove last 3 columns from current row.

OFS="\n": set output field separator to newline (or use OFS=ORS).

$1=$1: Forces awk to rebuild current row with new OFS (now newline).

1: awk evaluates 1 as true expression and outputs the current line.

Output:


bar
baz
foo

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Bash: how to split a line by a delimiter and print each element on a new line?

Could you please try following, it should be fast enough with awk(since you have mentioned a .csv Input_file so I have taken field separator as , if you don't have comma as a field separator then you may remove FS="," part from following code too).

awk 'BEGIN{FS=",";OFS="\n"} FNR==1{$1=$1;print;exit}' Input_file

Since you haven't posted examples, so considering following is the Input_file:

cat Input_file
a,b,c,d
1,2,3,4,5,

Now after running code will get following output.

awk 'BEGIN{FS=",";OFS="\n"} FNR==1{$1=$1;print;exit}' Input_file
a
b
c
d

Since I am using condition FNR==1 so it will read only very first Line, then I am using exit which will exit from program itself after completing operations on Line1 and will not read whole Input_file.

Explanation: Adding explanation for above code.

awk '             ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here.
FS="," ##Setting FS as comma here.
OFS="\n" ##Setting OFS as a new line here.
}
FNR==1{ ##Checking condition if this is first line then do following.
$1=$1 ##Re-arranging first field to make OFS into picture, since by default OFS is space and I am setting it as a new line.
print ##Printing current line here.
exit ##exit will make program exit.
}
' Input_file ##Mentioning Input_file name here.

How to get the second column from command output?

Or use sed & regex.

<some_command> | sed 's/^.* \(".*"$\)/\1/'

How to Split a Bash Array into Multiple columns in a Markdown table?

Here's the general approach:

$ cat tst.awk
BEGIN {
numCols = (numCols ? numCols : 5)
OFS = "|"
}
{
colNr = (NR - 1) % numCols + 1
if ( colNr == 1 ) {
numRows++
}
vals[numRows,colNr] = $0
}
END {
hdr2 = OFS
for (colNr=1; colNr<=numCols; colNr++) {
hdr2 = hdr2 "---" OFS
}
hdr1 = hdr2
gsub(/-/,"",hdr1)
print hdr1 ORS hdr2

for (rowNr=1; rowNr<=numRows; rowNr++) {
printf "|"
for (colNr=1; colNr<=numCols; colNr++) {
val = vals[rowNr,colNr]
printf "%s%s", val, (colNr<numCols ? OFS : ORS)
}
}
}

.

$ awk -f tst.awk file
||||||
|---|---|---|---|---|
|awk|accessibility|bash|behat|c++
|cache|d3.js|dates|engineering|elasticsearch

but it obviously doesn't output the columns in the order you asked for in your question as I don't understand how you arrive at that order.



Related Topics



Leave a reply



Submit