6

Trying to get the year out of a date with sed

 3 years ago
source link: https://www.codesd.com/item/trying-to-get-the-year-out-of-a-date-with-sed.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Trying to get the year out of a date with sed

advertisements

This question already has an answer here:

  • Why doesn't `\d` work in regular expressions in sed? 3 answers

I'm using the following command to get the year out of a string using sed.

echo 1234-1-12 | sed -r 's/(\d{4})-\d{1,2}-\d{1,2}/\1/'

but somehow it returns the entire date instead of the year.

getting 1234-1-12

expecting 1234

Any ideas why it doesn't work?


Simple grep approach:

echo "1234-1-12" | grep -o '^[^-]*'

The output:

Related Articles

Get the year from giving a date string

I have this date format Jan 1, 2015 how can i get the year "2015" from the string "Jan 1, 2015"? so far I tried this var str = "Jan 1, 2015"; var res = str.substr(1, 7); but unfortunately and sadly not working. Any help, idea

Oracle SQL: Get the last eight months of data (with YEAR and MONTH columns)

I have a table that has columns YEAR and MONTH, which are varchars, which have a format like MONTH = '02', YEAR = '2011'. What query can I use to get the last eight months of data, excluding the current month?Try where to_date(year || month, 'YYYYMM'

Why should I get the year 2555 when formatting DateTime with the current time?

I have C# code that does the following: DateTime now = DateTime.UtcNow; string timeToOutput = now.ToString(); String pathInStorage = now.ToString("yyyy-MM-dd/HH/"); CloudBlobClient client = getClient(); CloudBlobContainer container = client.GetC

can not get the right out of a group & ldquo; by & quot;

Trying to get the list of product Id's with amount of feedbacks each of them has Need to count only feedbacks that were given to the product as belonging to specific category (see the SQL script: categoryId == 50). Product can belong to multiple cate

jQuery: get the value of different selection labels with the same name

I was trying to get the value from different select tag with the same name using jQuery. The following is the HTML <div class="row"> <div class="col-md-3"> <h1><i class="fa fa-glass" aria-hidden="tru

get the facebook page flow like json with jquery

I have been trying to get facebook page feed from json data with jquery. so far I have been able to get page details with my method but when I try to get page's feed it gives me an error and in the console it says "syntax error: invalid label".

How to get the time difference in two dates in SQL Server

I am trying to get the time difference between two dates by query with this code: DECLARE @firstDate DATETIME DECLARE @SecondDate DATETIME SELECT CONVERT(VARCHAR(5), DATEDIFF(dd, @firstDate, @SecondDate)) + ' Days ' + CONVERT(VARCHAR(5), (DATEDIFF(hh

Get the DateTime field from the table and get the year, month, and day on C #

I have a SQL Server table that has a DateTime field called ProductionDate. The format of this field is: 2015-06-29 00:00:00.000 I have a C# WinForm application that selects this date with this: string productionDate = String.Empty; var prodDate = pal

Get the year off timestamp sql

I have a problem extracting the year out of a mysql timestamp field. I managed to get it work with a datetime field using this method: SELECT id FROM TABLE WHERE YEAR(creation_date) = 2010 CREATE TABLE IF NOT EXISTS `pub_media` ( `id` int(11) NOT NUL

I have the hardest time trying to get the IPN PayPal subscription ID

I'm having the most difficult time trying to get the PayPal IPN subscription Id. This is my IPN page. <?php // read the post from PayPal system and add 'cmd' $req = 'cmd=_notify-validate'; foreach ($_POST as $key => $value) { $value = urlencode(stri

I'm trying to get the total number of a column containing 'New' and 'Used' from a csv file using php

I am trying to get the total count for new and used cars from the column "type" in a csv file. I output the data fine but cant seem to figure out out to split up the new and used count.. Here is what im using - // Set path to CSV file $csvFile =

I'm trying to get the latest version of my code on git

I'm trying to get the last version of the master code on my local machine without deleting any current work that's being done in the later part of the tree. How do I accomplish this in git? I want my local machine to have the code at the 'new items'

Get the year and month of the postman

I have a dataset with a date as a factor I tried using the lubridate package to extract the year and the month in order to create a new column in my data.frame, but it doesn't work. #load packages library(lubridate) #Create Dataset Data <- read.csv(&qu

When trying to get the display size of an image in an ImageView

I'm trying to get the real size of an image displayed in an image view. Actually my image is larger than the screen and the imageview is resizing the image to diplay it. I'm looking for this new size. I've tried to override the onDraw method of the I

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK