how to find the last word in file & ignore empty spaces
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
how to find the last word in file even after word there are empty lines
the case
I tried this to find the last word
tail -1 /tmp/requests.txt
but no output
I try the following approach
awk 'END print $NF' /tmp/requests.txt
but no output
I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file
tail -6 /tmp/requests.txt
"IN_PROGRESS"
so what is the way to capture the last word in case we have empty space or not
expected results
echo $Last_word
IN_PROGRESS
linux text-processing awk sed perl
add a comment |Â
up vote
4
down vote
favorite
how to find the last word in file even after word there are empty lines
the case
I tried this to find the last word
tail -1 /tmp/requests.txt
but no output
I try the following approach
awk 'END print $NF' /tmp/requests.txt
but no output
I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file
tail -6 /tmp/requests.txt
"IN_PROGRESS"
so what is the way to capture the last word in case we have empty space or not
expected results
echo $Last_word
IN_PROGRESS
linux text-processing awk sed perl
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
how to find the last word in file even after word there are empty lines
the case
I tried this to find the last word
tail -1 /tmp/requests.txt
but no output
I try the following approach
awk 'END print $NF' /tmp/requests.txt
but no output
I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file
tail -6 /tmp/requests.txt
"IN_PROGRESS"
so what is the way to capture the last word in case we have empty space or not
expected results
echo $Last_word
IN_PROGRESS
linux text-processing awk sed perl
how to find the last word in file even after word there are empty lines
the case
I tried this to find the last word
tail -1 /tmp/requests.txt
but no output
I try the following approach
awk 'END print $NF' /tmp/requests.txt
but no output
I capture the word only by tail -6 ( because the word was 6 empty lines before the end of the file
tail -6 /tmp/requests.txt
"IN_PROGRESS"
so what is the way to capture the last word in case we have empty space or not
expected results
echo $Last_word
IN_PROGRESS
linux text-processing awk sed perl
edited Jan 3 at 15:04
Jeff Schaller
31.8k848109
31.8k848109
asked Jan 3 at 14:47
yael
2,0091145
2,0091145
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
7
down vote
accepted
Just start reading from the bottom and print the last word of the first line containing at least "something":
tac file | awk 'NFprint $NF; exit'
For example:
$ cat -vet file # note the spaces and tabs
hello$
bla ble bli$
$
^I$
$ tac file | awk 'NFprint $NF; exit'
bli
If you happen to not have tac
, just use the same logic when reading the file normally:
awk 'NFlast=$NF ENDprint last' file
That is, store the last word whenever there is "something" in a line. Finally, print the stored value.
2
tac
is the GNU equivalent of Unixtail -r
.
â Stéphane Chazelas
Jan 3 at 15:35
add a comment |Â
up vote
1
down vote
Keep track of the last word of the last non-empty line and print it at the end:
awk '/S/ s=$NF; END print(s); '
No need to keep the whole file in memory to print its lines in reverse order or anything like that.
add a comment |Â
up vote
0
down vote
sed -n '$p' example.txt | awk 'print $NF'
This will get the last line of the file and display the last column in the last line.
If last line is blank, nothing to print
â ctac_
Jan 3 at 17:21
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
Just start reading from the bottom and print the last word of the first line containing at least "something":
tac file | awk 'NFprint $NF; exit'
For example:
$ cat -vet file # note the spaces and tabs
hello$
bla ble bli$
$
^I$
$ tac file | awk 'NFprint $NF; exit'
bli
If you happen to not have tac
, just use the same logic when reading the file normally:
awk 'NFlast=$NF ENDprint last' file
That is, store the last word whenever there is "something" in a line. Finally, print the stored value.
2
tac
is the GNU equivalent of Unixtail -r
.
â Stéphane Chazelas
Jan 3 at 15:35
add a comment |Â
up vote
7
down vote
accepted
Just start reading from the bottom and print the last word of the first line containing at least "something":
tac file | awk 'NFprint $NF; exit'
For example:
$ cat -vet file # note the spaces and tabs
hello$
bla ble bli$
$
^I$
$ tac file | awk 'NFprint $NF; exit'
bli
If you happen to not have tac
, just use the same logic when reading the file normally:
awk 'NFlast=$NF ENDprint last' file
That is, store the last word whenever there is "something" in a line. Finally, print the stored value.
2
tac
is the GNU equivalent of Unixtail -r
.
â Stéphane Chazelas
Jan 3 at 15:35
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
Just start reading from the bottom and print the last word of the first line containing at least "something":
tac file | awk 'NFprint $NF; exit'
For example:
$ cat -vet file # note the spaces and tabs
hello$
bla ble bli$
$
^I$
$ tac file | awk 'NFprint $NF; exit'
bli
If you happen to not have tac
, just use the same logic when reading the file normally:
awk 'NFlast=$NF ENDprint last' file
That is, store the last word whenever there is "something" in a line. Finally, print the stored value.
Just start reading from the bottom and print the last word of the first line containing at least "something":
tac file | awk 'NFprint $NF; exit'
For example:
$ cat -vet file # note the spaces and tabs
hello$
bla ble bli$
$
^I$
$ tac file | awk 'NFprint $NF; exit'
bli
If you happen to not have tac
, just use the same logic when reading the file normally:
awk 'NFlast=$NF ENDprint last' file
That is, store the last word whenever there is "something" in a line. Finally, print the stored value.
answered Jan 3 at 14:52
fedorqui
3,89221853
3,89221853
2
tac
is the GNU equivalent of Unixtail -r
.
â Stéphane Chazelas
Jan 3 at 15:35
add a comment |Â
2
tac
is the GNU equivalent of Unixtail -r
.
â Stéphane Chazelas
Jan 3 at 15:35
2
2
tac
is the GNU equivalent of Unix tail -r
.â Stéphane Chazelas
Jan 3 at 15:35
tac
is the GNU equivalent of Unix tail -r
.â Stéphane Chazelas
Jan 3 at 15:35
add a comment |Â
up vote
1
down vote
Keep track of the last word of the last non-empty line and print it at the end:
awk '/S/ s=$NF; END print(s); '
No need to keep the whole file in memory to print its lines in reverse order or anything like that.
add a comment |Â
up vote
1
down vote
Keep track of the last word of the last non-empty line and print it at the end:
awk '/S/ s=$NF; END print(s); '
No need to keep the whole file in memory to print its lines in reverse order or anything like that.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Keep track of the last word of the last non-empty line and print it at the end:
awk '/S/ s=$NF; END print(s); '
No need to keep the whole file in memory to print its lines in reverse order or anything like that.
Keep track of the last word of the last non-empty line and print it at the end:
awk '/S/ s=$NF; END print(s); '
No need to keep the whole file in memory to print its lines in reverse order or anything like that.
answered Jan 3 at 17:27
David Foerster
915616
915616
add a comment |Â
add a comment |Â
up vote
0
down vote
sed -n '$p' example.txt | awk 'print $NF'
This will get the last line of the file and display the last column in the last line.
If last line is blank, nothing to print
â ctac_
Jan 3 at 17:21
add a comment |Â
up vote
0
down vote
sed -n '$p' example.txt | awk 'print $NF'
This will get the last line of the file and display the last column in the last line.
If last line is blank, nothing to print
â ctac_
Jan 3 at 17:21
add a comment |Â
up vote
0
down vote
up vote
0
down vote
sed -n '$p' example.txt | awk 'print $NF'
This will get the last line of the file and display the last column in the last line.
sed -n '$p' example.txt | awk 'print $NF'
This will get the last line of the file and display the last column in the last line.
edited Jan 5 at 11:57
grg
1857
1857
answered Jan 3 at 15:07
Praveen Kumar BS
1,010128
1,010128
If last line is blank, nothing to print
â ctac_
Jan 3 at 17:21
add a comment |Â
If last line is blank, nothing to print
â ctac_
Jan 3 at 17:21
If last line is blank, nothing to print
â ctac_
Jan 3 at 17:21
If last line is blank, nothing to print
â ctac_
Jan 3 at 17:21
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%2f414560%2fhow-to-find-the-last-word-in-file-ignore-empty-spaces%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