Solved

Need help reading ksh script

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now