For example if I have a file as follow:
1 2 3 4 5 6
7 8 9 10 11 12
And I want to reorganize this file as:
1
2
3
4
5
6
7
8
9
10
11
12
Can I use the awk
command for that or not?
For example if I have a file as follow:
1 2 3 4 5 6
7 8 9 10 11 12
And I want to reorganize this file as:
1
2
3
4
5
6
7
8
9
10
11
12
Can I use the awk
command for that or not?
There are multiple ways to achieve this.
With grep
:
grep -oE "[0-9]+" file
-o
flag prints only the matching patterns (the digits in this case), delimited by newline-E
activates extended regular expressions.With awk
:
awk 'OFS="\n"{$1=$1}1' file
OFS
defines the output field separator.$1=$1
because we changed the OFS, we need to rebuild the line by setting the first field to itself, this will force the rebuild.1
at least we need a true condition that the line is printed.With sed
:
TMP$ sed -r 's/ +/\n/g' File
1
2
3
4
5
6
7
8
9
10
11
12
Replace all continuous spaces with newline.
The naive AWK approach:
#!/usr/bin/awk -f
{ for (i = 1; i <= NF; i++) print $i; }
Chaos's approach is probably more efficient.