Changing Contents of a File Through Shell Script

Changing contents of a file through shell script

How about something like:

#!/bin/bash

addr=$1
port=$2
user=$3

sed -i -e "s/\(address=\).*/\1$1/" \
-e "s/\(port=\).*/\1$2/" \
-e "s/\(username=\).*/\1$3/" xyz.cfg

Where $1,$2 and $3 are the arguments passed to the script. Save it a file such as script.sh and make sure it executable with chmod +x script.sh then you can run it like:

$ ./script.sh 127.8.7.7 7822 xyz_ITR4

$ cat xyz.cfg
group address=127.8.7.7
port=7822
Jboss username=xyz_ITR4

This gives you the basic structure however you would want to think about validating input ect.

Changing contents of files through shell script

You can use awk:

awk '$4 >= 23 {print}' file

that can be shortened to(thanks @RomanPerekhrest):

awk '$4 >= 23' file

If you want to write the file in place, you can use a temporary file:

awk '$4 >= 23' file > tmp && mv tmp file

In case you have gawk 4.1.0 or later, you can use the -i flag to edit the file in place:

gawk -i '$4 >= 23' file

Or using a Bash loop:

while read -r a b c d; do
[[ $d -ge 23 ]] && echo $a $b $c $d
done < file

Find and Replace Inside a Text File from a Bash Command

The easiest way is to use sed (or perl):

sed -i -e 's/abc/XYZ/g' /tmp/file.txt

Which will invoke sed to do an in-place edit due to the -i option. This can be called from bash.

If you really really want to use just bash, then the following can work:

while IFS='' read -r a; do
echo "${a//abc/XYZ}"
done < /tmp/file.txt > /tmp/file.txt.t
mv /tmp/file.txt{.t,}

This loops over each line, doing a substitution, and writing to a temporary file (don't want to clobber the input). The move at the end just moves temporary to the original name. (For robustness and security, the temporary file name should not be static or predictable, but let's not go there.)

For Mac users:

sed -i '' 's/abc/XYZ/g' /tmp/file.txt

(See the comment below why)

How to search and replace a content of a file using shell script?

I believe you missed the g option in your sed command. See below

sed "s/find/replace/g" filename

Changing contents of a tsx file through shell script

This should work:

sed -i 's/development/production/g' config.tsx

The -i option will edit the file in place. If you first want to try the command to see if it works the way you want, use it without the -i. The output will be printed to stdout.

How to change contents of a file in shell script?

sed -i 's!$strurl!$url!g' filename

This will be the answer as url itself contains '/' as separator so we need to use some other separator inside sed command

shell command to change file and save

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

How to modify configuration files using shell script

Would you please try the following:

#!/bin/bash

files=( "$dataset_path"train_new.record-* ) # assign an array "files" to the list of matched files
datalist="\"${files[0]##*/}\"" # 1st element of the list, dirname removed and enclosed by double quotes
for (( i = 1; i < ${#files[@]}; i++ )); do # append remaining elements by prepending comma, newline and tabs
datalist+=$',\\n\t\t"'"${files[$i]##*/}\""
done

sed -i'.org' -E '
:l ;# define a label "l"
N ;# append next line
$!b l ;# go to label "l" unless eof
# now the pattern space holds the entire file
# then perform the replacement across the lines
s|(train_input_reader:[^}]*input_path: *\[)[^]]*|\1'"$datalist"'|' "$folder$config_path"

Output:

train_input_reader: {
tf_record_input_reader {
input_path: ["train_new.record-001",
"train_new.record-002"]
}
}

eval_input_reader: {
tf_record_input_reader {
input_path: ["test.record-0001"]
}
}

As you mention you're working with MacOS, it is tested with bash 3.2 on Linux. But I'm not confident about the interoperability of sed.

[Edit]

Explanation about the regex:

  • As (train_input_reader:[^}]*input_path: *\[) is enclosed with parentheses,
    the matched substring is reused in the replacement text as \1.
  • [^}]* matches any characters other than }. It prevents the longest
    match to the last input_path.
  • [^]]* matches zero or more sequence of any characters other than ].
    It will match the value of input_path enclosed with square brackets
    and will be replaced with
    datalist.

A shell script to to change a value in a file with a parameter

$ sed 's/\($Dbconnection=\).*/\1NewValue/' param.prm 
[3_go:wf_test:s_test]

$Dbconnection=NewValue

$Dbstring=qwert

To change the file in-place, use the -i option. For GNU sed:

sed -i 's/\($Dbconnection=\).*/\1NewValue/' param.prm 

For BSD (OSX) sed:

sed -i "" 's/\($Dbconnection=\).*/\1NewValue/' param.prm 


Related Topics



Leave a reply



Submit