Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
cancel
Showing results for 
Search instead for 
Did you mean: 

use of cut in command

SOLVED
Go to solution
Gesmundo
Advisor

use of cut in command

Can anybody see the issue?
I have the following commands:
$ x3=$(grep -i isend x.x | tr -s " " )
$ echo $x3
isEndOfStream = false
$ x3=$(grep -i isend x.x | tr -s " " | cut -d "=" -f 2 )
$ echo $x3
false
$ x3=$(grep -i isend x.x | tr -s " " | cut -d "=" -f 2 | cut -c1-5)
$ echo $x3
fals


Where is 'e'?

But if I further do this:
$ x4=$(echo $x3)
$ echo $x4
false
$ x4=$(echo $x3 | cut -c1-5)
$ echo $x4
false


I got the whole value (false).
Thanks for any explanation and workaround.

Noel

3 REPLIES
Steven Schweda
Honored Contributor
Solution

Re: use of cut in command

> Where is 'e'?

   Your "cut -c1-5" command cut it off from " false".

pro3$ x3='isEndOfStream = false'
pro3$ echo "$x3" | cut -d "=" -f 2
 false

(Note: " false" != "false".)

pro3$ x='   xxx   '

pro3$ echo $x
xxx

pro3$ echo "$x"
   xxx   

   Guess which one works more like what happens in your pipeline.

   As for a work-around, it depends on how you'd like to get rid of that
extra space.  As usual, many things are possible (once you see the extra
space).

Gesmundo
Advisor

Re: use of cut in command

Thanks Steven for the reply.

I also found out that there is an unprintable character (CR - ASCII 13) at the end of "false" in the file so I still need to use cut to get only the first 5 characters.

$ x=$(grep -i end x.x | tr -s " " | cut -d "=" -f 2)                               
$ echo $x                                                                          
false
$ expr length $x
6
$ x1=$(echo $x | cut -c1-5)
$ echo $x1
false

$ expr length $x1
5

 

 

 

Steven Schweda
Honored Contributor

Re: use of cut in command

> I also found out that there is an unprintable character (CR - ASCII
> 13) at the end of "false" in the file so I still need to use cut to get
> only the first 5 characters.

   You're already using "tr", so it'd be easy to have it translate a CR
("\r") into something more harmless, like, say, a space.  And, of
course, "sed" can destroy spaces with ease:

mba$ x='   xxx   '
mba$ echo "$x"
   xxx   
mba$ echo "$x" | sed -e 's/ //g'
xxx

   If you really want exactly five characters, then "cut" is fine, but
if you want to trim off spaces and other junk, then counting characters
may not be the best way.