I have these output values of an Arduino Sensor saved to text file like this
9 P2.5=195.60 P10=211.00
10 P2.5=195.70 P10=211.10
11 P2.5=195.70 P10=211.10
2295 P2.5=201.20 P10=218.20
2300 P2.5=201.40 P10=218.40
...
...
And I want to extract each column to its own text file.
Expected Output: 3 text Files Number.txt, P25.txt and P10.txt where
Number.txt contains
9
10
11
2295
2300
P25.txt contains
195.60
195.70
195.70
201.20
201.40
and P10.txt contains
211.00
211.10
211.10
218.20
218.40
PS: the file has more than just 5 lines, so the code should be applied to every line.
CodePudding user response:
Here is how you could do:
$ grep -Po '^[0-9.] ' data.txt > Number.txt
$ grep -Po '(?<=P2\.5=)[0-9.] ' data.txt > P25.txt
$ grep -Po '(?<=P10=)[0-9.] ' data.txt > P10.txt
^
: Assert position at the start of the line.[0-9.]
Matches either a digit or a dot, between one and unlimited times, as much as possible.(?<=)
: Positive lookbehind.P2\.5=
: MatchesP2.5=
.P10=
: MatchesP10=
.
-o
: Print only matching part.-P
: Perl style regex.
CodePudding user response:
Use awk
, which can open files itself rather than rely on standard output.
awk '{sub("P2.5=", "", $2);
sub("P10=", "", $3);
print $1 > "Number.txt";
print $2 > "P25.txt";
print $3 > "P10.txt"; }' data.txt
or
awk '{print $1 > "Number.txt";
print substr($2, 6) > "P25.txt";
print substr($3, 5) > "P10.txt"; }' data.txt