Using sed to capture the year


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to capture the year
# 1  
Old 11-06-2015
Using sed to capture the year

Hi,

I'm not real familiar with sed, but I need to capture the year this directory path. Here's what I have so far. I'm able to capture the run name, but I want the only the year that's within the run name. How can I do this?
Code:
TSP_FILEPATH_PLUGIN_DIR=/Home/Auto_user_124-451-12October2015_WH15-0186_YN_531_504/plugin_out/variantCaller_out.981/
RUN=`echo ${TSP_FILEPATH_PLUGIN_DIR} | sed -e 's#.*/Home/\(.*\)/plugin_out/.*#\1#g'`;

Thanks
-J
# 2  
Old 11-06-2015
Look for 20 followed by two more digits appears to work in your example.
# 3  
Old 11-06-2015
Code:
year=`echo "${TSP_FILEPATH_PLUGIN_DIR}" | sed -n -e 's/.*\(20[0-9][0-9]\).*/\1/p'`

This very probabilistic. Can we get more concrete data on the format of the filename?
# 4  
Old 11-06-2015
What operating system and shell are you using?

If you're using a shell that performs parameter expansions required by the POSIX standards, there are ways to do this entirely in the shell without invoking sed twice. For example:
Code:
#!/bin/ksh
TSP_FILEPATH_PLUGIN_DIR=/Home/Auto_user_124-451-12October2015_WH15-0186_YN_531_504/plugin_out/variantCaller_out.981/
RUN=${TSP_FILEPATH_PLUGIN_DIR##*/Home/}
RUN=${RUN%%/*}
YEAR=${RUN##*[yhletr]}
YEAR=${YEAR%%_*}
printf '%s="%s"\n' RUN "$RUN" YEAR "$YEAR"

which produces the output:
Code:
RUN="Auto_user_124-451-12October2015_WH15-0186_YN_531_504"
YEAR="2015"

If it isn't obvious, the match against the letters "y", "h", "l", "e", "t", and "r" is a search for the last letter in an English month name. You could also use:
Code:
YEAR=${RUN##*[[:lower:]]}

to get the same results. Of the two, I chose the one requiring less typing.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Last login with year

'last' command gives me date and time the user has logged in last. But how to get the date with year? (8 Replies)
Discussion started by: Soham
8 Replies

2. Shell Programming and Scripting

Sed capture help

Hi, I'm a newbie to sed and learning about sed 1 liners and captures. I was wondering if someone could help me with the code below: http://i51.tinypic.com/2eg4kdd.png So that's taking a URL and splitting it into URL,TLD & Filename. Any help on how I can do this is much appreciated. ... (6 Replies)
Discussion started by: r4v3n
6 Replies

3. Shell Programming and Scripting

sed/awk to retrieve max year in column

I am trying to retrieve that max 'year' in a text file that is delimited by tilde (~). It is the second column and the values may be in Char format (double quoted) and have duplicate values. Please help. (4 Replies)
Discussion started by: CKT_newbie88
4 Replies

4. Emergency UNIX and Linux Support

LAST command with Year

Hi Guys, I'm trying to identify the last logins by all the users in the system in AIX. the last command gives me the output, but there is no year displayed for it . Since there is a duplication of months i mean Apr 2010 and Apr 2009 also its giving me inaccurate data.. Is there a way I can filter... (4 Replies)
Discussion started by: kkeng808
4 Replies

5. What is on Your Mind?

How Much Vacation Do You Take Every Year?

Please vote and comment: How much vacation from work do you take every year? (40 Replies)
Discussion started by: Neo
40 Replies

6. Shell Programming and Scripting

use sed to remove year from string?

Hi guys, I have been trying to play with sed to accomplish this but I just can't quite get it right. I need to be able to remove the year from a string held in a variable in my bash script. The string may have multiple words but always ends with a year such as (2009) for example: ... (2 Replies)
Discussion started by: tret
2 Replies

7. Shell Programming and Scripting

How do i get the year of the file

When i use, 'ls -ltr' I only see the month and day (timestamp) of the file. How do i see the year also. Thanks and Regards, Ram (1 Reply)
Discussion started by: ramky79
1 Replies

8. Shell Programming and Scripting

How many weeks in a year

Hi, I search how i could do to find if a year (for example 2004, 1989, 2058) has 52 or 53 weeks... Have you a idea for me please??? (1 Reply)
Discussion started by: Castelior
1 Replies
Login or Register to Ask a Question