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 08-06-2012, 07: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 07:27 AM. Reason: visual
 
Old 08-06-2012, 07:54 AM   #2
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
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, 11:23 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,028

Rep: Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200Reputation: 3200
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, 08:06 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,417

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

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

All times are GMT -5. The time now is 10:39 PM.

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
Open Source Consulting | Domain Registration