I've a text file with a line
default_color acolor
and I want to replace this line with
default_color anothercolor
I don't know the first color and the second is contained in a variable.
How can I do it in bash ?
Thank you
It is not pure bash but if you have other console tools, try something like
cat your_file | sed "s/default_color\ .*/default_color\ $VAR/"
You could use awk. The manual entry is here:
http://ss64.com/bash/awk.html
I won't write the regular expression necessary, as that will depend on your color format, but this will fit the bill. Best of luck.
use gawk
awk '$2=="defaultcolor"{$2="anothercolor"}1' file
or just bash shell and no external commands
while read -r line
do
case "$line" in
*defaultcolor*) line=${line/defaultcolor/anothercolor}
esac
echo $line
done <"file"
Not really a bash solution, but you can do the replacement in-place, for any number of files, with perl:
perl -i -npe 's/default_color acolor/default_color another_color/' list_of_files
Related
Is it possible with the use of some linux tool change lines in file like following:
was:
status:<whatever>
become:
status:"red"
UPDATE
The best solution:
awk 'BEGIN{FS=OFS=":"} $1 ~ /status/ {$2="\"red\""}1' file
This will print output and we can redirect it to file or whatever.
sed is the Stream EDitor, its purpose is to edit text streams, not text files. It really is the wrong tool for the job here. You should use a text file editor such as ed instead:
ed -- /path/to/file <<-HERE
,s/^status:.*/status:"red"
w
q
HERE
With GNU sed:
sed -E 's/^(status:).*/\1"red"/' file
With a string:
s="red"
sed -E 's/^(status:).*/\1"'"${s//\//}"'"/' file
If you want to edit your file "in place" use sed's option -i.
If your Input_file is same as shown sample then following may help you in same.
echo "status:<whatever>" | sed 's/\(.*:\)\(.*\)/\1"red"/g'
I am new to this platform. Just had a requirement which I have been working over sometime but not able to find it.
If this pattern was to occur in the middle of a line. How to handle it. Suppose the line is like. aaaa ---- bbbb. If i want to erase the ----bbbb part how to do it. But I want to keep the aaaa part as it is in the file.
Thanks
You can do it easily with sed:
sed -r 's/^--.*//' inputfile > outputfile
Or in place:
sed -r -i.bak 's/^--.*//' inputfile
This will create an inputfile.bak as a backup before modifying the file
Here is a good old bash solution:
while read -r line; do
echo "${line/#--*/}"
done < inputFile > outputFile
One way using awk:
awk '/^--/{$0=" ";}1' file
This will repalce the line with a space when it begins with --
Its not clear from your problem statement what the criteria (limitations) of the solution is.
What you are looking for is something that will support regular expressions. There are a lot of UNIX/Linux tools that can be used to solve this problem.
One simple solution is:
# cat file.txt | sed -e "{s/^--.*/ /}"
The regular expression "^--." will match any line beginning "^" with "--" followed
by any number of characters ".". "s" is the sed substitution command.
so "s/^--.*/ /" means, substitute all lines that start with -- and are followed by any
number of characters with a single space.
The command I'm trying to use is
sed -n 's/'$LASTNAME'/pgIw '$TEMP_FILE2'' < "$TEMP_FILE"
My goal is to get search TEMP_FILE for the value in LASTNAME and write the line containing the match, if there is one, it to TEMP_FILE2. I keep getting that the sed command is garbled. The code above will return
sed: command garbled: s/smith/pgIw /tmp/tmp.aKaGFH
Any help is appreciated! I've been trying to figure this out for hours! This is suppose to be done in the Korn shell in UNIX and I can't use awk or python, it is the stipulations of the homework.
Thank you!
It looks like you have an abundance of quotes that you don't need. Try:
sed -n "/$LASTNAME/p" >$TEMP_FILE2 <$TEMP_FILE
Also, your use of the s sed command seems to be out of place, since you don't actually want to substitute anything.
If you just want to find something and pipe to output, then simply use grep
grep -i "$LASTNAME" "$TEMP_FILE" > "$TEMP_FILE2" # -i case-insensitive
Or Perl
perl -ne "print if /$LAST_NAME/" "$TEMP_FILE" > "$TEMP_FILE2"
I have a file named test-domain, the contents of which contain the line 100.am.
When I do this, the line with 100.am is deleted from the test-domain file, as expected:
for x in $(echo 100.am); do sed -i "/$x/d" test-domain; done
However, if instead of echo 100.am, I read each line from a file named unwanted-lines, it does NOT work.
for x in $(cat unwanted-lines); do sed -i "/$x/d" test-domain; done
This is even if the only contents of unwanted-lines is one line, with the exact contents 100.am.
Does anyone know why sed delete line works if you use echo in your variable, but not if you use cat?
fgrep -v -f unwanted-lines test-domain > /tmp/Buffer
mv /tmp/Buffer test-domain
sed is not interesting in this case due to multiple call in shell (poor efficiency and lot of ressources used). The way to still use sed is to preload line to delete, and make a search base on this preloaded info but very heavy compare to fgrep in this case
Does anyone know why sed delete line works if you use echo in your
variable, but not if you use cat?
I believe that your file containing unwanted lines contains CR+LF line endings due to which it doesn't work when you use the file. You could strip the CR in your loop:
for x in $(cat unwanted-lines); do x="${x//$'\r'}"; sed -i "/$x/d" test-domain; done
One better strategy than yours would be to use a genuine editor, e.g., ed, as so:
ed -s test-domain < <(
shopt -s extglob
while IFS= read -r l; do
[[ $l = *([[:space:]]) ]] && continue
l=${l//./\\.}
echo "g/$l/d"
done < unwanted-lines
echo "wq"
)
Caveat. You must make sure that the file unwanted-lines doesn't contain any character that could clash with ed's regexps and commands. I have already included a match for a period (i.e., replace . with \.).
This method is quite efficient, as you're not forking so many times on sed, writing temp files, renaming them, etc.
Another possibility would be to use grep, but then you won't have the editing option ed offers.
Remark. ed is the standard editor.
why not just applying the sed command on your file?
sed -i '/.*100\.am/d' your_file
Id like to know a good way to read a file and delete all lines of text that start with numbers, i.e [0-9] using some shell scripting. Thanks in advance!
You could use something like this:
grep -v '^[0-9]' input-file > output-file
Using sed:
sed '/^[0-9]/d' file > outfile
If you have GNU sed, the original file itself can be updated:
sed -i '/^[0-9]/d' file