LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-20-2004, 08:24 AM   #1
nausicaa3000
LQ Newbie
 
Registered: Feb 2004
Distribution: slackware
Posts: 5

Rep: Reputation: 0
using sed to grab two characters?


I am new to sed, but it seems to me this may be able to do what I want. I need to be able to capture two integers from a variable. I have a variable holding a date and time in the form YYYYMMDDHHMMSS.

For example if I have a variable holding 20040419204537, and I want to grab the hour (20) and place it in a new variable.

Also, if possible, I would like to be able to capture the first 8 characters and put them into a new variable (20040419)

Thanks for any help you can give!
 
Old 04-20-2004, 09:58 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
I'm not any good with sed, but here's a couple of C programs to do it easily:

Code:
gethour.c
int main(int argc, char **argv)
{
  if(argc != 2 || strlen(argv[1]) != 14)
    return 1;
  printf("%c%c\n", argv[1][8], argv[1][9]);
  return 0;
}
Code:
getdate.c
int main(int argc, char **argv)
{
  if(argc != 2 || strlen(argv[1]) != 14)
    return 1;
  argv[1][8] = '\0';
  puts(argv[1]);
  return 0;
}
 
Old 04-20-2004, 10:33 AM   #3
mfeat
Member
 
Registered: Aug 2003
Location: Akron, OH
Distribution: Fedora Core 3
Posts: 185

Rep: Reputation: 30
echo 20040419204537 |
while read a; do
hr=`echo $a | cut -c9-10`
dt=`echo $a | cut -c1-8`
echo $hr $dt
done
 
Old 04-20-2004, 10:37 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Code:
d="20040419204537"
day=`expr substr $d 9 2 `
echo "date=$d  day=$day"
 
Old 04-20-2004, 12:01 PM   #5
nausicaa3000
LQ Newbie
 
Registered: Feb 2004
Distribution: slackware
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you all very much. Such a quick response and good examples. This is a great site!
 
  


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
can sed edit individual characters/portions of a sentence? TGWDNGHN Linux - Software 10 10-18-2005 05:50 AM
sed error message: extra characters after the command. nano_mag Linux - General 3 05-15-2005 02:00 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
Need to grab bookmarks from IE... slackMeUp Linux - Software 3 01-25-2004 06:02 PM
Grab Cert?? joseph_1970 Linux - General 6 12-17-2003 10:39 AM


All times are GMT -5. The time now is 05:35 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