In TextMate find two lines and replace with the first? - textmate

In Textmate (or with sed), how can I find two lines that start with "Date:" and replace both lines with the first? Here's the search expression I used in TextMate that works:
Date+.+?$\n^Date:+.+?$
Example text:
blah
blah blah
Date: Fri, 9 Jan 2009 16:20:59 -0800 (PST)
Date: 2009-01-09 16:20:59 -0800 (Fri, 09 Jan 2009)
blah
turn into:
blah
blah blah
Date: Fri, 9 Jan 2009 16:20:59 -0800 (PST)
blah

Capture the first date using parentheses:
(Date+.+?)$\n^Date:+.+?$
And then use this as your replacement field:
$1
($1 just means to use the text captured in the first set of parentheses in the find string.)

Related

How can I change command and option about 'date' command in bash?

I want to convert this bash command to shell script.
BASH
Input:
date --date="Wed Aug 25 22:37:44 +0900 2021" +"%s"
Output:
1629898664
SHELL
tmp.sh:
function time(a, b, c, d, e) { return date --date="a b c d +0900 e" +"%s" }
{print time($1, $2, $3, $4, $5}
timeline:
Wed Aug 25 22:37:44 2021
Command:
awk -f tmp.sh timeline
Output:
awk: tmp.sh:1: function cvtTime(w) { return date --date="Thu May 14 23:40:52 +0900 2020" +"%s" }
awk: tmp.sh:1: ^ syntax error
What about timeline file has multiple lines? Like:
Wed Aug 25 22:37:44 2021 JACK
Wed Aug 26 22:37:44 2021 EMILY
Wed Aug 27 22:37:44 2021 SAM
I tried:
#!/bin/bash
while read -r line; do
date --date="${1} ${2} ${3} ${4} +0900 ${5}" +"%s"
done
Want:
1629898664 JACK
1629985064 EMILY
1630071464 SAM
But it doesn't work :(
It seems that you want a shell script that is invoked with five command line parameters:
A weekday (in a three-letter format)
A month (in a three-letter format)
Day-of-month
A time expression (HH:MM:SS)
A year (four digits)
(Note that 1. is redundant, it is implied by 2., 3., and 5.)
Hence a somewhat minimal shell script would look sth. like this:
#!/bin/bash
date --date="${1} ${2} ${3} ${4} +0900 ${5}" +"%s"
Of course, this can be greatly improved, e.g., by adding sanity checks for the passed parameters.
In case you want to store the date information in a file so that you can pass a single filename parameter to the script instead (allowing for multiple such lines), the following variation will do:
#!/bin/bash
while read -a i; do
echo $(date --date="${i[0]} ${i[1]} ${i[2]} ${i[3]} +0900 ${i[4]}" +"%s") ${i[5]}
done < ${1}
Note, however, that this version expects an additional name parameter after the date information in each line.
In any event, no need for awk here.

Convert GMT with offset string to MST in bash Shell script

I have a String variable (datetime with GMT offset) in the following format.
How can I convert this to a MST in bash shell script?
Input GMT :- 08/Sep/2020:11:38:01 +0000
Output MST :- 08/Sep/2020:04:38:01 -0700
We can get the offset like this
offset=$(date +%-z)
I dont want to again convert into date from string and then use offset and minus offset to reach MST.Is there a way to convert it in a better way?
Convert 08/Sep/2020:11:38:01 +0000 with bash to 08 Sep 2020 11:38:01 +0000:
gmt="08/Sep/2020:11:38:01 +0000"
gmt="${gmt//\// }" # replace all / with spaces
gmt="${gmt/:/ }" # replace first : with space
Then use it with GNU date:
TZ="MST" date --date="$gmt" +'%d/%h/%Y:%H:%M:%S %z'
Output:
08/Sep/2020:04:38:01 -0700

Email.date formatting

When using email.date.to_s
I receive a date in this format
Wed, 3 Jun 2015 14:57:46 -0700
but I want it to be in this format
06/03/15
You can use strftime to format the string:
Time.now.strftime("%m/%d/%y")
#=> 09/16/15

Using sed to extract a substring in curly brackets

I've currently got a string as below:
integration#{Wed Nov 19 14:17:32 2014} branch: thebranch
This is contained in a file, and I parse the string. However I want the value between the brackets {Wed Nov 19 14:17:32 2014}
I have zero experience with Sed, and to be honest I find it a little cryptic.
So far I've managed to use the following command, however the output is still the entire string.
What am I doing wrong?
sed -e 's/[^/{]*"\([^/}]*\).*/\1/'
To get the values which was between {, }
$ sed 's/^[^{]*{\([^{}]*\)}.*/\1/' file
Wed Nov 19 14:17:32 2014
This is very simple to do with awk, not complicate regex.
awk -F"{|}" '{print $2}' file
Wed Nov 19 14:17:32 2014
It sets the field separator to { or }, then your data will be in the second field.
FS could be set like this to:
awk -F"[{}]" '{print $2}' file
To see all field:
awk -F"{|}" '{print "field#1="$1"\nfield#2="$2"\nfield#3="$3}' file
field#1=integration#
field#2=Wed Nov 19 14:17:32 2014
field#3= branch: thebranch
This might work
sed -e 's/[^{]*\({[^}]*}\).*/\1/g'
Test
$ echo "integration#{Wed Nov 19 14:17:32 2014} branch: thebranch" | sed -e 's/[^{]*{\([^}]*\)}.*/\1/g'
Wed Nov 19 14:17:32 2014
Regex
[^{]* Matches anything other than the {, That is integration#
([^}]*) Capture group 1
\{ Matches {
[^}]* matches anything other than }, That is Wed Nov 19 14:17:32 2014
\} matches a }
.* matches the rest
Simply, below command also get the data...
echo "integration#{Wed Nov 19 14:17:32 2014} branch: thebranch" | sed 's/.*{\(.*\)}.*/\1/g'

Create a valid rss date

I want to convert a date like
Sat Dec 31 22:42:58 CET 2011
to a valid rss date which normaly looks like
Mon, 06 Sep 2009 16:45:00 +0000
Seconds should always be 00. Is there anyway to do so? Besides it would be could to find out the current as a valid rss but that's optional ;)
Update
With your new sample input, you don't even need to reformat. It's as simple as
$ time="Sat Dec 31 22:42:58 CET 2011"; date -Rd "$time"
Sat, 31 Dec 2011 13:42:58 -0800
The trick is to reformat your date into a string that date can understand and take as input. In this case it is 2011-12-31 13:37. I'm using awk to do this, but there are number of different utilities that will suffice.
#!/bin/bash
time="12-31-11 13:37"
date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Output
$ time="12-31-11 13:37"; date -Rd "$(awk -F'[- ]' '{printf("20%s-%s-%s %s\n", $3,$1,$2,$4)}' <<<"$time")"
Sat, 31 Dec 2011 13:37:00 -0800

Resources