Fairly new to sed. I am trying to write a sed command that converts dates to the reverse, but not if they're part of other words.
So far I have:
sed 's/[0-9]\{1\}/[0-9]\{1\}/[0-9]\{4\}/SUBSTITUTE/g'
Trying to figure out the substitute part. Thank you!
You need to use word boundaries.
sed 's~\b\([0-9]\{2\}\)/\([0-9]\{2\}\)/\([0-9]\{4\}\)\b~\3/\2/\1~g' file
Example:
$ echo '04/13/1991hello' | sed 's~\b\([0-9]\{2\}\)/\([0-9]\{2\}\)/\([0-9]\{4\}\)\b~\3/\1/\2~g'
04/13/1991hello
$ echo '02/03/2001' | sed 's~\b\([0-9]\{2\}\)/\([0-9]\{2\}\)/\([0-9]\{4\}\)\b~\3/\2/\1~g'
2001/03/02
Related
I a file containing the genome ids following NZ_FLAT01000030.1_173 I need to manipulate those ids like this one: NZ_FLAT01000030.1
I tried some but didn't give me the exact thing.
sed 's/_/\t/' output : NZ FLAT01000030.1_173
sed -r 's/_//' output: NZFLAT01000030.1_173
sed -r 's/_//g' output: NZFLAT01000030.1173
How can I do that by using sed command?
Are you trying to remove the undesrscore and the digits following it?
echo 'NZ_FLAT01000030.1_173' | sed -E 's/_[0-9]+//g'
NZ_FLAT01000030.1
$ echo 'NZ_FLAT01000030.1_173' | sed 's/_[^_]*$//'
NZ_FLAT01000030.1
I am trying to filter a long html page, for leaving only fingerprints which have a consistent structure. for example:
DCD0 5B71 EAB9 4199 527F 44AC DB6B 8C1F 96D8 BF60
i know how to do it by using standrd command line commands as grep, cut and head/tail, but is there more elegant way to do it with sed? the shell comman i use is long and not looking so nice.
thank you
grep is the right tool for extracting strings from a file based on regular expression matching:
grep -Eo '([A-F0-9]{4}[[:space:]]){9}[A-F0-9]{4}' file.html
Here is a sed command tested with GNU sed 4.2.2:
sed -nr '/(([[:xdigit:]]){4} ?){10}/p' file
It matches and prints
10 groups that are made of
4 hexdigits
followed by an optional space
With GNU sed:
sed -E 's/.*(([A-F0-9]{4}[[:space:]]){9}[A-F0-9]{4}).*/\1/' file
I have a string like prefix-2020.80-suffix-1
Here are all of possible combinations of input string
"2020.80-suffix-1"
"2020.80-suffix"
"prefix-2020.80"
"prefix-2020.80-1"
I need to cut out and assign 2020 to a variable but cannot get my desired output
Here what i got so far...
set var=`echo "prefix-2020.80-suffix-1" | sed "s/[[:alnum:]]*-*\([0-9]*\).*/\1/"`
My regexp does not work for other cases and i cannot figure out why! its more complicated that python's regexp syntax
This should work for all you inputs
sed 's/.*\(^\|-\)\([0-9]*\)\..*/\2/' test
Matches the start of the line or everything up to -[number]. and captures the number.
The problem with the original you were using was you didn't take into account when there wasn't a prefix.
You can use this grep -oP:
echo "prefix-2020.80-suffix-1" | grep -oP '^([[:alnum:]]+-)?\K[0-9]+'
2020
RegEx Demo
Using sed (with extended regex):
echo "prefix-2020.80-suffix-1" |sed -r 's/^([^-]*-|)([0-9]+).*/\2/'
Using grep:
echo "prefix-2020.80-suffix-1" |grep -oP "^([^-]*-|)\K\d+"
2020
-P is for Perl regex.
I have a LaTeX-File like this:
\usepackage[colorlinks,
citecolor=black,
urlcolor=black
]
{hyperref}
\usepackage{ngerman}
and i have to output it like this:
hyperref:colorlinks,citecolor=black,urlcolor=black
ngerman:
I may only use sed and egrep, not awk and perl.
How do I do this?
You can pipe sed command into other sed commands to use a layered approach.
Remove newlines
Convert } into }\n
Convert \usepackage[optional]{packagename} into packagename:[optional]
Remove the spaces and brackets
Here is the script:
sed ':a;N;$!ba;s/\n/ /g' file.latex | sed 's/}/}\n/g' | sed 's/\\usepackage[[:space:]]*\(.\+\)\?[[:space:]]*{\(.\+\)}/\2:\1/' | sed 's/\[//g;s/\]//g;s/[[:space:]]//g'
Here is the result:
hyperref:colorlinks,citecolor=black,urlcolor=black
ngerman:
The first command is taken verbatim from How to insert a newline in front of a pattern?. Some of these commands can be combined, but I find it easier to understand when there is a separate command for each step. If your latex files are small, time should not be a problem.
I need to used sed for following requirment using sed
I have one string as $str and I need to replace blow line in a file
abh{1..$abh} cdf_$ghu,xyz * abh{}.$xy
New modified line should be as below
abh{1..$abh} cdf_$ghu,$str * abh{}.$xy
Note "xyz" can be any arbitrary value. Could you please tell me how to do using sed in one liner.
sed 's/\(^\s*abh{1..$abh}\s*\)\(.*xyz\)/\1/' file.txt
but still does not work. Any help would be appreciated.
Try this:
$ sed 's|\(\S\+\s\+[^,]\+,\)\S\+\(\s\+.*\)|\1$str\2|' file.txt
abh{1..$abh} cdf_$ghu,$str * abh{}.$xy
Or even more simple:
$ sed 's|,\S\+|,$str|' example.txt
echo 'abh{1..$abh} cdf_$ghu,xyz * abh{}.$xy' | sed 's/\(.*\$ghu,\)\(.*\)\( .*\)/\1\$str\3/g'