03-25-2021 11:22 AM - edited 03-25-2021 11:50 AM
Hi all,
I've played a little bit with the cut command from the IOS.sh and wonder how I can define white spaces as delimiter.
Using pure text works:
switch#sh ip int br | cut -f 1 -d unassigned Interface IP-Address OK? Method Stat GigabitEthernet0/0 GigabitEthernet0/1 GigabitEthernet0/2 GigabitEthernet0/3
But that's, of course, not what one wants. Does anybody know how to define whitespaces as delimiter?
Thanks
Thomas
03-25-2021 01:37 PM
I haven't tried it in the IOS shell but this would do it at a Unix shell prompt.
cut -f1 -d' '
03-26-2021 01:50 AM
Elliot,
thanks for your reply.
Unfortunately, this doesn't work on IOS Shell (only for the leftmost column). I suspect, it should be some RegEx, but wasn't able to make it work.
Thomas
03-26-2021 03:08 AM
Is awk available? cut expects single character delimiters and you have to specify the delimiter. awk defaults to any amount of white space being the delimiter.
# echo hello there | awk '{print $1}' - hello # echo "hello there" | awk '{print $2}' - there
03-26-2021 03:30 AM
No, awk ist not available. It's a very limited selection of Unix tools.
03-26-2021 03:38 AM
It could be that your delimiter is a tab, not a space. tab is the default delimiter.
# echo "hello there" | cut -f2 there # echo "hello there junior" | cut -f3 # echo "hello there junior" | cut -f4 junior
03-26-2021 05:36 AM
Elliot,
I can't replicate this on IOS Shell.
switch#echo "hello there" | cut -f2
Gives me just an empty line as output (tested on a 3750E with IOS 15.2(2)E7).
Thanks for your efforts!
Thomas
03-26-2021 05:46 AM
Instead of the space in the "hello there", make that a tab. Then I bet you will get the right output.
03-30-2021 10:23 PM
How can I insert a tab in the IOS CLI? The tab key is reserved for command expension.
The command output I would like to cut don't come with tabs in them, so I must deal with multiple spaces.
03-31-2021 06:39 AM
Good point about the tab key. I was thinking more shell prompt that IOS CLI.The below command might do it, but you have to count spaces to figure out which "field" cut thinks that is. That kind of sucks. It is too bad awk isn't available. It treats each instance of white space as a single delimiter.
cut -fX -d' '
Sorry, I wish I had something better to suggest.
04-02-2021 06:21 AM
I don't know if the "tr" command is available, but here is an option to use that to squeeze down multiple spaces.
echo "some test string" | tr -s ' ' | cut -d ' ' -f X
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide