If I do not know the number of words in a line then how do I use cut such a way that I get the last word? [duplicate] - bash

This question already has answers here:
How do I get the last word in each line with bash
(7 answers)
Closed 3 years ago.
for a file file.txt, the contents are:
Had repulsive dashwoods suspicion sincerity but advantage now him.
Remark easily garret nor nay.
Civil those mrs enjoy shy fat merry. You greatest jointure saw horrible.
He private he on be imagine suppose.
Fertile beloved evident through no service elderly is.
Now I want to cut such a way that I only get the last words of the lines
I tried
cut -d" " -f1- file.txt
but that just gives all from start to end.
cut -d" " -f-1 file.txt
This just gives the first word.

You can do that easily with awk:
echo "Hi there Zeeshan." | awk '{print $NF}'
would print Zeeshan.
For your file:
awk '{print $NF}' file.txt

Related

How can I combine multiple regular expression conditions in a single awk command? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Type the shell command, which says how many times it has a whitespace in a particular file, and that it consists of at least four numbers (for example: "" 1945 "").
When I tried to solve the above exercise, I could not reach the result that I wanted, I want your help in this subject.
First of all, I created a txt file and filled it with random numbers. - sign represents spaces.
---234352432-
-123---
-12342---
-1-
-12345-
122333
I made a code to find the count of the numbers with more than 4 digits and has spaces in front of and behind of numbers.
cat text1.txt | awk '/^[[:space:]]&&[0-9]{4,}&&[[:space:]]$/' | awk 'END {print NR}'
returned 0
cat text1.txt | awk '/^" "/' | awk '/[0-9] {4, }/' | awk '/" "$/' | awk '{print NR}'
returned 6
this might be easier with grep
$ grep -Ec '\s[0-9]{4,}\s' file
3
to verify the matches
$ grep -E '\s[0-9]{4,}\s' file | tr ' ' '-'
---234352432--
-12342---
-12345-
To match a line that starts with white space then has 4 or more contiguous digits then white space to the end of the line:
$ awk '/^[[:space:]]+[0-9]{4,}[[:space:]]+$/{c++} END{print c+0}' file
3
To match a line that starts with white space, ends with white space and contains 4 or more contiguous digits somewhere on the line:
$ awk '/^[[:space:]]+/ && /[0-9]{4,}/ && /[[:space:]]+$/{c++} END{print c+0}' file
3
They'll behave the same with the input you provided but try them both with:
3 foo 12345 bar 7
for example (where that line has blanks at the start and end).
You never need to cat a file into a pipe to awk (or any other command), nor do you need a pipeline of multiple awk commands (nor pipes of awk+sed+grep, etc.) so if you ever find yourself doing any of that know you're using the wrong approach.
$ awk '{for(i=1; i<=NF; i++) {if($i ~ /^[0-9]/&&$i>999) {print $i}}}' text1.txt >> text2.txt | awk 'END {print NR}' text2.txt
That worked on my case. Thank u for everything

How to only read the last line from a text file [duplicate]

This question already has answers here:
How to read the last line of a text file into a variable using Bash? [closed]
(2 answers)
Print the last line of a file, from the CLI
(5 answers)
Closed 4 years ago.
I am working on a tool project. I need to grab the last line from a file & assign into a variable. This is what I have tried:
line=$(head -n $NF input_file)
echo $line
Maybe I could read the file in reverse then use
line=$(head -n $1 input_file)
echo $line
Any ideas are welcome.
Use tail ;)
line=$(tail -n 1 input_file)
echo $line
Combination of tac and awk here. Benefit in this approach could be we need NOT to read complete Input_file in it.
tac Input_file | awk '{print;exit}'
With sed or awk :
sed -n '$p' file
sed '$!d' file
awk 'END{print}' file
However, tail is still the right tool to do the job.

How to iterate through line and check needed part? [duplicate]

This question already has an answer here:
How can I retrieve an entry from /etc/passwd for a given username?
(1 answer)
Closed 5 years ago.
I have this line
Username:x:120:101:somethingsomething
and I need to get the '101' part after the third ':', how can I do that?
do I use grep or sed?
cut -d':' -f4 /etc/passwd
awk, only with string:
mstr="Username:x:120:101:somethingsomething"; awk -F: '{print $4}' <<< "$mstr"

Get the extension of file [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 7 years ago.
I have files with "multiple" extension , for better manipulation I would like to create new folder for each last extension but first I need to retrieve the last extension.
Just for example lets assume i have file called info.tar.tbz2 how could I get "tbz2" ?
One way that comes to my mind is using cut -d "." but in this case I would need to specify -f parameter of the last column, which I don't know how to achieve.
What is the fastest way to do it?
You may use awk,
awk -F. '{print $NF}' file
or
sed,
$ echo 'info.tar.tbz2' | awk -F. '{print $NF}'
tbz2
$ echo 'info.tar.tbz2' | sed 's/.*\.//'
tbz2

Print the last field using 'cut' [duplicate]

This question already has answers here:
How to find the last field using 'cut'
(14 answers)
Closed 6 years ago.
Let's say I've the following data:
ae722d1d94dcd3b161af166936:!]z#.:123
09338b2b29919e6c0daefbcce0361335:21f2e48d86f28ab7cd5c9c95:123
$H$92Gh.gRJwECLPzkjACPihoLg/:123
c6f1cb5f1feeece60f9a8e067e0:v4Zz'Cj5_Ze{J+iRW{2z,<~:123
202cb962a75b964b07152d234b70:123
40bd0015630c35165329ea5ecbdbbeef:123
(Invaild hashes, for explaining purpose only)
I want to use the cut tool in printing the last field only.
123
123
123
123
123
123
I don't want to use rev command.
I want nothing other than cut, I know how to do it in sed, awk.
--complement flag might help!
For that specific data you can:
cut -f2- -d: file | cut -f2 -d:
If you might have three colons:
cut -f2- -d: file | cut -f2- -d: | cut -f2 -d:
You can keep adding more cuts as needed.
The trick is that cut does nothing to a record that doesn't have the delimiter so you can use successive cuts to chop off the first field while leaving the last fields that you've already found alone.
i saw this on another place when I was looking for this answer.
rev, cut (on delimiter) -f 1, rev
move the last field forward, cut it, and move it back
i thought that was kinda slick

Resources