Solved

Need help reading ksh script

Posted on 2013-06-10
5
163 Views
Last Modified: 2013-06-11
Can someone tell me what does this line do?

cut -d"/" -f1-4`

I understand that it grabs the string, ignored the forward slash character and cus characters 1 throught 4 from the string. Am I correct?

Also what is the difference when cut command is called with -c1-4 as opposed to -f1-4?
0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 39235078
man cut
...
     -c list
             The list specifies character positions.

     -d delim
             Use delim as the field delimiter character instead of the tab character.

     -f list
             The list specifies fields, separated in the input by the field delimiter character (see the -d option.)  Output fields are separated by a single occurrence of the field delimiter
             character.

so
     
cut -d"/" -f1-4
prints everything up to   the 4th / on each line
0
 
LVL 35

Author Comment

by:YZlat
ID: 39235094
but what does it do to the / character?
0
 
LVL 84

Expert Comment

by:ozo
ID: 39235119
It uses it as a field delimiter to determine what the first 4 fields are
0
 
LVL 35

Author Comment

by:YZlat
ID: 39235132
But what about the difference bewteen -f1-4 and -c1-4?
0
 
LVL 23

Assisted Solution

by:nemws1
nemws1 earned 250 total points
ID: 39235183
If we have this string as input:

Hello/there/my/friend/how/are/you/?

And we run this

cut -d"/" -f1-4

The output is:

Hello/there/my/friend

If we run this:

cut -d"/" -c1-4

We get an error.  Specifying *both* -d and -c doesn't make sense, as '-c' already uses a null character as a delimiter (meaning the thing between each individual character, which is actually nothing).  We can do this however:

cut -c1-4

And with our input, we'll get:

Hell

(I perhaps should have chosen a better string!)
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question