Issue
I'm working with some data using bash and I need that this kind of input file:
Col1 Col2
A B
C D
E F
G H
Col1
A
B
C
D
E
F
G
H
I tried with some commands but they didn't work. Any kind of suggestions will be very appreciated!
Solution
As with many problems, there are many solutions. Here is one using awk
:
awk 'NR > 1 {print $1; print $2}' inputfile.txt
The NR > 1
expression says to execute the following block for all line numbers greater than one. (NR is the current record number which is the same as line number by default.)
The {print $1; print $2}
code block says to print the first field, then print the second field. The advantage of using awk
in this case is that it doesn't matter if the fields are separated by space characters, tabs, or a combination; the fields just have to be separated by some number of whitespace characters.
If the field values on each line are only separated by a single space character, then this would work:
tail -n +2 inputfile.txt | tr ' ' '\n'
In this solution, tail -n +2
is used to print all lines starting with the second line and tr ' ' '\n'
is used to replace all the space characters with newlines, as suggested by previously.
Answered By - Greg Barrett Answer Checked By - Mildred Charles (WPSolving Admin)