How to find the last field using 'cut'

2019-01-10 00:04发布

Without using sed or awk, only cut, how do I get the last field when the number of fields are unknown or change with every line?

标签: linux bash cut
10条回答
不美不萌又怎样
2楼-- · 2019-01-10 00:20

If your input string doesn't contain forward slashes then you can use basename and a subshell:

$ basename "$(echo 'maps.google.com' | tr '.' '/')"

This doesn't use sed or awk but it also doesn't use cut either, so I'm not quite sure if it qualifies as an answer to the question as its worded.

This doesn't work well if processing input strings that can contain forward slashes. A workaround for that situation would be to replace forward slash with some other character that you know isn't part of a valid input string. For example, the pipe (|) character is also not allowed in filenames, so this would work:

$ basename "$(echo 'maps.google.com/some/url/things' | tr '/' '|' | tr '.' '/')" | tr '|' '/'
查看更多
神经病院院长
3楼-- · 2019-01-10 00:26

You could try something like this:

echo 'maps.google.com' | rev | cut -d'.' -f 1 | rev

Explanation

  • maps.google.com's reverse will be moc.elgoog.spam
  • cut uses dot as the delimiter and chooses the first field, which is moc
  • lastly, we reverse it again (thanks for the reminder, @tom) to get com
查看更多
Juvenile、少年°
4楼-- · 2019-01-10 00:26

It is not possible using just cut. Here is a way using grep:

grep -o '[^,]*$'

Replace the comma for other delimiters.

查看更多
干净又极端
5楼-- · 2019-01-10 00:27

I realized if we just ensure a trailing delimiter exists, it works. So in my case I have comma and whitespace delimiters. I add a space at the end; $ ans="a, b" $ ans+=" "; echo ${ans} | tr ',' ' ' | tr -s ' ' | cut -d' ' -f2 b

查看更多
何必那么认真
6楼-- · 2019-01-10 00:29

If you have a file named filelist.txt that is a list paths such as the following: c:/dir1/dir2/file1.h c:/dir1/dir2/dir3/file2.h

then you can do this: rev filelist.txt | cut -d"/" -f1 | rev

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-01-10 00:38

This is the only solution possible for using nothing but cut:

echo "s.t.r.i.n.g." | cut -d'.' -f2- [repeat_following_part_forever_or_until_out_of_memory:] | cut -d'.' -f2-

Using this solution, the number of fields can indeed be unknown and vary from time to time. However as line length must not exceed LINE_MAX characters or fields, including the new-line character, then an arbitrary number of fields can never be part as a real condition of this solution.

Yes, a very silly solution but the only one that meets the criterias I think.

查看更多
登录 后发表回答