I’m trying to split a tab delimitted field in bash.
I am aware of this answer: how to split a string in shell and get the last field
But that does not answer for a tab character.
I want to do get the part of a string before the tab character, so I’m doing this:
x=`head -1 my-file.txt`
echo ${x%\t*}
But the \t is matching on the letter ‘t’ and not on a tab. What is the best way to do this?
Thanks
If your file look something like this (with tab as separator):
you can use
cutto extract the first field (operates on tab by default):If you’re using
awk, there is no need to usetailto get the last line, changing the input to:Solution using awk:
Pure bash-solution:
outputs:
Lastly, a solution using
sedhere,
$is the range operator; i.e. operate on the last line only.For your original question, use a literal tab, i.e.
outputs: