Clear a particular line of a file through bash shell - bash

I want to clear the contents of a particular line in a file.
I could delete the line by using sed:
sed -i "${linenumber}d" filename.txt
But I dont want the line to be deleted. I want the line to stay with no content.
Is there a way to do that?

Sure:
sed -i "${linenumber} s/.*//" filename.txt
The ${linenumber} specifies which line to work on, the delete command (d) in your sed script can be replaced by any other command, here the substitution command (s/pattern/replacement/).

Related

How to use sed and cat to add multi lines from one file to another

How can I use a cat and sed to read data from a file and insert it into another file under known line?
For example I have a file named script1.txt that contains a few hundred lines, one of the line has the value "COMMANDS="commands"
If I wanted use sed to insert a line under it, simply I can use sed as the command bellow.
sed -i '/^COMMANDS=.*/a NEW LINE HERE' script1.txt
But if I want to insert a multi lines and these lines inside a file, and these line changes every a few hours.. how can i do that ?
I tried:
DATA=$(cat data.txt)
sed -i '/^COMMANDS=.*/a '$DATA'' script1.txt
I got the error bellow.
sed: -e expression #1, char 1: unknown command: `"'
Is there a way other than sed to insert the data from file under known line with no issues?
This might work for you (GNU sed):
sed -i '/^COMMANDS=/r dataFile' file
This will append the contents of the file dataFile after the line beginning COMMANDS= and update file
If the data you want to append is multi-line, you might want to replace newlines with \n.
#!/bin/sh
DATA="$(awk '{gsub(/[]\/$*.^&[]/, "\\\\&");printf (FNR>1)?"\\n%s":"%s",$0}END{print ""}' data.txt)"
sed -i -e '/^COMMANDS=.*/a\' -e "$DATA" script1.txt
Here the awk command escapes sed special characters (for basic regular expressions), then prints "%s" for the first line, and "\\n%s" for the others. A newline is printed at the end, but it's somewhat pointless as $() strips it anyway.
The sed command is almost the same but multiple expressions are used which is equivalent to a multi-line sed script (The a text sed alternative syntax can act weirdly with leading spaces/backslashes).

shell command to change file and save

I want to modify file content with shell script like replace the line 3 in file with a new string, and then save to the original file. Can anyone give advice to achieve that?
It's a bit unusual to do that with vim from a shell script, but since you asked:
vim -es '+3s/.*/a new string' '+wq' file
Usually, you would chose another tool like (sed -i is in-place edit):
sed -i '3s/.*/a new string/' file
Or with awk
gawk -i inplace 'NR==3{$0="a new string"}1' file
In a Unix-like system, you can use sed to replace the content of specific line. For example, below command will replace the 3rd line with "HelloWorld" in text.txt file.
sed -i '3c HelloWorld' text.txt
In case you only want to change part of 3rd line content, you can use :
sed -i '3s/aaa/bbb/' text.txt
this will only replace string "aaa" into "bbb" in 3rd line.
ed would be more appropriate than sed or vim, as it is designed to edit files in-place programmatically. (sed is the stream version of ed; -i is a non-standard extension.)
printf '3s/.*/new stuff/\nw\n' | ed my_file.txt

Bash: Output result of function into Sed parameters

sed -i '$a\curl -s http://whatismyip.org/' file
Trying to find a way to pull the WAN IP and insert it into the last line of a file as illustrated above (not working of course). This will be utilized via command line.
sed -i '$a\test' file
This will insert "test" after the last line in "file" as utlilized but how could I output the result of a function or command in it's place within Sed's syntax? Any suggests (awk, perl, bash script?) are welcome!
sed isn't required here. Just use this:
curl -s http://whatsmyip.org >> your.file
Note that bash supports the >> redirection operator which appends a program's output to a file
hek2mgl has shown you how to solve this specific problem. To address the more general question, you can do:
var=$(some command line)
This sets the shell variable $var to the output of the command. Then you can subsitute this into sed with:
sed -i "\$a\\$var" file

delete the last 2 characters from a text file: shell script

I need to delete the last 2 characters from a text file in shell script.
Any idea about how I can do it?
Delete the last two characters on the last line only with sed:
$ sed '$s/..$//' file
If you are happy with changes then use -i to store them back to the file:
$ sed -i '$s/..$//' file
If you wanted to delete the last two characters on every line it would be:
$ sed 's/..$//' file
Again use -i to store the changes back to the file:
$ sed -i 's/..$//' file

replace a string in file using shell script

Suppose my file a.conf is as following
Include /1
Include /2
Include /3
I want to replace "Include /2" with a new line, I write the code in .sh file :
line="Include /2"
rep=""
sed -e "s/${line}/${rep}/g" /root/new_scripts/a.conf
But after running the sh file, It give me the following error
sed: -e expression #1, char 14: unknown option to `s'
If you are using a newer version of sed you can use -i to read from and write to the same file. Using -i you can specify a file extension so a backup will be made, incase something went wrong. Also you don't need to use the -e flag unless you are using multiple commands
sed -i.bak "s/${line}/${rep}/g" /root/new_scripts/a.conf
I have just noticed that as the variables you are using are quoted strings you may want to use single quotes around your sed expression. Also your string contains a forward slash, to avoid any errors you can use a different delimiter in your sed command (the delimiter doesn't need to be a slash):
sed -i.bak 's|${line}|${rep}|g' /root/new_scripts/a.conf
You have to write the changes to a new file and then, move the new file over the old one. Like this:
line="Include 2"
rep=""
sed -e "s/${line}/${rep}/g" /root/new_scripts/a.conf > /root/new_scripts/a.conf-new
mv /root/new_scripts/a.conf-new /root/new_scripts/a.conf
The redirection (> /root/new_scripts/a.conf) wipes the contents of the file before sed can see it.
You need to pass the -i option to sed to edit the file in-place:
sed -i "s/${line}/${rep}/g" /root/new_scripts/a.conf
You can also ask sed to create a backup of the original file:
sed -i.bak "s/${line}/${rep}/g" /root/new_scripts/a.conf
So, if you have to replace a substring in a file, you can use sed command like this, say we have a file as file.txt, so replacing a substring in it can be done like this
searchString="abc";
replaceString="def";
sed -i '' "s|$searchString|$replaceString|g" file.txt
This will all the occurrences of "abc" with "def" in file.txt. Also, this keeps a check for any / character present in the variables used, and with no backup file made.

Resources