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'
Related
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
My text file contains 100 lines and the text file surely contains Technology and /Technology words .In which ,I want to remove Technology and /Technology words present in the file using shell scripting.
sed -i.bak -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This is delete the words and also make a backup of the original file just in case
sed -i -e 's#/Technology##g' -e 's#Technology##g' my_text_file
This will not make a backup but just modify the original file
You can try this one.
sed -r 's/<[\/]*technology>//g' a
Here is an awk
cat file
This Technology
More here
Mine /Technology must go
awk '{gsub(/\/*Technology/,"")}1' file
This
More here
Mine must go
By adding an extra space in the regex, it will not leave an extra space in the output.
awk '{gsub(/\/*Technology /,"")}1' file
This Technology
More here
Mine must go
To write back to original file
awk '{gsub(/\/*Technology /,"")}1' file > tmp && mv tmp file
If you have gnu awk 4.1+ you can do
awk -i '{gsub(/\/*Technology /,"")}1' file
how to add the current date in a each record as first column.
Input file:
12345|Test1
67890|Test2
expected Output file:
2014-04-26|12345|Test1
2014-04-26|67890|Test2
Thanks,
sed -e "s,^,$(date +'%Y-%M-%d')|," file
If you use Linux (more specifically, GNU sed) then you may use in-place editing with -i flag:
sed -i -e "s,^,$(date +'%Y-%M-%d')|," file
Otherwise you have to store results into a temporary file and then rename.
You could use awk
awk -vOFS='|' -vcdate=$(date '+%Y-%m-%d') ' {print cdate, $0}' file
You can use sed for example:
sed -i "/^$/ !s/^/`date +"%Y-%m-%d"`|/" data_file
If you want to edit the file, why not use ed, the standard editor? the common and nice versions of ed will support the following:
printf '%s\n' "$(date '+%%s/^/%Y-%m-%d|/')" wq | ed -s file
(this will edit the file in place, so make sure you have appropriate backups if you want to revert the changes).
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
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