Visit Our UNIX and Linux User Community


using cut command right to left


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using cut command right to left
# 1  
Old 09-24-2009
using cut command right to left

Hi guys

I have variable that contains a full directory path.

var=/tmp/a/b/c/d

I want to be able to extract different directories in the path right to left while using / as the delimiter.


so for e.g. if in the above example if I want c then I want it to be in the middle of 1st and second / rather than 4th and 5th.
and if I want to extract do then it would be right of the first / .

If possible please post the full command rather than just suggesting a switch to use.

many thanks
ali
# 2  
Old 09-24-2009
Code:
echo $var | awk -F/ '{print $(NF-2)}'

NF is the right most field, subtract values from it to move to the left. In this case, subtracting 2 would return 'b'
# 3  
Old 09-24-2009
thanks mate. Issue resolved Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #199
Difficulty: Easy
The TIOBE Index for October 2019 proclaimed that Python is becoming the new big language in favor of languages such as PHP and Perl.
True or False?

8 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

2. UNIX for Dummies Questions & Answers

Cut pid from ps using cut command

hay i am trying to get JUST the PID from the ps command. my command line is: ps -ef | grep "mintty" | cut -d' ' -f2 but i get an empty line. i assume that the delimiter is not just one space character, but can't figure out what should i do in order to do that. i know i can use awk or cut... (8 Replies)
Discussion started by: ran ber
8 Replies

3. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies

4. Shell Programming and Scripting

Unix Cut or Awk from 'Right TO Left'

Hello, I want to get the User Name details of a user from a file list. This list can be in the format: FirstName_MiddleName1_LastName_ID FirstName_LastName_ID FirstName_MiddleName1_MiddleName2_LastName_ID What i want it to return is FirstName_MiddleName1_LastName of a user. I... (6 Replies)
Discussion started by: limamichelle
6 Replies

5. Shell Programming and Scripting

cut command help

n2=user1 pts/3 2010-06-29 01 Now i want to split this string with space(' ') character. After splitting output would be: use1 pts/3 2010-06-29 01 I did: nn=${n2} | cut -d ' ' -f2 echo ${nn} It prints nothing. I want the output: pts/3 (2 Replies)
Discussion started by: cola
2 Replies

6. UNIX for Dummies Questions & Answers

Need help with the cut command

Hi, tmp=`stat -c "%a %n" $APPLTMP` printf "\n$tmp\n" t1=`echo $tmp | cut -c 1-3` printf "t1" $t1 tmp has the value 777 /podaai/applcsf/tmp but t1 returns NULL string, what needs to be corrected in this ? (2 Replies)
Discussion started by: happyrain
2 Replies

7. Shell Programming and Scripting

Cut command

Hi, I want to cut from a particular position to a particular position and retain the rest. I tried this cut -c31-51 file1.txt > file2.txt But The characters from the position 31 to 51 were only present in file2.txt. Is there a way to reverse this i.e to retain the rest except from... (1 Reply)
Discussion started by: ragavhere
1 Replies

8. Shell Programming and Scripting

cut command

I need to use the cut command with a variable.I have to select a few characters so I tried cut -c30-42 but how do I join it with variable x. Thanks (1 Reply)
Discussion started by: aries
1 Replies

Featured Tech Videos