Trying to get the year out of a date with sed [duplicate] - sed

This question already has answers here:
Why doesn't `\d` work in regular expressions in sed? [duplicate]
(3 answers)
Closed 5 years ago.
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?

Use the date command:
string="1234-1-12"
date -d "${string}" +%Y
Output:
1234

Simple grep approach:
echo "1234-1-12" | grep -o '^[^-]*'
The output:
1234

Related

Sed with variables [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 1 year ago.
i have a config file with this format
http://link:port/username/password/1234
Im using this code to replace the Username1/Password1 with a different Username2/Password2
sed -i -e 's/\/Username1\/Password1/\/Username2\/Password2/g' /etc/config.cfg
Now i want to make this something like
$UsernameOLD = Username1
$PasswordOLD = Password1
$UsernameNEW = Username2
$PasswordNEW = Password2
sed -i -e 's/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/g' /etc/config.cfg
Could anyone help me getting this ready ?
I found the solution i had to use double quotes instead of '
The working command looks like this:
UsernameOLD=MyOldUsername
PasswordOLD=MyOldPassword
UsernameNEW=MyNewUsername
PasswordNEW=MyOldUsername
sed -i -e "s/\/$UsernameOLD\/$PasswordOLD/\/$UsernameNEW\/$PasswordNEW/" /etc/config.cfg

Using sed with minus sign [duplicate]

This question already has answers here:
Replacing string in linux using sed/awk based
(3 answers)
Closed 2 years ago.
I want to find all instances of #- "${COMPOSER_CONFIG} and replace with - "${COMPOSER_CONFIG} within a file. (Note, the removal of #- )
The command I have come up with is:
sed -i '/#- "${COMPOSER_CONFIG}/- "${COMPOSER_CONFIG}' docker-compose.yml
But I get the error:
sed: -e expression #1, char 25: unknown command: `-'
It appears the minus sign is causing the issue. After researching this I am unable to identify why this is the case.
Any insight greatly welcome.
Try this
sed -i '/COMPOSER_CONFIG/s/#//' docker-compose.yml

sed not replacing the second occurance [duplicate]

This question already has answers here:
How to replace only on second matching row using sed
(4 answers)
Closed 4 years ago.
File: test
cat cat
dog cat
dog puppy
dog cat
Command:
sed -i 's/dog/big_dog/2' test
According to the explanation here: http://www.grymoire.com/Unix/Sed.html#uh-8 the output of the code should have been:
cat cat
dog cat
big_dog puppy
dog cat
But the file remains unchanged for me.
What am I doing wrong?
Looks like awk has a solution:
awk '/dog/{c++;if(c==2){sub("dog","big_dog");c=0}}1' test
Source: https://www.linuxquestions.org/questions/programming-9/replace-2nd-occurrence-of-a-string-in-a-file-sed-or-awk-800171/

sed: Why does a file being edited in-place have modified lines doubled? [duplicate]

This question already has answers here:
sed is printing a substituted line twice
(2 answers)
Closed 6 years ago.
The perils of working tired:
sed -i 's/foo/barbazqux/p' example.txt
This made the output file have two copies of any modified line.
Turns out I hadn't removed the p flag from the end of the command. I'd been doing this when testing it on the command line.
Correct command:
sed -i 's/foo/barbazqux/' example.txt

Sed (POSIX) coming from linux [duplicate]

This question already has answers here:
How to remove every other line with sed?
(5 answers)
Closed 8 years ago.
I am somewhat new to posix, and i can't use: sed '1~2p'
My goal is to skip every one line from line 1:
1
2
3
4
would become
1
3
I was wondering what is the posix equivalent of ~.
Code for sed:
sed -e n -e d file
or:
sed -e 'n;d' file
The simpler, portable solution would be:
awk 'NR%2' file
bash solution:
while read -r line; do
[ $((i++ % 2)) -eq 0 ] && echo "$line";
done < file