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
Solved

Need help reading ksh script

Posted on 2013-06-10
5
158 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…

829 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