LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple "CUT" Query (https://www.linuxquestions.org/questions/programming-9/simple-cut-query-663777/)

paragkalra 08-19-2008 03:47 AM

Simple "CUT" Query
 
Hello All,

I have a simple Cut query. I want to extract 17195 from the following and store it in a variable.

Quote:

ABCD PQRS LMNO XYZW (2.3.17195)
Would someone like to enlighten me.

matthewg42 08-19-2008 04:19 AM

If you have this text in a variable, and are using bash or a similar shell, you can do it without needing to use cut or sed - this will be more efficient as spawning a new process is much more expensive than manipulating strings inside the shell itself.
Code:

s="ABCD PQRS LMNO XYZW (2.3.17195)"
s=${s##*.}
echo "I have: ${s%)}"

will output:
Code:

I have: 17195
See the "Parameter Expansion" section of the bash manual page for an explanation and other, similar methods.

paragkalra 08-19-2008 12:43 PM

This was what I was looking for:
Quote:

cat /tmp/test.txt | cut -f 3 -d "." | cut -c 1-5
Anyways...thanks matthewg42

Hko 08-19-2008 01:30 PM

..but matthewg42's is faster and simpler code.


All times are GMT -5. The time now is 07:47 PM.