My data sample seems:
1 2 3 4 5
4 5 6 7 9 9 0
1 2 3 4
1 8 7 6 9
and I want to change in to :
1
2
3
4
5
4
5
6
7
9
9
0
1
2
3
4
1
8
7
6
9
My data sample seems:
1 2 3 4 5
4 5 6 7 9 9 0
1 2 3 4
1 8 7 6 9
and I want to change in to :
1
2
3
4
5
4
5
6
7
9
9
0
1
2
3
4
1
8
7
6
9
If input lines doesn't have spaces use this:
fold -1 data_sample.txt
fold - wrap each input line to fit in specified width.
In this case the width is the one column, specified by -1
option.
If input has spaces, but you want omit them, use this:
grep -o '\S' data_sample.txt
\S
- matches any non-white-space character.
-o
- print every matched parts on a separate line.
Using the rs
(reshape) utility:
$ rs 0 1 < data
1
2
3
4
5
4
5
6
7
9
9
0
1
2
3
4
1
8
7
6
9
The output array shape is determined from the optional row
and col
arguments as follows:
If only one of them is a positive integer, rs computes a value for the
other which will accommodate all of the data.
In this case, 0
is not a positive integer, so the number of rows is chosen to put all the fields in a single column.
You could use tr
command as follows:
tr -s ' ' '\n' < infile
and here are other options to do that.
sed -e $'s/\s*/\\\n/g' infile
Or:
sed 's/\s*/\
/g' infile.txt
Or in some sed
implementations, use:
sed 's/\s*/\n/g' infile
Or via gawk
(if you don't mind last empty line):
awk -v RS='[[:blank:]]*' '1' infile
Or in bash
:
#!/bin/bash
while read -r line; do
echo "${line// \+/$'\n'}";
done < infile
Or reading to an array and then printf
:
#!/bin/bash
while read -a fields; do
printf "%s\n" "${fields[@]}";
done < infile
$ tr ' ' '\n' <file.in
This will replace all spaces in the input file with newlines.
The result will be
2
3
4
5
(etc.)
To save the output to a file:
$ tr ' ' '\n' <file.in >file.out
awk '{ for(i=1;i<=NF;i++) print $i; }' input
or if blank lines shall be kept:
awk '/^$/ { print; }; { for(i=1;i<=NF;i++) print $i; }' input
fold -1 file
?
Aug 28, 2017 at 0:30