Help with Awk and regex or any thing else
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).
I have ~20 files all around 300 lines long, populated by data formatted like this:
62640 usec, 3500 usec
1640 usec, 480 usec
360 usec, 520 usec
1200 usec, 500 usec
340 usec, 520 usec
and I want to turn this into Arduino code, in the format
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.
Any ideas? I feel like this should be easy, I really need to learn regex.
text-processing awk
New contributor
add a comment |Â
up vote
1
down vote
favorite
I can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).
I have ~20 files all around 300 lines long, populated by data formatted like this:
62640 usec, 3500 usec
1640 usec, 480 usec
360 usec, 520 usec
1200 usec, 500 usec
340 usec, 520 usec
and I want to turn this into Arduino code, in the format
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.
Any ideas? I feel like this should be easy, I really need to learn regex.
text-processing awk
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).
I have ~20 files all around 300 lines long, populated by data formatted like this:
62640 usec, 3500 usec
1640 usec, 480 usec
360 usec, 520 usec
1200 usec, 500 usec
340 usec, 520 usec
and I want to turn this into Arduino code, in the format
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.
Any ideas? I feel like this should be easy, I really need to learn regex.
text-processing awk
New contributor
I can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).
I have ~20 files all around 300 lines long, populated by data formatted like this:
62640 usec, 3500 usec
1640 usec, 480 usec
360 usec, 520 usec
1200 usec, 500 usec
340 usec, 520 usec
and I want to turn this into Arduino code, in the format
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.
Any ideas? I feel like this should be easy, I really need to learn regex.
text-processing awk
text-processing awk
New contributor
New contributor
edited 19 mins ago
ilkkachu
52.7k679145
52.7k679145
New contributor
asked 31 mins ago
Craig Smith
61
61
New contributor
New contributor
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
There's no need for any regex here - just printf
to format the fields into a string:
$ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
add a comment |Â
up vote
1
down vote
you can try this:
#!/bin/bash
cat file |sed '1d' | while IFS= read line; do
n1=$(echo $line | awk 'print $1')
n2=$(echo $line | awk 'print $3')
echo "
delayMicroseconds($n1);
pulseIR($n2);
"
done
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
Alternatively you may try perl
as follows;
perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
add a comment |Â
up vote
1
down vote
With GNU sed:
$ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < data
delayMicroseconds(62640);
pulseIR(3500);
...
-E
tells sed
to use extended regular expressions(*), the s/pattern/replacement/
command runs a search-replace operation over the current line (sed
repeats the instructions given for each input line).
The pattern is ([0-9]+) usec, ([0-9]+) usec.*
where [0-9]
means any one digit, +
any number of the previous "atom", and (...)
saves ("captures") whatever was matched. So ([0-9]+)
means any number of any digits, and saves the result. The letters are matched as-is, and the final .*
matches any number of any characters, just to eat any possible garbage at end of line.
In the replacement, 1
and 2
are replaced with the saved contents of the (...)
groups (but the parenthesis themselves are literal here), and n
means the newline characters (that may be not work on all sed
s).
(*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed
online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s///
command.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
There's no need for any regex here - just printf
to format the fields into a string:
$ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
add a comment |Â
up vote
2
down vote
There's no need for any regex here - just printf
to format the fields into a string:
$ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
add a comment |Â
up vote
2
down vote
up vote
2
down vote
There's no need for any regex here - just printf
to format the fields into a string:
$ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
There's no need for any regex here - just printf
to format the fields into a string:
$ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
answered 27 mins ago
steeldriver
32.6k34980
32.6k34980
add a comment |Â
add a comment |Â
up vote
1
down vote
you can try this:
#!/bin/bash
cat file |sed '1d' | while IFS= read line; do
n1=$(echo $line | awk 'print $1')
n2=$(echo $line | awk 'print $3')
echo "
delayMicroseconds($n1);
pulseIR($n2);
"
done
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
Alternatively you may try perl
as follows;
perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
add a comment |Â
up vote
1
down vote
you can try this:
#!/bin/bash
cat file |sed '1d' | while IFS= read line; do
n1=$(echo $line | awk 'print $1')
n2=$(echo $line | awk 'print $3')
echo "
delayMicroseconds($n1);
pulseIR($n2);
"
done
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
Alternatively you may try perl
as follows;
perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
add a comment |Â
up vote
1
down vote
up vote
1
down vote
you can try this:
#!/bin/bash
cat file |sed '1d' | while IFS= read line; do
n1=$(echo $line | awk 'print $1')
n2=$(echo $line | awk 'print $3')
echo "
delayMicroseconds($n1);
pulseIR($n2);
"
done
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
Alternatively you may try perl
as follows;
perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
you can try this:
#!/bin/bash
cat file |sed '1d' | while IFS= read line; do
n1=$(echo $line | awk 'print $1')
n2=$(echo $line | awk 'print $3')
echo "
delayMicroseconds($n1);
pulseIR($n2);
"
done
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
Alternatively you may try perl
as follows;
perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);
answered 28 mins ago
Goro
7,20753168
7,20753168
add a comment |Â
add a comment |Â
up vote
1
down vote
With GNU sed:
$ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < data
delayMicroseconds(62640);
pulseIR(3500);
...
-E
tells sed
to use extended regular expressions(*), the s/pattern/replacement/
command runs a search-replace operation over the current line (sed
repeats the instructions given for each input line).
The pattern is ([0-9]+) usec, ([0-9]+) usec.*
where [0-9]
means any one digit, +
any number of the previous "atom", and (...)
saves ("captures") whatever was matched. So ([0-9]+)
means any number of any digits, and saves the result. The letters are matched as-is, and the final .*
matches any number of any characters, just to eat any possible garbage at end of line.
In the replacement, 1
and 2
are replaced with the saved contents of the (...)
groups (but the parenthesis themselves are literal here), and n
means the newline characters (that may be not work on all sed
s).
(*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed
online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s///
command.
add a comment |Â
up vote
1
down vote
With GNU sed:
$ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < data
delayMicroseconds(62640);
pulseIR(3500);
...
-E
tells sed
to use extended regular expressions(*), the s/pattern/replacement/
command runs a search-replace operation over the current line (sed
repeats the instructions given for each input line).
The pattern is ([0-9]+) usec, ([0-9]+) usec.*
where [0-9]
means any one digit, +
any number of the previous "atom", and (...)
saves ("captures") whatever was matched. So ([0-9]+)
means any number of any digits, and saves the result. The letters are matched as-is, and the final .*
matches any number of any characters, just to eat any possible garbage at end of line.
In the replacement, 1
and 2
are replaced with the saved contents of the (...)
groups (but the parenthesis themselves are literal here), and n
means the newline characters (that may be not work on all sed
s).
(*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed
online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s///
command.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
With GNU sed:
$ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < data
delayMicroseconds(62640);
pulseIR(3500);
...
-E
tells sed
to use extended regular expressions(*), the s/pattern/replacement/
command runs a search-replace operation over the current line (sed
repeats the instructions given for each input line).
The pattern is ([0-9]+) usec, ([0-9]+) usec.*
where [0-9]
means any one digit, +
any number of the previous "atom", and (...)
saves ("captures") whatever was matched. So ([0-9]+)
means any number of any digits, and saves the result. The letters are matched as-is, and the final .*
matches any number of any characters, just to eat any possible garbage at end of line.
In the replacement, 1
and 2
are replaced with the saved contents of the (...)
groups (but the parenthesis themselves are literal here), and n
means the newline characters (that may be not work on all sed
s).
(*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed
online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s///
command.
With GNU sed:
$ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < data
delayMicroseconds(62640);
pulseIR(3500);
...
-E
tells sed
to use extended regular expressions(*), the s/pattern/replacement/
command runs a search-replace operation over the current line (sed
repeats the instructions given for each input line).
The pattern is ([0-9]+) usec, ([0-9]+) usec.*
where [0-9]
means any one digit, +
any number of the previous "atom", and (...)
saves ("captures") whatever was matched. So ([0-9]+)
means any number of any digits, and saves the result. The letters are matched as-is, and the final .*
matches any number of any characters, just to eat any possible garbage at end of line.
In the replacement, 1
and 2
are replaced with the saved contents of the (...)
groups (but the parenthesis themselves are literal here), and n
means the newline characters (that may be not work on all sed
s).
(*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed
online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s///
command.
answered 23 mins ago
ilkkachu
52.7k679145
52.7k679145
add a comment |Â
add a comment |Â
Craig Smith is a new contributor. Be nice, and check out our Code of Conduct.
Craig Smith is a new contributor. Be nice, and check out our Code of Conduct.
Craig Smith is a new contributor. Be nice, and check out our Code of Conduct.
Craig Smith is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473867%2fhelp-with-awk-and-regex-or-any-thing-else%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password