[SCRIPT] read and substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SCRIPT] read and substitution
# 1  
Old 07-29-2011
[SCRIPT] read and substitution

Hi all,

Actually I have a huge bash file like:

Code:
port1=$PORT && if [ $port -eq "$NUM"]; then .....
....stuff
port2=$PORT && if [ $port -eq "$NUM"]; then .....
...stuff
port3=$PORT && if [ $port -eq "$NUM"]; then .....
...stuff
port4=$PORT && if [ $port -eq "$NUM"]; then .....
...stuff
port5=$PORT && if [ $port -eq "$NUM"]; then .....

I would like to change the $port inside the square brackets accordly to the beginning of line, by getting:


Code:
port1=$PORT && if [ $port1 -eq "$NUM"]; then .....
....stuff
port3=$PORT && if [ $port3 -eq "$NUM"]; then .....
...stuff
port8=$PORT && if [ $port8 -eq "$NUM"]; then .....
...stuff
port66=$PORT && if [ $port66 -eq "$NUM"]; then .....
...stuff
port5=$PORT && if [ $port5 -eq "$NUM"]; then .....

I was thinking to do that with sed but not able to work out a solution.
Any idea please?
Thanks in advance
# 2  
Old 07-29-2011
Code:
$ nawk -F"[= ]" ' {if($1~/port/) {$6="\$"$1; print $0} else {print $0}} ' test                                                                     
port1 $PORT && if [ $port1 -eq "$NUM"]; then .....
....stuff
port2 $PORT && if [ $port2 -eq "$NUM"]; then .....
...stuff
port3 $PORT && if [ $port3 -eq "$NUM"]; then .....
...stuff
port4 $PORT && if [ $port4 -eq "$NUM"]; then .....
...stuff
port5 $PORT && if [ $port5 -eq "$NUM"]; then .....

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-29-2011
Thanks a lot
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Partial variable substitution in script

I have a script. filecreatenew () { touch /usr/src/$1_newfile.txt var=$1 echo $var touch /usr/src/$var_newfile_with_var.txt } filecreatenew myfile Its creating file /usr/src/myfile_newfile.txt as the variable $1 is correctly used. When $ is... (2 Replies)
Discussion started by: anil510
2 Replies

2. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

3. Shell Programming and Scripting

Bad substitution error in shell script

I have script data.sh which has following error. Script Name : data.sh #!/bin/sh infile=$1 len=${#infile} echo $len texfile=${infile:0:$len-4} echo $texfile run command ./data.sh acb.xml I get following error message: (5 Replies)
Discussion started by: man4ish
5 Replies

4. Shell Programming and Scripting

sed variable substitution in a script

Hi I am trying to do the following in a script find a string and add in a block of text two lines above on the command line this works fine #/usr/bin/cat /usr/local/etc/dhcpd.conf_subnet | /usr/xpg4/bin/sed -n -e '1h;1\!H;${;g;s/}.*#END of 10.42.33.0/#START of RANGE $dstart\:option... (3 Replies)
Discussion started by: eeisken
3 Replies

5. Shell Programming and Scripting

read variable substitution

Allright so a quick question. I'm building a script that will eventually do a full IP subnet scan. It starts off by first entering an IP address, (capturing host and net ID comes after that) and I want it to use the current IP address if no input is given. Is there a quick way to define the... (1 Reply)
Discussion started by: BisMarc
1 Replies

6. Shell Programming and Scripting

Help with substitution in shell script

Hello, I Have a file like this, with five columns: B D F T L --------- 0 0 0 0 0 0 0 0 1 0 0 2 0 0 1 I need, for each row, to append another five columns similar to the first ones, but with the field labeled as "T" (4th field of the column) substituted in this way: (16 * value of... (3 Replies)
Discussion started by: PapaJustify
3 Replies

7. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

8. Shell Programming and Scripting

looking for expert sed/script help for translation/substitution

Hi All, I'm looking for some expert help on sed/script to work out the best way to transform one xml format into another however there are a few complexities around translation. The extra complexities are to: 1) take the start and stop time (YYYYMMDDHHMMSS) and convert to start time to unix... (8 Replies)
Discussion started by: hotbaws11
8 Replies

9. Shell Programming and Scripting

Making script show command (e.g. copy) being executed and variable substitution?

When script is running you only see when some of the commands are not successfull. Is there a way to see which command are executed and to show the substitution of variables as every line is executed ? (3 Replies)
Discussion started by: gr0124
3 Replies

10. Shell Programming and Scripting

Bad substitution errors in shell script

Hello, I was hoping for a second pair of eyes or a little bit of help figuring out what my error is in a script. I did some searching in the forums and didn't find anything so please forgive me if it a similar problem has been discussed before. My script accepts normal user arguments; however,... (2 Replies)
Discussion started by: Jackinthemox
2 Replies
Login or Register to Ask a Question