LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 08-06-2012, 06:26 AM   #1
StrongIce
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: Disabled
Parse string using bash


Hi,

Linux newbie here trying to create a simple bash verification script.
I need help to parse this tring and put the date in a variable.
This line is from the 3rd line of a file.

VARIABLE_DATE 080612 08/06/2012 14:13:10

There are more spaces in between the values but I just need the
date 080612 so I can confirm it with the current system date
VDATE=`date '+%m%d%y'`.

Would greatly appreciate if someone could help.

Last edited by StrongIce; 08-06-2012 at 06:27 AM. Reason: visual
 
Old 08-06-2012, 06:54 AM   #2
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
Quote:
Originally Posted by StrongIce View Post
Hi,

Linux newbie here trying to create a simple bash verification script.
I need help to parse this tring and put the date in a variable.
This line is from the 3rd line of a file.

VARIABLE_DATE 080612 08/06/2012 14:13:10

There are more spaces in between the values but I just need the
date 080612 so I can confirm it with the current system date
VDATE=`date '+%m%d%y'`.

Would greatly appreciate if someone could help.

Hi, something like the following could work for you to only get the date which in your case is '080612'.

Code:
# TEST='VARIABLE_DATE      080612         08/06/2012        14:13:10'
# echo ${TEST} | grep -Eo '[[:space:]]+[0-9]{6}[[:space:]]' | grep -Eo [0-9]{6}
080612
it will get the date only no matter how many spaces are there in between the values.

hope that helps
 
Old 08-06-2012, 10:23 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,502

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Well as you asked for bash, here are 2 ways:
Code:
str='VARIABLE_DATE 080612 08/06/2012 14:13:10'

regex='^[^ ]* ([^ ]*)'

[[ $str =~ $regex ]] && echo ${BASH_REMATCH[1]}

str2=${str#* }
str2=${str2%% *}

echo $str2
 
Old 08-06-2012, 07:06 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,267

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Depends what you mean by 'using bash'; strictly bash fns only, or are you allowed to use/call eg awk ?
Code:
d=$(echo "VARIABLE_DATE   080612   08/06/2012 14:13:10 "|awk '{print $2}')
echo $d
080612
 
Old 08-07-2012, 03:46 AM   #5
StrongIce
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you all for the suggestions. I will try them all out and let you know the results though I know that
all of them should be working fine!
 
  


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
Parse String in a Bash script jimwelc Linux - Newbie 8 11-09-2012 07:47 AM
Bash script: Parse folders in path string miceagol Programming 3 03-25-2008 06:31 PM
parse a string in perl 2007fld Programming 13 08-07-2007 02:41 PM
c++ parse date string?? blizunt7 Programming 25 05-18-2006 03:12 PM
Parse RPM version string in Bash jimwelc Linux - Newbie 1 02-28-2005 05:22 PM


All times are GMT -5. The time now is 11:05 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration