LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple Bash script question for a noob (https://www.linuxquestions.org/questions/linux-newbie-8/simple-bash-script-question-for-a-noob-4175486723/)

demolish50 12-03-2013 12:54 PM

Simple Bash script question for a noob
 
I have a variable that contains: "m;0;1386036965;3;8;135;202;1"

I want to extract out of that a variable that contains the number after the 4th ";". I know this is simple but I'm just lost. In this case the 8.

Thanks

Kustom42 12-03-2013 01:03 PM

Code:


cut -d';' -f4


The above is an easy way to do it, you could look into awk or sed to make it cleaner. -d sets your delimiter and -f selects which field. So the fourth field seperated by a ';'

youngstorm 12-03-2013 01:10 PM

echo "m;0;1386036965;3;8;135;202;1" | cut -d';' -f5

demolish50 12-03-2013 02:12 PM

Quote:

Originally Posted by Kustom42 (Post 5074763)
Code:


cut -d';' -f4


The above is an easy way to do it, you could look into awk or sed to make it cleaner. -d sets your delimiter and -f selects which field. So the fourth field seperated by a ';'

Damn I feel dumb. Thanks!

Sydney 12-03-2013 04:16 PM

Code:

$ awk -F";" '{print $5}' #press enter here
m;0;1386036965;3;8;135;202;1 #paste in the value
8


Kustom42 12-03-2013 04:36 PM

Quote:

Originally Posted by Sydney (Post 5074873)
Code:

$ awk -F";" '{print $5}' #press enter here
m;0;1386036965;3;8;135;202;1 #paste in the value
8


Would probably be better to spend 5 minutes learning this method if its something you are planning to keep using. Awk is MUCH more reliable and allows you to be much more exact via setting field as well as record separators and a bunch of other formatting options.


All times are GMT -5. The time now is 06:23 AM.