LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2013, 01:54 PM   #1
demolish50
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Rep: Reputation: Disabled
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
 
Old 12-03-2013, 02:03 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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 ';'
 
1 members found this post helpful.
Old 12-03-2013, 02:10 PM   #3
youngstorm
Member
 
Registered: May 2003
Location: USA
Distribution: Fedora 21, RHEL 5,6&7, Windoze 7
Posts: 235

Rep: Reputation: 31
echo "m;0;1386036965;3;8;135;202;1" | cut -d';' -f5
 
1 members found this post helpful.
Old 12-03-2013, 03:12 PM   #4
demolish50
LQ Newbie
 
Registered: Dec 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Kustom42 View Post
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!
 
Old 12-03-2013, 05:16 PM   #5
Sydney
Member
 
Registered: Mar 2012
Distribution: Scientific Linux
Posts: 147

Rep: Reputation: 36
Code:
$ awk -F";" '{print $5}' #press enter here
m;0;1386036965;3;8;135;202;1 #paste in the value
8
 
Old 12-03-2013, 05:36 PM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Quote:
Originally Posted by Sydney View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple variable bash script help/question lilrazzzzz Programming 4 04-29-2009 06:50 PM
simple noob question (samba) Slimster Ubuntu 4 10-29-2005 08:16 AM
simple noob question (samba) Slimster Debian 3 10-03-2005 10:53 AM
noob bash script question babag Programming 9 04-30-2005 03:25 AM
simple bash script question bullium Programming 3 11-01-2004 03:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration