Issue
I have a tab-delimited .txt file that has 4 rows and 4 columns. I want to extract numbers (integer, decimal number and scientific notation) from the txt file. The numbers are in row 2-4 and column 2-4 (the first row is the header, and the first column is the rowname). The content of the file is pasted below:
component sigma h2 h2_se
G -5.55758e-19 -0.0964725 26.3887
GxE 6.13144e-18 1.09647 26.3651
noise 0 0 0
This is the desired output, and due to further processing for this output, I would like to have newline \n
appended at the end.
-5.55758e-19 -0.0964725 26.3887 6.13144e-18 1.09647 26.3651 0 0 0
Any help is greatly appreciated!
Below is some code I tried, which does not yield what I want:
grep -o '0.[[:digit:]]*' myfile
grep -o '[[:digit:]]*' myfile
====================================================
Here's the code that worked for me, and a lot of thanks to @tink!
awk 'NR>1 {printf "%s %s %s ", $2,$3,$4}END{printf "\n"}' myfile
Solution
$ awk 'NR>1{$1=""; out=out $0} END{$0=out; $1=$1; print}' file
-5.55758e-19 -0.0964725 26.3887 6.13144e-18 1.09647 26.3651 0 0 0
Alternatively either of these would also work:
$ awk 'NR>1{printf "%s%s %s %s", sep, $2, $3, $4; sep=OFS} END{print ""}' file
-5.55758e-19 -0.0964725 26.3887 6.13144e-18 1.09647 26.3651 0 0 0
$ awk 'NR>1{printf "%s%s %s %s", (NR>2 ? OFS : ""), $2, $3, $4} END{print ""}' file
-5.55758e-19 -0.0964725 26.3887 6.13144e-18 1.09647 26.3651 0 0 0
If your first field can contain a blank then add -F'\t'
at the start of the script.
Answered By - Ed Morton