Stripping date fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stripping date fields
# 1  
Old 11-01-2003
Stripping date fields

How would I strip a date field? For example, if I have
TODAY defined as the following: TODAY=`date +"%m/%d/%y"`,
TODAY is assigned the value 11/01/03 on Nov 1, 2003
How would I strip the first 2 bytes? (11)?

Thank you in advance for the help.
# 2  
Old 11-01-2003
You might want to use the cut command.

Last edited by Simerian; 11-01-2003 at 01:06 PM..
# 3  
Old 11-01-2003
You didnt mention what you wanted to use the first two chars for but if you are using Korn shell then you could use one of the builtins (which should run faster than the cut command) and set a variable equal to the first two chars. For example:

${parameter%%pattern} which deletes the longest suffix that matches the pattern. You can modify this construct to set a variable to the shortest suffix of the parameter by removing one of the percent signs: ${parameter%pattern}. Finally, you can play the same game with prefixes by using the pound sign: ${parameter##pattern} or ${parameter#pattern}

Last edited by google; 11-01-2003 at 11:05 AM..
# 4  
Old 11-01-2003
google is correct in that cut is very slow and should be used sparingly in circumstances where extracts need to occur many times. Much better to use a data processing tool such as awk for mass data extraction and manipulation.

However, I would suggest that you need to get to grips with the fundamentals of UNIX (i.e. commands such as cut) before you try to experiment with more advanced features.

To get the most out of parameter substitution as suggested by google, it is necessary to understand the principles of regular expressions (often called regexp) which underpin many of the best attributes of UNIX commands (e.g. egrep and awk)
# 5  
Old 11-03-2003
Another way: use typeset to define attributes of a variable, eg left-justified string width=2...
Code:
typeset -L2 MM
MM=$TODAY

# 6  
Old 11-03-2003
Thanks for all who replied. Using the "typeset" command seemed the simplest way to me. I used it, and it worked. For those who knows the system so well, thank you so much for sharing your knowledge. This is a great help for people like me, who are just getting familiar with Unix. I hope in the years to come, I would be able to return the favor for somebody else.

Thanks again.
Latha
# 7  
Old 11-03-2003
Google wanted to know what I was using this for. We have monthend procedures, which creates reports, logs, etc. I wanted to name these files with the appropriate month code. That is where the month code came in.

Latha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using awk to get folder name and date fields with heading

It is on the HP-Unix Output desired: Folder Name Date Folder1 Mar 18 10:00 Folder2 Mar 17 12:23 Tried with ls -ldt wp411* | awk '{print $9,"\t",$6," ",$7," "$8}' Please help me with the headings (3 Replies)
Discussion started by: Siva SQL
3 Replies

2. Shell Programming and Scripting

Find fields based on date criterion

Hi All , I am trying to find some non empty files from a directory based on below conditions : Files:: SIZE DATE FILE 3679 Jan 25 23:59 belk_rpo_error_po9324892_01252014.log 0 Jul 01 06:30 belk_rpo_error_po9324267_07012014.log 0 Jul 20 05:50... (7 Replies)
Discussion started by: LoneRanger
7 Replies

3. UNIX for Dummies Questions & Answers

Sorting on fields for last date

Hi all, I have a file with a list of rpm's that have different dates. I am trying to just grab the latest rpm and install date, and discard the rest. The file has 1000's of entries all with different names and dates. I have tried sort -k on the file and I am not grabbing the info, ... (4 Replies)
Discussion started by: gartie
4 Replies

4. UNIX for Advanced & Expert Users

Sort in fields date and columns.

Hi colleagues, I have this output, i need sort for 7th and 8th columns, the column 7th is date mm/dd/yyyy format and column 8th is time hh:mm:ss.number PRUEBA 1263 0007 1 0 7131292 03/25/2013 16:43:04.159976 PROCESS PRUEBA1 666 0146 1 0 11600064 ... (1 Reply)
Discussion started by: systemoper
1 Replies

5. Shell Programming and Scripting

Date conversion in row while preserving rest of fields

Hi Forum. I searched the forum for a solution but could not find an exact one to my problem. I have some records in the file where I would like to convert the last date field to another format while preserving the rest of the other fields. For example: Found:... (6 Replies)
Discussion started by: pchang
6 Replies

6. Shell Programming and Scripting

How to remove '-' for date fields alone in a file

Hi, I have a scenario like, I need to replace a hyphens(-) for date field alone in the file. I have minus(-) sign for other fields it should remain as such, only for date fields hyphens must be removed. Please find the content for the file below: sample.txt: -----------... (4 Replies)
Discussion started by: G.K.K
4 Replies

7. UNIX for Dummies Questions & Answers

Stripping down binaries

Hello, I am the CEO of Grand Tech Corporation. We are launching Linux NT and forgive me, but I do not know how to strip binaries down in Mandriva Linux. Can someone tell me a way to?:b: (2 Replies)
Discussion started by: Linux NT
2 Replies

8. Shell Programming and Scripting

stripping a variable down

I have a variable that has an absolute path for a file on my computer. This dynamically changes. Is there a way I can assign two new variables from that one? variable: /Users/keith/Desktop/test/file.mov 1) filename - no path or extention ....so just....file 2) path no filename or... (3 Replies)
Discussion started by: mainegate
3 Replies

9. Shell Programming and Scripting

stripping out certain charecters

we are ftping zipped up files from the development server to the production server daily.The files are in this format filename.dat.20061231.12131.gz I have to unzip the file (i can do that with gunzip) and then strip out the timestamp after the .dat extension. I can do something like this ... (4 Replies)
Discussion started by: mervin2006
4 Replies

10. Shell Programming and Scripting

getting the date fields in Perl

I get the perl date in the following format: Mon Jan 31 06:01:37 2005 How can I extract each field in a single line using a regular expression?? I dont want to use "split". Any other way out?? JP :) (2 Replies)
Discussion started by: jyotipg
2 Replies
Login or Register to Ask a Question