Extracting last parsed segment with the cut command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting last parsed segment with the cut command
# 1  
Old 02-06-2009
Extracting last parsed segment with the cut command

I can see in the man pages how cut can be used to extract a specified string by using the -f qualifier. However, I can't find anywhere how to specify the "last" parsed segment with the cut command. If the string you are parsing is (or can be) differant each time the script is run, but you know that you always want the last segment parsed from the input string, how do you so specify in your command?

THanks
Phudgens
Denver
# 2  
Old 02-06-2009
instead of cut, you could use awk:

Code:
echo "one two three four" | awk '{print $NF}'

gives your "four", and:

Code:
echo "one two three" | awk '{print $NF}'

gives you "three".

The variable 'NF' by itself is the number of fields in the record (line), and if you prefix that with a '$' then that will give you the last field.

hth
# 3  
Old 02-09-2009
Thanks for the help. My final code ended up being:

set CurrentPwd = `pwd`
set CurrentFolder = `echo $CurrentPwd | awk -F'/' '{print $NF}'`


Paul H.
# 4  
Old 02-09-2009
Quote:
Originally Posted by phudgens
Thanks for the help. My final code ended up being:

set CurrentPwd = `pwd`
set CurrentFolder = `echo $CurrentPwd | awk -F'/' '{print $NF}'`


Paul H.
What about parameter expansion:
Code:
set CurrentFolder = ${PWD##*/}

# 5  
Old 02-09-2009
Couldn't get that to work with either upper or lower case "pwd". Got a "Variable syntax" error message.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in extracting data using cut/awk command

Hi Everyone, I have a very simple problem and i am stuck in that from last 8 days. I tried many attempts, googled my query but all in vain. I have a text file named "test.txt" In that suppose i have contents like: Java: 1 Object oriented programming language 2 Concepts of Abstraction... (5 Replies)
Discussion started by: Abhijeet Anand
5 Replies

2. Shell Programming and Scripting

Need help to get the parsed output of "iostat" command

Hi, I have a requirement where parsed output from various linux commands like top, netstat, iostat, etc. will be the input for one javascript with the parsed output from these commands converted to JSON format For "iostat" command, since there are two outputs - one w.r.t CPU utilization and... (2 Replies)
Discussion started by: gopivallabha
2 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Extracting text using cut command

Hi All, I need to extract text 'ULTIMATE and 4spaces after it' from below line 77"2686"2010-11-21 14:09:13.000"4"I"200"1"2010-11-21 14:09:14.000"001:S087110131 ULTIMATE 4.99 T"" I have used cut command : a=`echo $line | cut -f9 -d '"' | cut -c20-31` echo $a But... (5 Replies)
Discussion started by: angie1234
5 Replies

6. Programming

How to get the number of bytes parsed in libxml2

Hi, I am using the libxml2 sax parser to parse a in memory xml string along with validating it against a schema. I am using the following code: xmlSAXHandlerPtr sax_ = new xmlSAXHandler(); sax_->initialized = XML_SAX2_MAGIC; sax_->startElementNs =... (0 Replies)
Discussion started by: Sam Krishna
0 Replies

7. 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

8. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

9. Shell Programming and Scripting

Initializing array using awk from parsed csv

Hi all, I am fairly new to unix scripting and I am struggling with some awk commands here. I want to create an array COMPANYCODE from some parsed csv values (single column) . I have been able to access the data in other ways but I would prefer if I could put each value in it's own array... (6 Replies)
Discussion started by: P8TRO
6 Replies

10. Shell Programming and Scripting

How To Parsed the timings from the unix Script

I want to make one scrip in unix which parsed the timing from the unix mail server for login into the mail server. say for e.g. we are searching something in google.co.in and google tells us that this much time is elapesed to fetch the data. same i want to know that how to find the timing... (2 Replies)
Discussion started by: riddhi_pbr
2 Replies
Login or Register to Ask a Question