cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2383
Views
5
Helpful
10
Replies

How to use whitespace as a delimiter in IOS Shell command 'cut'?

schlesix
Level 1
Level 1

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

10 Replies 10

I haven't tried it in the IOS shell but this would do it at a Unix shell prompt.

cut -f1 -d' '

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

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

No, awk ist not available. It's a very limited selection of Unix tools.

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

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

Instead of the space in the "hello there", make that a tab. Then I bet you will get the right output.

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.

 

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.

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