LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-10-2015, 08:43 PM   #1
sagar666
Member
 
Registered: Feb 2013
Posts: 212

Rep: Reputation: Disabled
need help to replace line in shell script


Below is the file i need to replace third line with genarated output from script

My file:

[root@isge ~]# cat /root/sge_scripts/night_sched_conf
algorithm default
schedule_interval 0:0:05
maxujobs 12
queue_sort_method seqno

I want to replace in third line maxujobs number 12 to my script generated value (range value in below script)

My script:
#!/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

Please help me
 
Old 06-10-2015, 10:59 PM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
What exactly is the problem? Is the substitution not being made?
 
Old 06-11-2015, 12:24 AM   #3
sagar666
Member
 
Registered: Feb 2013
Posts: 212

Original Poster
Rep: Reputation: Disabled
I cant able to pass range value in substitutions

sed -i '3s/.*/maxujobs "$range"/' /root/sge_scripts/night_sched_conf its not working instead value its its substitution $range itself
 
Old 06-11-2015, 12:33 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Try this:
Code:
sed -i '3s/.*/maxujobs '"$range/" /root/sge_scripts/night_sched_conf
 
1 members found this post helpful.
Old 06-11-2015, 09:37 AM   #5
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Why do you have quotes around $range, when none is present in the original? Do you want the replacement to have quotes?

If not, and you only want the number, try this:

Code:
sed -i "3s/.*/maxujobs ${range}/" /root/sge_scripts/night_sched_conf
If you do indeed want the double quotes around the number, try this:

Code:
sed -i "3s/.*/maxujobs \"${range}\"/" /root/sge_scripts/night_sched_conf
Note:

I always use brackets for safety.

The type of quote - single or double - does matter to the shell (usually bash or dash by default) when doing substitutions. Using bash/dash, a variable name in single quotes will not be expanded to its value, whereas in a double quote the variable will be expanded.

Last edited by goumba; 06-11-2015 at 09:43 AM. Reason: Spelling and such
 
Old 06-12-2015, 08:58 PM   #6
sagar666
Member
 
Registered: Feb 2013
Posts: 212

Original Poster
Rep: Reputation: Disabled
Thanks for your replies
 
Old 06-13-2015, 03:21 AM   #7
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
If this solves your problem, please, mark the thread as solved.
 
  


Reply

Tags
linux, programing, scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - Cant get sed to replace a line from a textfile CrowdstarQA Programming 4 02-23-2012 11:53 AM
shell script replace a line from a file dthanoon Programming 11 03-05-2010 09:46 AM
How to replace line with another line in shell script pramod.srk Linux - Newbie 5 02-26-2010 06:00 AM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 09:43 AM
How to identify a line and replace another string on that line using Shell script? Sid2007 Programming 10 10-01-2007 08:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration