awk how to find first available date field?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Fields 1,2,3,4 are date fields yyyy-mm-dd.
Delimited by ";"
"-" if no date.
Field 4 will always have a date
Examples;
-; 2016-08-19; 2016-08-19; 2018-07-17; Beach-Rangiroa.jpg
-; -; -; 2018-09-12; MV3_0034-copy.webp
2016-12-10; 2016-12-10; 2016-12-20; 2018-07-18; Sukhothai-61.jpg
-; -; -; 2018-07-19; Gdu9Rwhu6W3Q5W6q_1Qag.jpg
Objective: Use awk to print the 1st available date in order fields 1,2,3,4
I've tried this;
awk -F";" '{if ($1!="-") print $1; else if ($2!="-") print $2; else if ($3!="-") prin$3; else if ($4!="-") print $4}'
Results...
2016-08-19
-
-
bash version 4.3.48
I am trying to achieve this: e.g. line 1 in example...
2016-08-19; Beach-Rangiroa.jpg
echo '-; -; -; 2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333' |
awk -F';' 'OFS=";" {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333
This works nicely, except the 1st space on the date, also is there a method available to verify the date, e.g. date -d "%Y-%m-%d" ?
Thank you.
bash awk
add a comment |
Fields 1,2,3,4 are date fields yyyy-mm-dd.
Delimited by ";"
"-" if no date.
Field 4 will always have a date
Examples;
-; 2016-08-19; 2016-08-19; 2018-07-17; Beach-Rangiroa.jpg
-; -; -; 2018-09-12; MV3_0034-copy.webp
2016-12-10; 2016-12-10; 2016-12-20; 2018-07-18; Sukhothai-61.jpg
-; -; -; 2018-07-19; Gdu9Rwhu6W3Q5W6q_1Qag.jpg
Objective: Use awk to print the 1st available date in order fields 1,2,3,4
I've tried this;
awk -F";" '{if ($1!="-") print $1; else if ($2!="-") print $2; else if ($3!="-") prin$3; else if ($4!="-") print $4}'
Results...
2016-08-19
-
-
bash version 4.3.48
I am trying to achieve this: e.g. line 1 in example...
2016-08-19; Beach-Rangiroa.jpg
echo '-; -; -; 2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333' |
awk -F';' 'OFS=";" {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333
This works nicely, except the 1st space on the date, also is there a method available to verify the date, e.g. date -d "%Y-%m-%d" ?
Thank you.
bash awk
If you clean up your question to get rid of all the edits and just tell us the question with the sample input/output it'll be much easier for us to help you.
– Ed Morton
Nov 26 '18 at 1:54
add a comment |
Fields 1,2,3,4 are date fields yyyy-mm-dd.
Delimited by ";"
"-" if no date.
Field 4 will always have a date
Examples;
-; 2016-08-19; 2016-08-19; 2018-07-17; Beach-Rangiroa.jpg
-; -; -; 2018-09-12; MV3_0034-copy.webp
2016-12-10; 2016-12-10; 2016-12-20; 2018-07-18; Sukhothai-61.jpg
-; -; -; 2018-07-19; Gdu9Rwhu6W3Q5W6q_1Qag.jpg
Objective: Use awk to print the 1st available date in order fields 1,2,3,4
I've tried this;
awk -F";" '{if ($1!="-") print $1; else if ($2!="-") print $2; else if ($3!="-") prin$3; else if ($4!="-") print $4}'
Results...
2016-08-19
-
-
bash version 4.3.48
I am trying to achieve this: e.g. line 1 in example...
2016-08-19; Beach-Rangiroa.jpg
echo '-; -; -; 2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333' |
awk -F';' 'OFS=";" {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333
This works nicely, except the 1st space on the date, also is there a method available to verify the date, e.g. date -d "%Y-%m-%d" ?
Thank you.
bash awk
Fields 1,2,3,4 are date fields yyyy-mm-dd.
Delimited by ";"
"-" if no date.
Field 4 will always have a date
Examples;
-; 2016-08-19; 2016-08-19; 2018-07-17; Beach-Rangiroa.jpg
-; -; -; 2018-09-12; MV3_0034-copy.webp
2016-12-10; 2016-12-10; 2016-12-20; 2018-07-18; Sukhothai-61.jpg
-; -; -; 2018-07-19; Gdu9Rwhu6W3Q5W6q_1Qag.jpg
Objective: Use awk to print the 1st available date in order fields 1,2,3,4
I've tried this;
awk -F";" '{if ($1!="-") print $1; else if ($2!="-") print $2; else if ($3!="-") prin$3; else if ($4!="-") print $4}'
Results...
2016-08-19
-
-
bash version 4.3.48
I am trying to achieve this: e.g. line 1 in example...
2016-08-19; Beach-Rangiroa.jpg
echo '-; -; -; 2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333' |
awk -F';' 'OFS=";" {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-15; Stock-Photo-114398301.webp; WEBP; image/webp; 2000; 1333
This works nicely, except the 1st space on the date, also is there a method available to verify the date, e.g. date -d "%Y-%m-%d" ?
Thank you.
bash awk
bash awk
edited Nov 26 '18 at 4:01
DooUKnow
asked Nov 25 '18 at 8:59
DooUKnowDooUKnow
62
62
If you clean up your question to get rid of all the edits and just tell us the question with the sample input/output it'll be much easier for us to help you.
– Ed Morton
Nov 26 '18 at 1:54
add a comment |
If you clean up your question to get rid of all the edits and just tell us the question with the sample input/output it'll be much easier for us to help you.
– Ed Morton
Nov 26 '18 at 1:54
If you clean up your question to get rid of all the edits and just tell us the question with the sample input/output it'll be much easier for us to help you.
– Ed Morton
Nov 26 '18 at 1:54
If you clean up your question to get rid of all the edits and just tell us the question with the sample input/output it'll be much easier for us to help you.
– Ed Morton
Nov 26 '18 at 1:54
add a comment |
4 Answers
4
active
oldest
votes
This is a gnu only gawk
solution using FPAT
:
awk 'BEGIN{FPAT="[0-9]{4}-[0-9]{,2}-[0-9]{,2}"}{print $1}' file1
2016-08-19
2018-09-12
2018-07-19
With FPAT
you actually instruct gawk
what to consider as a field, a whole regex here. If the input line has also a second date this will appear as $2
, $NF
will return the last date field of each line,NF
will return the total date fields,and so on.
There's no FPAT in awk. Perhaps you're using gawk?
– ghoti
Nov 25 '18 at 15:09
doesn't work in macos El Capitan.
– Graham
Nov 25 '18 at 16:13
add a comment |
You can use a variable for field numbers:
awk -F; '{for(i=1; i<5; ++i) { if ($i ~ /[0-9]/) { print $i; next; }}}' in
Should be-F' *; *'
to trim the date value.FS
is a full regular expression, according to the man page. You can also set thei < NF
for the condition.
– Amessihel
Nov 25 '18 at 9:42
add a comment |
Solution without awk:
You said you wanted the 1st available date. When you only want 1 line output, you can use
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}" inputfile| head -1
When you want to have the first date for each line, change the grep
or use sed
:
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}.*" inputfile| cut -d';' -f1
# or
sed -r 's/([0-9]{4}-[0-9]{2}-[0-9]{2}).*/1/; s/.*([0-9]{4}-[0-9]{2}-[0-9]{2})/1/' inputfile
add a comment |
Thank you for all for your help.
I think this acomplishes the objective;
echo '-; -; -; 2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720' |
awk -F'; ' 'OFS="; " {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720
Best regards.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466019%2fawk-how-to-find-first-available-date-field%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a gnu only gawk
solution using FPAT
:
awk 'BEGIN{FPAT="[0-9]{4}-[0-9]{,2}-[0-9]{,2}"}{print $1}' file1
2016-08-19
2018-09-12
2018-07-19
With FPAT
you actually instruct gawk
what to consider as a field, a whole regex here. If the input line has also a second date this will appear as $2
, $NF
will return the last date field of each line,NF
will return the total date fields,and so on.
There's no FPAT in awk. Perhaps you're using gawk?
– ghoti
Nov 25 '18 at 15:09
doesn't work in macos El Capitan.
– Graham
Nov 25 '18 at 16:13
add a comment |
This is a gnu only gawk
solution using FPAT
:
awk 'BEGIN{FPAT="[0-9]{4}-[0-9]{,2}-[0-9]{,2}"}{print $1}' file1
2016-08-19
2018-09-12
2018-07-19
With FPAT
you actually instruct gawk
what to consider as a field, a whole regex here. If the input line has also a second date this will appear as $2
, $NF
will return the last date field of each line,NF
will return the total date fields,and so on.
There's no FPAT in awk. Perhaps you're using gawk?
– ghoti
Nov 25 '18 at 15:09
doesn't work in macos El Capitan.
– Graham
Nov 25 '18 at 16:13
add a comment |
This is a gnu only gawk
solution using FPAT
:
awk 'BEGIN{FPAT="[0-9]{4}-[0-9]{,2}-[0-9]{,2}"}{print $1}' file1
2016-08-19
2018-09-12
2018-07-19
With FPAT
you actually instruct gawk
what to consider as a field, a whole regex here. If the input line has also a second date this will appear as $2
, $NF
will return the last date field of each line,NF
will return the total date fields,and so on.
This is a gnu only gawk
solution using FPAT
:
awk 'BEGIN{FPAT="[0-9]{4}-[0-9]{,2}-[0-9]{,2}"}{print $1}' file1
2016-08-19
2018-09-12
2018-07-19
With FPAT
you actually instruct gawk
what to consider as a field, a whole regex here. If the input line has also a second date this will appear as $2
, $NF
will return the last date field of each line,NF
will return the total date fields,and so on.
edited Nov 25 '18 at 16:52
answered Nov 25 '18 at 9:29
George VasiliouGeorge Vasiliou
4,27221019
4,27221019
There's no FPAT in awk. Perhaps you're using gawk?
– ghoti
Nov 25 '18 at 15:09
doesn't work in macos El Capitan.
– Graham
Nov 25 '18 at 16:13
add a comment |
There's no FPAT in awk. Perhaps you're using gawk?
– ghoti
Nov 25 '18 at 15:09
doesn't work in macos El Capitan.
– Graham
Nov 25 '18 at 16:13
There's no FPAT in awk. Perhaps you're using gawk?
– ghoti
Nov 25 '18 at 15:09
There's no FPAT in awk. Perhaps you're using gawk?
– ghoti
Nov 25 '18 at 15:09
doesn't work in macos El Capitan.
– Graham
Nov 25 '18 at 16:13
doesn't work in macos El Capitan.
– Graham
Nov 25 '18 at 16:13
add a comment |
You can use a variable for field numbers:
awk -F; '{for(i=1; i<5; ++i) { if ($i ~ /[0-9]/) { print $i; next; }}}' in
Should be-F' *; *'
to trim the date value.FS
is a full regular expression, according to the man page. You can also set thei < NF
for the condition.
– Amessihel
Nov 25 '18 at 9:42
add a comment |
You can use a variable for field numbers:
awk -F; '{for(i=1; i<5; ++i) { if ($i ~ /[0-9]/) { print $i; next; }}}' in
Should be-F' *; *'
to trim the date value.FS
is a full regular expression, according to the man page. You can also set thei < NF
for the condition.
– Amessihel
Nov 25 '18 at 9:42
add a comment |
You can use a variable for field numbers:
awk -F; '{for(i=1; i<5; ++i) { if ($i ~ /[0-9]/) { print $i; next; }}}' in
You can use a variable for field numbers:
awk -F; '{for(i=1; i<5; ++i) { if ($i ~ /[0-9]/) { print $i; next; }}}' in
answered Nov 25 '18 at 9:05
perrealperreal
73.1k10113142
73.1k10113142
Should be-F' *; *'
to trim the date value.FS
is a full regular expression, according to the man page. You can also set thei < NF
for the condition.
– Amessihel
Nov 25 '18 at 9:42
add a comment |
Should be-F' *; *'
to trim the date value.FS
is a full regular expression, according to the man page. You can also set thei < NF
for the condition.
– Amessihel
Nov 25 '18 at 9:42
Should be
-F' *; *'
to trim the date value. FS
is a full regular expression, according to the man page. You can also set the i < NF
for the condition.– Amessihel
Nov 25 '18 at 9:42
Should be
-F' *; *'
to trim the date value. FS
is a full regular expression, according to the man page. You can also set the i < NF
for the condition.– Amessihel
Nov 25 '18 at 9:42
add a comment |
Solution without awk:
You said you wanted the 1st available date. When you only want 1 line output, you can use
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}" inputfile| head -1
When you want to have the first date for each line, change the grep
or use sed
:
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}.*" inputfile| cut -d';' -f1
# or
sed -r 's/([0-9]{4}-[0-9]{2}-[0-9]{2}).*/1/; s/.*([0-9]{4}-[0-9]{2}-[0-9]{2})/1/' inputfile
add a comment |
Solution without awk:
You said you wanted the 1st available date. When you only want 1 line output, you can use
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}" inputfile| head -1
When you want to have the first date for each line, change the grep
or use sed
:
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}.*" inputfile| cut -d';' -f1
# or
sed -r 's/([0-9]{4}-[0-9]{2}-[0-9]{2}).*/1/; s/.*([0-9]{4}-[0-9]{2}-[0-9]{2})/1/' inputfile
add a comment |
Solution without awk:
You said you wanted the 1st available date. When you only want 1 line output, you can use
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}" inputfile| head -1
When you want to have the first date for each line, change the grep
or use sed
:
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}.*" inputfile| cut -d';' -f1
# or
sed -r 's/([0-9]{4}-[0-9]{2}-[0-9]{2}).*/1/; s/.*([0-9]{4}-[0-9]{2}-[0-9]{2})/1/' inputfile
Solution without awk:
You said you wanted the 1st available date. When you only want 1 line output, you can use
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}" inputfile| head -1
When you want to have the first date for each line, change the grep
or use sed
:
grep -Eo "[0-9]{4}-[0-9]{2}-[0-9]{2}.*" inputfile| cut -d';' -f1
# or
sed -r 's/([0-9]{4}-[0-9]{2}-[0-9]{2}).*/1/; s/.*([0-9]{4}-[0-9]{2}-[0-9]{2})/1/' inputfile
answered Nov 25 '18 at 9:27
Walter AWalter A
11.2k21132
11.2k21132
add a comment |
add a comment |
Thank you for all for your help.
I think this acomplishes the objective;
echo '-; -; -; 2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720' |
awk -F'; ' 'OFS="; " {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720
Best regards.
add a comment |
Thank you for all for your help.
I think this acomplishes the objective;
echo '-; -; -; 2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720' |
awk -F'; ' 'OFS="; " {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720
Best regards.
add a comment |
Thank you for all for your help.
I think this acomplishes the objective;
echo '-; -; -; 2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720' |
awk -F'; ' 'OFS="; " {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720
Best regards.
Thank you for all for your help.
I think this acomplishes the objective;
echo '-; -; -; 2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720' |
awk -F'; ' 'OFS="; " {for(i=1; i<5; ++i) { if ($i ~ /[0-9]{4}-[0-9]{,2}-[0-9]{,2}/) { print $i,$5,$6,$7,$8,$9; next; }}}'
Result;
2018-07-25; Redwood-Forest-Sequoia-4.jpg; JPEG; image/jpeg; 1280; 720
Best regards.
answered Nov 25 '18 at 17:31
DooUKnowDooUKnow
62
62
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466019%2fawk-how-to-find-first-available-date-field%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you clean up your question to get rid of all the edits and just tell us the question with the sample input/output it'll be much easier for us to help you.
– Ed Morton
Nov 26 '18 at 1:54