Replace a line in a file using shell script
NickName:sagar Ask DateTime:2015-06-11T10:38:12

Replace a line in a file using shell script

I need to replace a line in from a file using a shell script.

The file is:

# cat /root/sge_scripts/night_sched_conf
algorithm default
schedule_interval 0:0:05
maxujobs 12
queue_sort_method seqno

I want to replace the third line (after maxujobs) by a value generated in the script.

Here is what I have done so far:

#!/bin/sh
export SGE_ROOT=/opt/sge
source=/opt/sge/defalt/common/settings.sh
delete=1
total=`qstat -u "*" | awk ' { print $4 } ' | sort | uniq | wc -l`
total_users=$((total - delete))

case $total_users in

1) range=18;;
2) range=9 ;;
3) range=7 ;;
4) range=6 ;;
5) range=5 ;;
6) range=4 ;;
7) range=3 ;;
8) range=3 ;;
9) range=2 ;;
10) range=2 ;;
11) range=2 ;;
12) range=2 ;;
13) range=1 ;;
14) range=1 ;;
15) range=1 ;;
16) range=1 ;;
18) range=1 ;;
19) range=1 ;;
20) range=1 ;;
21) range=1 ;;
22) range=1 ;;
23) range=1 ;;
24) range=1 ;;
25) range=1 ;;
26) range=1 ;;
27) range=1 ;;
28) range=1 ;;
29) range=1 ;;
30) range=1 ;;
31) range=1 ;;
32) range=1 ;;
33) range=1 ;;
34) range=1 ;;
*) echo "INVALID NUMBER!" ;;
esac

current=`cat /root/sge_scripts/night_sched_conf | awk ' /maxujobs/ { print $2 } '`
if [ "$range" != "$current" ]
then
  sed -i '3s/.*/maxujobs           "$range"/' /root/sge_scripts/night_sched_conf
fi

Copyright Notice:Content Author:「sagar」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30770618/replace-a-line-in-a-file-using-shell-script

More about “Replace a line in a file using shell script” related questions

Replace a line with slashes in a file using shell script

I am trying to replace a line in a text file using shell script The line in the file is path=/home/new/abc/xyz and i have to change it to path=/arun/old I want to use shell script to do this. I have

Show Detail

Replace a line in a file using shell script

I need to replace a line in from a file using a shell script. The file is: # cat /root/sge_scripts/night_sched_conf algorithm default schedule_interval 0:0:05 maxujobs 12 queue_sort_method seqno I

Show Detail

Need to replace a specific part of line from a file using shell script

I'm trying to replace the path part of below line from postgresql.conf using shell script: data_directory = '/var/lib/postgresql/10/main' # use data in another directory I first checked if I'm

Show Detail

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

I have started learning shell scripting a few days ago. My scenario is, i have to search and replace a content which exists in different lines in a file using shell script. For Example, I have men...

Show Detail

How to replace multi line text in html file with a shell script?

I have an html file in which I want to replace a style using a shell script using the function sed. I want to replace: pre { font-size: inherit; line-height: inherit; } with: ...

Show Detail

shell script replace and add next line with special characters

I am trying replace a line with another line and add new line below that using shell script. My file content looks like below. I want to replace export PATH with JAVA_HOME=/usr/lib/jvm/java-1.8.0-i...

Show Detail

replace a line in files present in different folders shell script

I have a common line in different files present in different folders. how can i replace that line with some other line using shell script sed command in all the files of the folders ? Eg: t="/home...

Show Detail

Replace a statement in java file using shell script

I need to replace a variable assignment statement using shell script. Eg: MyConstants.java file contains lines like String abcd = "ABCD"; String abc = "ABC"; String e = abcd + "e"; I need t

Show Detail

How to replace a html comments using shell script

I am trying to uncomment a line in html file using shell script but I am not able to write a sed command for this . I have a line <!--<url="/">--> I need to uncomment this line using

Show Detail

How to find and replace line in shell script

I want to find the line starting with a string and replace the entire line using the shell command. Example : content of the file: Enableuserlogin Yes search word : Enableuserlogin replace with :

Show Detail