I have a file that looks like this:
a b c d e f g h i j k l m
I want to reformat it like this:
abc def ghi jkl m
I want the number of columns to be tuned. How could you do with bash? I canβt think of anything.
host:~ user$ cat file a b c d e f g h i j k l m host:~ user$ xargs -L3 echo < file abc def ghi jkl m host:~ user$
Replace β3β with how many columns you want.
A slightly improved version of xargs answer would be:
xargs -n3 -r < file
This method will better cope with the previous space and will not create a single empty line without input.
Other:
zsh-4.3.11[t]% paste -d\ - - - < infile abc def ghi jkl m
Or (if you don't care about the last new line):
zsh-4.3.11[t]% awk 'ORS = NR % m ? FS : RS' m=3 infile abc def ghi jkl m %
column -x -c 30 /tmp/file abc def ghi jkl m
Yes, the span is not quite what you wanted, but it handled input with a variable size βbetterβ (better for some definitions).
Since the < operator changes the order of things, I understood this more intuitive approach:
<
cat file | xargs -n3
However, after tests with large files, the paste approach turned out to be much faster:
paste
cat file | paste -d\ - - -
Source: https://habr.com/ru/post/888658/More articles:Why do we need a discovery URL in OpenID - openidGoogle Chrome also has a console object. Where is its equivalent API page? - javascriptNotification that all properties have been changed in ViewModel - mvvm-lightUsing aspects from other cans - javaPHP 5 - The scope of a variable includes files without classes or functions - variablesWhy doesn't my recursive method from helper return every value? - methodsMultiple Sort with Common Expression Elements - vb.netC # Regex.Replace, Colon as Delimiter, Ignore DateTime format - jsonHow to sort search results by relevance and another field in Lucene.net - .netHow to remove xmlns from elements when generating XML using LINQ? - c #All Articles