I am trying to change the next line using 'sed' command
"metamodes" "DFP-0: 2048x2048 +1920+0, DFP-2: 1280x1024 +640+0, DFP-1: 1920x1080 +0+1024"
I need sed command find the resolution 2048x2048 or 1280x1024 or 1920x1080 and change the position (+1920+0 or +640+0 or +0+1024). Like this position is not always the same, I cannot sed only this position and I have to find the resolution (that always will be the same) and change the link position to the resolution.
The final result should be:
"metamodes" "DFP-0: 2048x2048 +1280+0, DFP-2: 1280x1024 +0+1024, DFP-3: 1920x1080 +1408+4440"
I used the next command:
sed 's/2048x2048.*[^,]/2048x2048 +1280+0,/g' /etc/X11/xorg.conf
Any suggestion?
thanks
With .*[^,] you match "any character as much as possible when the last character is not a ,. You want to match everything until the first ,. Use [^,]*:
sed 's/2048x2048[^,]*/2048x2048 +1280+0/g' /etc/X11/xorg.conf
The 3 replacements can be combined:
sed 's/2048x2048[^,]*/2048x2048 +1280+0/g;
s/1280x1024[^,]*/1280x1024 +640+0/g;
s/1920x1080[^,]*/1920x1080 +0+1024/g' /etc/X11/xorg.conf
Testing this will show, that the " is matched too, so you need to add the quote to the character class:
sed 's/2048x2048[^,"]*/2048x2048 +1280+0/g;
s/1280x1024[^,"]*/1280x1024 +640+0/g;
s/1920x1080[^,"]*/1920x1080 +0+1024/g' /etc/X11/xorg.conf
Related
Let's say i have a file where the word "default" is repeated more times. I want to replace it with "custom" but just where it follows the word "background", regardless of the line number. How could I achieve this with the sed command?
I have been able to do it with
sed - i '/set background default/c\set background custom /` file.conf but it's not really worth changing a whole line for just replacing a single word.
Like this, using capture group:
sed -i -E 's/(set background) default/\1 custom/' file.conf
# ^--------------^ ^-^
# captured restaured
Say there is a character like this #ま. If I wanted to add a space after this i could do sed -i "s/#ま/#ま\ /g" * to add a space. But I would like to not do that because it would create a problem where there are double spaces.
Given a text such as #あの #ま高校生の頃に. #あの #ま 高校生の頃に. How do I add a space after #ま if there is no space in the beginning?
so the output would be something like
#あの #ま 高校生の頃に. #あの #ま 高校生の頃に
You can use sed with -r for regular expression.
For your example, you can use this command:
sed -i -r 's/#ま([^ ])/#ま \1/g' <file>
Essentially, you are searching for #ま[^ ], which is a #ま followed by exactly one non-space character. Then, you are replacing all such matches with #ま<space><that non-space character>.
Take the string "hello_world 1 2 3"
I want the output to be "hello_world"
My attempt is "s/\(.*\) .*/\1/g"
But I get "hello_world 1 2"
Instead of stopping at the first space after the sequence, it gets the last space on the line.
I want to take any length of characters \(.*\) followed by a space ' ' and remove anything that comes after it .*
How can I do it?
Could you please try following.
echo "hello_world 1 2 3" | sed 's/\([^ ]*\).*/\1/'
Explanation of above:
Using sed's capability of storing matched regex into a temp buffer. Which could be later accessed by variables like 1, 2 and so on(depending upon number of buffers you are mentioning).
In here we are capturing everything till occurrence of first space into 1st temp buffer and then keeping everything as it is .*. While substituting we are mentioning \1 here which means substitute whole line's value with first matched/stored value of 1st temp buffer(which is hello_world).
Why OP's code not working: Because OP using .* which is a greedy matched regex and capturing all the line in 1st buffer itself that's why when its used \1 its actually printing whole line there.
This might work for you (GNU sed):
sed 's/\s.*//' file
Matches the first white space character and everything thereafter and removes it, leaving whatever is in front of i.e. all non-white space characters.
Same as:
sed 's/^(\S+).*/\1/' -E file
http://www.somesite/play/episodes/xyz/fred-episode-110
http://www.somesite/play/episodes/abc/simon-episode-266
http://www.somesite/play/episodes/qwe/mum-episode-39
http://www.somesite/play/episodes/zxc/dad-episode-41
http://www.somesite/play/episodes/asd/bob-episode-57
i have many url's saved in a txt file like show above i want to move everything after the 6th backslash up one line with a sed script
the txt after the 6th backslash is the title and always different i need to select the title so i can play it
so i need it to look like this
fred-episode-110
http://www.somesite/play/episodes/xyz/fred-episode-110
simon-episode-266
http://www.somesite/play/episodes/abc/simon-episode-266
mum-episode-39
http://www.somesite/play/episodes/qwe/mum-episode-39
dad-episode-41
http://www.somesite/play/episodes/zxc/dad-episode-41
bob-episode-57
http://www.somesite/play/episodes/asd/bob-episode-57
using just sed
i can do this with awk but i want to do this with just sed
You can use the following sed command:
sed 'h;s#\([^/]*/\)\{6\}##;p;x;' sed_test.txt
On your input:
Explanations:
h; copy your pattern buffer to your hold buffer
s#\([^/]*/\)\{6\}##; delete until the 6th / the content of your pattern buffer
p; print the pattern buffer
x exchange the pattern buffer and hold buffer content
then do the default action -> print the content of the pattern buffer
You can use this one too
sed -E 's|(.*/)(.*)|\2\n&|' infile
I'm having issues matching strings even if they start with any number of white spaces. It's been very little time since I started using regular expressions, so I need some help
Here is an example. I have a file (file.txt) that contains two lines
#String1='Test One'
String1='Test Two'
Im trying to change the value for the second line, without affecting line 1 so I used this
sed -i "s|String1=.*$|String1='Test Three'|g"
This changes the values for both lines. How can I make sed change only the value of the second string?
Thank you
With gnu sed, you match spaces using \s, while other sed implementations usually work with the [[:space:]] character class. So, pick one of these:
sed 's/^\s*AWord/AnotherWord/'
sed 's/^[[:space:]]*AWord/AnotherWord/'
Since you're using -i, I assume GNU sed. Either way, you probably shouldn't retype your word, as that introduces the chance of a typo. I'd go with:
sed -i "s/^\(\s*String1=\).*/\1'New Value'/" file
Move the \s* outside of the parens if you don't want to preserve the leading whitespace.
There are a couple of solutions you could use to go about your problem
If you want to ignore lines that begin with a comment character such as '#' you could use something like this:
sed -i "/^\s*#/! s|String1=.*$|String1='Test Three'|g" file.txt
which will only operate on lines that do not match the regular expression /.../! that begins ^ with optional whiltespace\s* followed by an octothorp #
The other option is to include the characters before 'String' as part of the substitution. Doing it this way means you'll need to capture \(...\) the group to include it in the output with \1
sed -i "s|^\(\s*\)String1=.*$|\1String1='Test Four'|g" file.txt
With GNU sed, try:
sed -i "s|^\s*String1=.*$|String1='Test Three'|" file
or
sed -i "/^\s*String1=/s/=.*/='Test Three'/" file
Using awk you could do:
awk '/String1/ && f++ {$2="Test Three"}1' FS=\' OFS=\' file
#String1='Test One'
String1='Test Three'
It will ignore first hits of string1 since f is not true.