Change the numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change the numbers
# 8  
Old 04-28-2014
Hello Nakul,

As per my code in place of NR==1, NR==2 and NR==3 you can put the line numbers on which these lines are in file.


Thanks,
R. Singh
# 9  
Old 04-28-2014
HI Ravindar,

Yes we can use the line numbers, but I am not sure that everytime line number will be the same.
Is there any solution which include searching the values and changing it as per requirement in same file?
# 10  
Old 04-28-2014
If you put the values in a mod file:
Code:
Data_Center_costing=205
Financial_loss=45
operational_cost=78

You could try
Code:
awk 'NR==FNR{A[$1]=$2; next} $1 in A{$2=A[$1]}1' FS=\\= OFS=\\= modfile file > newfile &&
mv "newfile" "file"


Last edited by Scrutinizer; 04-28-2014 at 09:12 AM.. Reason: Forgot closing brace, thanks Ravinder..
# 11  
Old 04-28-2014
Posted by Scrutinizer:

Quote:
If you put the values in a mod file:

Code:
Data_Center_costing=205Financial_loss=45operational_cost=78

You could try


Code:
awk 'NR==FNR{A[$1]=$2; next} $1 in A{$2=A[$1]}1' FS=\\= OFS=\\= modfile file > newfile &&mv "newfile" "file"
Just closed the array A seems to be missed in Scrutinizer's solution.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 12  
Old 04-28-2014
As you see i just recently finished it:
Here's a function (script) to write values to provided variable in a file.

Code:
tui-value-set -?

tui-value-set (0.1 - BETA)
Save values to conf files
Usage: 		tui-value-set [options] FILE VALUENAME VALUE
Examples:	tui-value-set -l  /etc/default/grub
		tui-value-set     /etc/default/grub GRUB_TIMEOUT 15	| Saves VALUENAME as passed (VALUENAME)
		tui-value-set -ic /etc/default/grub grub_timeout 15	| Saves valuename as VALUENAME
		tui-value-set -i  /etc/default/grub grub_timeout 15	| Saves valuename as passed (valuename)
		tui-value-set -is /etc/default/grub GRUB_TIMEOUT 15	| Saves VALUENAME as valuename
		
Where options are:
	-h|--help		This screen
	-i|--irrelevant		Is no longer case sensitive
	-l|--list		Lists values available in the file
	-c|--caps		Writes the VALUE, and stores VALUENAME in capitals
	-s|--small		Writes the VALUE, and stores VALUENAME in lower chars

Code:
cat $(which tui-value-set )
#!/bin/bash
#
#	Author: 	Simon Arjuna Erat (sea)
#	Contact:	erat.simon@gmail.com
#	License:	GNU Lesser General Public License (LGPL)
#	Created:	2014.04.24
#	Changed:	2014.04.24
	script_version=0.1
#	Description:	Set a value in an 'conf/ini' file
#
#	Variables
#
	#set -x
	#source tui
	TITLE="Save values to conf files"
	ME="${0##*/}"
	ME_DIR="${0/${0##/*/}}"
	help_text="
$ME ($script_version - BETA)
$TITLE
Usage: 		$ME [options] FILE VALUENAME VALUE
Examples:	$ME -l  /etc/default/grub
		$ME     /etc/default/grub GRUB_TIMEOUT 15	| Saves VALUENAME as passed (VALUENAME)
		$ME -ic /etc/default/grub grub_timeout 15	| Saves valuename as VALUENAME
		$ME -i  /etc/default/grub grub_timeout 15	| Saves valuename as passed (valuename)
		$ME -is /etc/default/grub GRUB_TIMEOUT 15	| Saves VALUENAME as valuename
		
Where options are:
	-h|--help		This screen
	-i|--irrelevant		Is no longer case sensitive
	-l|--list		Lists values available in the file
	-c|--caps		Writes the VALUE, and stores VALUENAME in capitals
	-s|--small		Writes the VALUE, and stores VALUENAME in lower chars
"
	OPT=""
	VALUE=""
	CONFFILE=""
	VARNAME=""
	CAPS=false
	SMALL=false
#
#	Catching Arguments
#
	[[ -z $2 ]] && printf "$help_text" && exit $RET_HELP
	while getopts "i(irrelevant)c(caps)s(small)l(list)h(help)": name
	do 	case $name in
		c|caps)
			CAPS=true
			SMALL=false
			;;
		s|small)
			SMALL=true
			CAPS=false
			;;
		i|irrelevant)
			OPT="-i"
			;;
		l|list)
			grep -v ^# "$2"|grep "="|sed s,"="," ",g|awk '{print $1}'
			exit 0
			;;
		h|help)
			printf "$help_text"
			exit $RET_HELP
			;;
		esac
	done
	if [ -z $OPT ] 
	then 	CONFFILE="$1"
		VARNAME="$2"
		VALUE="$3"
	else 	CONFFILE="$2"
		VARNAME="$3"
		VALUE="$4"
	fi
	
#
#	Display & Action
#
	SEARCH=$(grep $OPT "$VARNAME" "$CONFFILE"|grep -v ^#)
	$SMALL && VARNAME="${VARNAME,,}"
	$CAPS  && VARNAME="${VARNAME^^}"
	REPLACE="$VARNAME=\"$VALUE\""
	
	# Set proper SED 'divider'
	SD=","
	printf "$REPLACE"|grep "," && SD="/"
	printf "$REPLACE"|grep '\\' && SD="/"
	printf "$REPLACE"|grep "/" && SD="\\"
	
	[ -z "$SEARCH" ] && \
		printf "${REPLACE}\n" >> "$CONFFILE" || \
		sed s${SD}"$SEARCH"${SD}"${REPLACE}"${SD}g -i "$CONFFILE"
		#sed s\\"$SEARCH"\\"${REPLACE}"\\g -i "$CONFFILE"

Could use like:
Code:
tui-value-set "/pathto/file" "VARIABLE_NAME" "newvalue"

This script is part of my TUI (Text User Interface), but since its no interface function, it doesnt use any 'shared' variables, so i can easy share.

Note i'm no expert, but for 'simple' values such as regular strings/numbers it should work quite well.

Hope this helps
# 13  
Old 05-01-2014
You never told us what operating system you're using (and it does matter)! We know that you're not using a Linux system, but that still leaves several possibilities.

You haven't commented on whether or not Scrutinizer's awk script works for you (and if you're on a Solaris/SunOS system, it won't).

If you want to build the variable assignments to be changed and the new value into the script (instead of in a separate file), you could try something like:
Code:
#!/bin/ksh
awk '
BEGIN {	FS = OFS = "="
	c["Data_Center_costing"] = 205
	c["Financial_loss"] = 45
	c["operational_cost"] = 78
}
$1 in c {
	$2 = c[$1]
}
1' input > output$$ && mv output$$ input

You can add as many additional fields to be changed to the c[] array in the BEGIN clause as you want.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.

I tested this using the Korn shell, but it will work with any shell that accepts basic Bourne shell syntax.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies

2. Shell Programming and Scripting

Change numbers

Hallo This is the content of the file 3 4 5 6 7 8 9 10 11 12 And I want the following output 1 2 3 4 5 6 7 (4 Replies)
Discussion started by: thailand
4 Replies

3. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

4. Shell Programming and Scripting

Change numbers in flat file

Hi, I have some datatexts with values and I want to replace only the values of the first file with the result of array loaded in one variable. Also I want the operation difference with the original value replaced and only replace the new value not the character format of the file. Some Idea?... (24 Replies)
Discussion started by: faka
24 Replies

5. Shell Programming and Scripting

Regarding change in column numbers after some commands

Hi All, I was using some commands to: replace a column by a constant string character awk -v a=CA 'NF>1{ $3=a; print; } ' $line>$line"_1" to copy a column and paste it in another place awk '$5=$2" "$5' $line>$line"_2" to delete the extra columns awk '{for(i=1;i<=NF;i++)... (9 Replies)
Discussion started by: CAch
9 Replies

6. Shell Programming and Scripting

Change numbers in a file, incrementing them

Hello, Here's a file of mine: key1:431 key2:159 key3:998 I need to change these keys to something bigger - and I actually need to shift them all by a range of 3. The output would be: key1:434 key2:162 key3:1001 I can't find the propper sed/awk line that would alter all my... (4 Replies)
Discussion started by: fzd
4 Replies

7. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. AIX

how do I change major-minor numbers of disk devices

Good evening ... does anyone of you know how to change major/minor numbers of disk devices ? I had to migrate from raid1 to raid5 and this messed up my ASM cluster - I know which devices should have which IDs to match the content - but I have no idea how to change it. Any help would be... (2 Replies)
Discussion started by: zxmaus
2 Replies

10. Shell Programming and Scripting

script to change filename with numbers

ok, this one is definitely too hard for my shell-script-skills. Hopefully, there is somebody who can help me with this: I have a folder with files in it named 0.ppm 10.ppm 2.ppm ... 5.ppm 50.ppm 55.ppm ... 355.ppm 360.ppm etc. As you will notice, the order in which the files are... (5 Replies)
Discussion started by: silversurfer202
5 Replies
Login or Register to Ask a Question