sed + add remark before string in specific line - sed

My target is to add remark "#" before the "dialog" string
only in line that has the "Restart nfs and apply changes" line
Why my sed command not add the "#" char before the dialog string? , what wrong? In my syntax?
sed -i -r '/Restart nfs and apply changes/s/dialog ?$/#dialog/' /etc/init.d/nfsscript.sh
the line in /etc/init.d/nfsscript.sh file :
dialog --clear --colors --title "nfs Config" --yesno "Restart nfs and apply changes?" 10 20

This might work:
sed -e '/Restart nfs and apply changes/s/dialog/\#dialog/' -i /etc/init.d/nfsscript.sh

Related

Removing a specific line in bash with an exact string

I'm having trouble in getting sed to remove just the specific line I want. Let's say I have a file that looks like this:
testfile
testfile.txt
testfile2
Currently I'm using this to remove the line I want:
sed -i "/$1/d" file
The issue is that with this if I were to give testfile as input it would delete all three lines but I want it to only remove the first line. How do I do this?
With grep
grep -x -F -v -- "$1" file
# or
grep -xFv -- "$1" file
-F is for "fixed strings" -- turns off regex engine.
-x is to match entire line.
-v is for "everything but" the matched line(s).
-- to signal the end of options, in case $1 starts with a hyphen.
To save the file
grep -xFv -- "$1" file | sponge file # `moreutils` package
# or
tmp=$(mktemp)
grep -xFv -- "$1" file > "$tmp" && mv "$tmp" file
So match the whole line.
var=testfile
sed -i '/^'"$var"'$/d' file
# or with " quoting
sed -i "/^$var\$/d" file
You can learn regex with fun online with regex crosswords.

How do I add the sed command inside a bash script?

I want to add below line to build.sh file to a line number 26
sed -i 's/-DskipTests //' dev/make-distribution.sh
I tried with this command
sed "26 a sed -i 's/-DskipTests //' dev/make-distribution.sh" build.sh
But this is giving error
sed: 1: "26 a sed -i 's/-DskipTe ...": command a expects \ followed by text`
Try putting it in as if it were multiline add as a workaround. Does this work for you?
sed "26 a\\sed -i 's/-DskipTests //' dev/make-distribution.sh" build.sh
You need to quote your quote since you are compounding quote-types.
c.f. the manual, though - it ought to work as it is.

Sed. Save lines that are ending with specific file-type

I have a textfile and would like to save all lines that are ending with .m2 or .M2. I tried several ways including this here
D:\filetype\core\sed.exe -n -e "s/^\(.*\)\.M2//" D:\filetype\listfile\test.txt > D:\filetype\listfile\test2.txt
But as result i only get a emtpy textfile, so i guess something is wrong with my code.
The other way was
D:\filetype\core\sed.exe -n -e "^/\(.*)\/.M2\/" D:\filetype\listfile\test.txt > D:\filetype\listfile\test2.txt
But in this case i wasn't able to locate the source of the error
unknown command: `^'
Thanks if someone can see my fault.
You can use below sed command:
sed -n -e '/\.[mM]2$/p' <file_name>
This will print all the lines which have .m2 or .M2 at the end
Now comming to issues with your commands. Your first command does:
sed -n -e "s/^\(.*\)\.M2//"
which is a search and replace command indiacated by s in the command. Syntax for this command is s/search_text/replace_text/. Now if you are look at your command carefully, you are searching for something and replacing it with nothing - as indicated by last // in your command - where replace_text is missing.
Your second command does
sed -n -e "^/\(.*)\/.M2\/"
which is incorrect syntax. General syntax of a sed command is :
sed -e 'line_range command'
where line range - which is optional - can be line numbers like1, 5 , 2-5, or a regular expression like /[gG]/, /[gG][iIuU]rl/.
If line_range is missing, the first letter in sed command should be a command. In your case: line_range is missing - which is fine syntax wise -, however the first letter is ^ - which is not a sed command - because of which you are getting syntax error.
The command that I suggested is
sed -n -e '/\.[mM]2$/p'
Here, line_range is the regular expression /\.[mM]2$/ - which says "any line which has .m2 or .M2 at the end", and command is p, which is the letter for print command.
Sed is mostly used to transform text. Why not use grep instead?
grep -i "\.m2$"
This will match case insensitively (-i) any line ending with .m2.

wget - work with arguments

I have a list of URI: uri.txt with
category1/image1.jpeg
category1/image32.jpeg
category2/image1.jpeg
and so on, and need to download them from domain example.com with wget, with additional changing filename (final at save) to categoryX-imageY.jpeg
I understand, that I should read uri.txt line by line, add "http://example.com/" in front of each line and change "/" to "-" in each line.
What I have now:
Reading from uri.txt [work]
Adding domain name in front of each URI [work]
Change filename to save [fail]
I'm trying to do this with:
wget 'http://www.example.com/{}' -O '`sed "s/\//-/" {}`' < uri.txt
but wget fails (it depends what type of quotation sign I'm using: ` or ') with:
wget: option requires an argument -- 'O'
or
sed `s/\//-/` category1/image1.jpeg: No such file or directory
sed `s/\//-/` category1/image32.jpeg: No such file or directory
Could you tell, what I'm doing wrong?
Here is how I would do that:
while read LINE ; do
wget "http://example.com/$LINE" -O $(echo $LINE|sed 's=/=-=')
done < uri.txt
In other words, read uri.txt line by line (the text being placed in $LINE bash variable), before performing the wget and saving with modified name (I use another sed delimitor, to avoid escaping / and making it more readable)
When I want to construct a list of args to be executed, I like to use xargs:
cat uri.txt | sed "s#\(.*\)/\(.*\)#http://example.com/\1/\2 -O \1-\2#" | xargs -I {} wget {}

how to remove trailing line from every file with our renaming the fuel

Hi I use the following code to remove the trailing lines from a file. But is there anyway I can run this on 2000 files inside a folder with out renaming them ? thanx in advance
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba'
sed -i "" -e :a -e '/^\n*$/{$d;N;};/\n$/ba' YourFile
if it is in same folder with a pattern to select id like *.txt, replace YourFile by the shell pattern, if other selection pass each file name via a pre selection like find or a while read from a stream/file input
You can use find to list the files that you need, and then run sed on each on them. This version uses the -i option to modify the files in place:
find . -name "*.txt" | xargs -I % sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' %