Variable Substitution

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I have a variable assigned to a returned string:
ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)
I want to substring last 2 characters:
ytd_wk=$ytd_wk:(-2)
Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:
ytd_wk=$ tail -1):(-2)
shell variable-substitution
add a comment |Â
up vote
2
down vote
favorite
I have a variable assigned to a returned string:
ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)
I want to substring last 2 characters:
ytd_wk=$ytd_wk:(-2)
Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:
ytd_wk=$ tail -1):(-2)
shell variable-substitution
2
How do you define âÂÂone-linerâÂÂ?â You could just put your first two commands together on one line, separated byâ¯&∨.
â G-Man
Nov 20 '17 at 21:03
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a variable assigned to a returned string:
ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)
I want to substring last 2 characters:
ytd_wk=$ytd_wk:(-2)
Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:
ytd_wk=$ tail -1):(-2)
shell variable-substitution
I have a variable assigned to a returned string:
ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)
I want to substring last 2 characters:
ytd_wk=$ytd_wk:(-2)
Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:
ytd_wk=$ tail -1):(-2)
shell variable-substitution
edited Nov 21 '17 at 1:53
asked Nov 20 '17 at 18:54
lovechillcool
336
336
2
How do you define âÂÂone-linerâÂÂ?â You could just put your first two commands together on one line, separated byâ¯&∨.
â G-Man
Nov 20 '17 at 21:03
add a comment |Â
2
How do you define âÂÂone-linerâÂÂ?â You could just put your first two commands together on one line, separated byâ¯&∨.
â G-Man
Nov 20 '17 at 21:03
2
2
How do you define âÂÂone-linerâÂÂ?â You could just put your first two commands together on one line, separated byâ¯
&& or ;.â G-Man
Nov 20 '17 at 21:03
How do you define âÂÂone-linerâÂÂ?â You could just put your first two commands together on one line, separated byâ¯
&& or ;.â G-Man
Nov 20 '17 at 21:03
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
accepted
Try to use:
grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'
You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.
The full solution:
ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')
You can omit tail with GNU sed '$!d':
grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'
POSIX:
grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'
2
+1 for not usingcatin your answer â although it would have been better it you had explained whycatis unnecessary.
â G-Man
Nov 20 '17 at 21:00
add a comment |Â
up vote
1
down vote
You could use awk, combining the cat, grep, sed, and tail of other suggestions:
awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv
Writing this out step-by-step
-v year=$(date +'%Y')sets theawkvariableyearto the current year$0 ~ year line=$0this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in theawkvariablelineEND print substr(line,length(line)-1,2)at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.
add a comment |Â
up vote
1
down vote
This is more efficient, especially if file.csv is large and the desired line is nearer to the end:
ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"
How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Try to use:
grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'
You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.
The full solution:
ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')
You can omit tail with GNU sed '$!d':
grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'
POSIX:
grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'
2
+1 for not usingcatin your answer â although it would have been better it you had explained whycatis unnecessary.
â G-Man
Nov 20 '17 at 21:00
add a comment |Â
up vote
3
down vote
accepted
Try to use:
grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'
You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.
The full solution:
ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')
You can omit tail with GNU sed '$!d':
grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'
POSIX:
grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'
2
+1 for not usingcatin your answer â although it would have been better it you had explained whycatis unnecessary.
â G-Man
Nov 20 '17 at 21:00
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Try to use:
grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'
You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.
The full solution:
ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')
You can omit tail with GNU sed '$!d':
grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'
POSIX:
grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'
Try to use:
grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'
You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.
The full solution:
ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')
You can omit tail with GNU sed '$!d':
grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'
POSIX:
grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'
edited Nov 21 '17 at 6:59
answered Nov 20 '17 at 19:09
Egor Vasilyev
1,792129
1,792129
2
+1 for not usingcatin your answer â although it would have been better it you had explained whycatis unnecessary.
â G-Man
Nov 20 '17 at 21:00
add a comment |Â
2
+1 for not usingcatin your answer â although it would have been better it you had explained whycatis unnecessary.
â G-Man
Nov 20 '17 at 21:00
2
2
+1 for not using
cat in your answer â although it would have been better it you had explained why cat is unnecessary.â G-Man
Nov 20 '17 at 21:00
+1 for not using
cat in your answer â although it would have been better it you had explained why cat is unnecessary.â G-Man
Nov 20 '17 at 21:00
add a comment |Â
up vote
1
down vote
You could use awk, combining the cat, grep, sed, and tail of other suggestions:
awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv
Writing this out step-by-step
-v year=$(date +'%Y')sets theawkvariableyearto the current year$0 ~ year line=$0this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in theawkvariablelineEND print substr(line,length(line)-1,2)at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.
add a comment |Â
up vote
1
down vote
You could use awk, combining the cat, grep, sed, and tail of other suggestions:
awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv
Writing this out step-by-step
-v year=$(date +'%Y')sets theawkvariableyearto the current year$0 ~ year line=$0this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in theawkvariablelineEND print substr(line,length(line)-1,2)at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could use awk, combining the cat, grep, sed, and tail of other suggestions:
awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv
Writing this out step-by-step
-v year=$(date +'%Y')sets theawkvariableyearto the current year$0 ~ year line=$0this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in theawkvariablelineEND print substr(line,length(line)-1,2)at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.
You could use awk, combining the cat, grep, sed, and tail of other suggestions:
awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv
Writing this out step-by-step
-v year=$(date +'%Y')sets theawkvariableyearto the current year$0 ~ year line=$0this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in theawkvariablelineEND print substr(line,length(line)-1,2)at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.
answered Nov 20 '17 at 23:47
roaima
39.9k546109
39.9k546109
add a comment |Â
add a comment |Â
up vote
1
down vote
This is more efficient, especially if file.csv is large and the desired line is nearer to the end:
ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"
How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.
add a comment |Â
up vote
1
down vote
This is more efficient, especially if file.csv is large and the desired line is nearer to the end:
ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"
How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This is more efficient, especially if file.csv is large and the desired line is nearer to the end:
ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"
How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.
This is more efficient, especially if file.csv is large and the desired line is nearer to the end:
ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"
How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.
edited Nov 21 '17 at 9:25
answered Nov 21 '17 at 1:42
agc
4,1101935
4,1101935
add a comment |Â
add a comment |Â
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%2f405827%2fvariable-substitution%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
2
How do you define âÂÂone-linerâÂÂ?â You could just put your first two commands together on one line, separated byâ¯
&∨.â G-Man
Nov 20 '17 at 21:03