How to extract the year from date (mm/dd/yyyy)

9,762

Solution 1

Try sed, like this:

cat file | sed 's/[0-1][0-9]\/[0-3][0-9]\/\([12][0-9][0-9][0-9]\)/\1/g' > newfile

which would read your file called file and write the results to the new file newfile (replace as needed).

If you wish to collect the years you could do something like

cat file | sed 's/[0-1][0-9]\/[0-3][0-9]\/\([12][0-9][0-9][0-9]\)/\1/g' | tr ' ' \\n | grep . | sort -nu

Solution 2

echo "01/01/2001 05/16/1970 06/08/2010" > datefile
sed 's:[0-1][0-9]/[0-3][0-9]/::g' datefile

Result: 2001 1970 2010

This will work both when all dates are on one line and when each date is on its own line. The separator is the : to assist intelligibility. The global g option makes all valid substitutions.

Solution 3

Since the dates are in the US "MM/DD/YYYY" format, one may use GNU date directly on the file dates containing the dates:

$ date -f dates "+%Y"
2001
1970
2010

You can do fancier formatting to, like

$ date -f dates "+%F is in %Y and is a %A"
2001-01-01 is in 2001 and is a Monday
1970-05-16 is in 1970 and is a Saturday
2010-06-08 is in 2010 and is a Tuesday

The formatting sequences (%Y and the like) are described in the GNU date manual.

The input formats handled by GNU date are described in the GNU coreutils manual.

Solution 4

For your example file, you could simply

cut -d / -f3 file

If you want to loop over those dates, pipe into a loop:

cut -d / -f3 file |
while read year; do ...

or just use read and split on a custom IFS value;

while IFS=/ read -r mm dd yyyy; do
    : do something with "$yyyy"
done <file
Share:
9,762

Related videos on Youtube

user455555009
Author by

user455555009

Updated on September 18, 2022

Comments

  • user455555009
    user455555009 over 1 year

    How can I extract the year from a date in the form mm/dd/yyyy?

    I have a text file that has the following dates:

    01/01/2001
    05/16/1970
    06/08/2010
    

    How can I use sed to extract only the year from each one of these dates?

  • Sparhawk
    Sparhawk about 7 years
    No need to spawn cat, just <file sed or sed 's/…' file. Also, you can increase readability by using a different delimiter to /, e.g. sed 's,…,…,g', then you don't need to escape literal /. Finally, what is the purpose of grep?
  • Ned64
    Ned64 about 7 years
    @Sparhawk Yes, you are right, you could use < but that would be less accessible in my opinion (here comes the content | here is the pipe > here goes the result). I would not be a good code golfer. The grep . gobbles empty lines (I do not know the file). sed s,... would work, although the manual says differently. It is a habit, I suppose...
  • Sparhawk
    Sparhawk about 7 years
    Yeah, nothing is big deal really IMO. FWIW, <file can go at the beginning, too, i.e. <file sed 's…' | etc. Also man sed has a section at the end (SEE ALSO) on the "full manual", which leads to info sed > sed scripts > The "s" Command.
  • tripleee
    tripleee about 7 years
    I never stop being baffled that people actually use this wacky date format.
  • Stéphane Chazelas
    Stéphane Chazelas about 7 years
    Quite lucky here that GNU date understands 06/08/2010 as 2010-06-08 and not 2010-08-06 as most of the world would expect.
  • Kusalananda
    Kusalananda about 7 years
    @StéphaneChazelas The dates are in the US format, which GNU date knows how to parse. If the dates are in another format which GNU date cannot parse, a simple rearrangement may be made on the input.