Sponsored Content
Top Forums Shell Programming and Scripting replacing field in specific line in a file Post 302447936 by vytenis on Tuesday 24th of August 2010 06:01:38 PM
Old 08-24-2010
Question replacing field in specific line in a file

Hi,

I know there are lots of threads on replacing text within files, usually using sed or awk. However, I find it hard to adapt examples that I found to my specific case. I am kind of new to UNIX and have hard times learning the syntax either for sed or awk so I would appreciate any help. Here's what I want to do:

I have a file containing user information

PIN000:FirstName.LastName:AccountNumber:Balance

I use cut to retreive Balance and place it into a variable and do calculations with it, but I don't know how to replace the Balance in the file above with new Balance that I get.

For example,
withdrawsum=number
Balance=&(($Balance - $withdrawsum))

then I want to replace the Balance part of the file so that when I exit the script, the changes remain and the next time I try to read Balance from the user file, I get the changed one.

For example,

user file:

PIN123:John.Smith:10203040:1000

so the Balance=1000

lets say that

withdrawsum=10

So once I've done the subtraction from the balance,

Balance=$(($Balance - $withdrawsum))

I want the new balance to be overwritten in the old balance place, so the file would look like this:

PIN123:John.Smith:10203040:980

Any thoughts? Thanks in advance for your help.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing the last field of a line.

Hi, I wrote a script which extracts data from 2 tables (joining the tables together) and outputs the fields to a csv file. the output may look something like scenario 1: a,b,c,d,1,2,3,4 or scenario 2: a,b,c,d,,,, now, in the second scenario, there are some empty fields at the end of... (3 Replies)
Discussion started by: Darek
3 Replies

2. UNIX for Dummies Questions & Answers

replacing string in a column on a specific line

hi, i currently have a file with columns similar to this customer name owed CID123 John 300 CID342 harry 500 at present i use use awk to find the amount owed by the customer using the customer ID (CID). if the customer spends more money how would i go about using sed/awk etc to... (2 Replies)
Discussion started by: skinnygav
2 Replies

3. Shell Programming and Scripting

How to append a character to the last but one field on a specific line?

Hi Guys, I have a file like this: aaa b c d e f fsss g h i k l qqq r t h n I want: aaa b c d e f fsss g h i k l qqq r t h , n ggg p t e d u qqq i o s , k (2 Replies)
Discussion started by: npatwardhan
2 Replies

4. Shell Programming and Scripting

SED Replacing all but one regex match on a line or specific matches

Hi, I'm attempting to rename some files that have spaces in them. Without linking sed commands together is it possible to replace the first three "." to " ". File.name.is.long.ext -> File name is long.ext I can get the desired effect with echo "File.name.is.long.ext" | sed 's/\./ /g;s/... (5 Replies)
Discussion started by: vectox
5 Replies

5. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

6. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

7. Shell Programming and Scripting

Update specific field in a line of text file

I have a text file like this: subject1:LecturerA:10 subject2:LecturerA:40 if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example. The... (6 Replies)
Discussion started by: bmtoan
6 Replies

8. Shell Programming and Scripting

Replacing nth field with nth_text for each line in a file

Hi All, I am very new to shell scripting and tried to search this in the forum but no luck. Requirment: I have an input file which is comma separated. I need to replace the value in 4th column with another value. This has to happen for all the lines in the file. Sample data: Input... (2 Replies)
Discussion started by: arunkumarsd
2 Replies

9. Shell Programming and Scripting

Replacing entire fields with specific text at end or beginning of field

Greetings. I've got a csv file with data along these lines: Spumoni's Pizza Place, Placemats n Things, Just Lamps Counterfeit Dollars by Vinnie, Just Shades, Dollar StoreI want to replace the entire comma-delimited field if it matches something ending in "Place" or beginning with "Dollar",... (2 Replies)
Discussion started by: palmfrond
2 Replies

10. UNIX for Beginners Questions & Answers

How do you preserve the max length of a line after replacing a specific value?

I'm new with scripting and I can't seem to figure out what I should do to get the output that I want. My file content would be below. ID2|ID3 |ID4|ID5 | I'm trying to replace the field of ID3 which has a fixed length of 10 characters, for each entry I have placed on a .txt file... (5 Replies)
Discussion started by: asdfghjkl
5 Replies
BALANCE(1)						      General Commands Manual							BALANCE(1)

NAME
balance 3.42 - A simple TCP proxy with load balancing and failover mechanisms. SYNOPSIS
balance [ -b addr ] [ -B addr ] [ -t sec ] [ -T sec ] [ -adfpHM ] port host1[:port1[:maxc]] [!|%] [ ... hostn[:portn[:maxc]]] balance [ -b addr ] -i [ -d ] [ -M ] port balance [ -b addr ] -c cmd [ -d ] [ -M ] port DESCRIPTION
Balance is a simple, generic "userland" TCP proxy, which allows simple round-robin load balancing and graceful failover between several destination servers. Balance supports IPv6 on the listening side which makes it a very useful tool for IPv6 migration of IPv4 only services and servers. Balance is available at http://balance.sourceforge.net. Definitions: A possible destination consisting of a host address and a port is called a "channel". A channel is member of a "channel group". Channels are numbered in a group starting with 0. Groups are numbered starting with 0, which is the initial default group. Balance accepts connections on the given port and forwards them to the supplied channels. At least one channel (in the default group) must be specified. If there are two or more channels specified in a group balance performs a simple round-robin load balancing between the channels. Balance allows the definition of further channel groups. The connection scheme works as follows: balance tries first to establish a connec- tion to a channel in the first group (0), performing the standard round-robin load balancing scheme. If no channel in this group is avail- able, balance proceeds with the next higher channel group. Groups are simply separated with a "!" at the command line at startup and can be controlled interactively with the "group" command. A "%" instead of a "!" as a group separator declares the previous group to be of type "hash". This means that instead of a round-robin algorithm, a hash distribution based on the client ip address is used to determine the destination channel. This allows connecting one client always to the same server (e.g. balancing http sessions to a single server). Hosts may be specified either by hostname or by IP address. Ports may be specified either by name (as listed in /etc/services) or numeri- cally. If no port is specified in a destination, the destination port defaults to the source port that balance controls. Balance allows the specification of the maximum number of connections per channel. This parameter can be optionally added after the port specification separated by a colon (":"). If a maximum number of connections is specified a channel will only be used for this maximum num- ber of simultaneous connections. A maxc value of 0 denotes an unlimited number of connections. This is the initial default value of a chan- nel. The maximum number of groups and channels balance can handle is specified at compile time and is initially 16 channels in 16 groups. Failover to another destination (a "channel") occurs if the connection is refused on the current channel or if the connect timeout is reached trying to establish a connection. If all possible destinations (channels) currently fail, the client connection to balance is closed. Balance accepts the following options: a Enable autodisable option: A channel needs to be manually re-enabled after a failure. b Bindhost: Balance binds to the specified host (or address) for listen() instead to INADDR_ANY. B Bindhost: Balance binds to the specified host (or address) for outgoing connections (the connection will be initiated from this address). c Command: allows to send a command to the balance master process (see interactive mode) d Debug: Balance outputs debugging and tracing information messages on stderr. H Hashfailover: Balance does failover to next node even if hash is used. F Foreground: tells balance to stay in foreground. This might be useful for testing and debugging since balance can be stopped in that mode using ^C (or other interrupt character). M Use memory mapping for IPC instead of shared memory i Interactive Control: Balance connects to the running instance defined by local port and bind address via shared memory and allows to control the behaviour of it using a command line interface. The access permission using this interface are determined by the access restrictions of the shared memory segment in effect. help or ? prints out a short command overview, create allows to establish a new destination definition (channel) consisting of host and port in the current group, disable disables a channel in the current group, enable enables a channel again in the current group, group changes the current group in interactive mode where all following commands are targeted, hash changes the current group to be of type "Hash", help prints out online help informations, kill shuts down the master process and exits interactive mode, maxc <channel> <maxc> sets the maximum number of connection ot the channel (0 means infinite), mrtg-bytes <group> <channel> prints out the bytes received/sent in MRTG compatible format (intended to be called with -c automatically by MRTG), mrtg-conns <group> <channel> prints out the total connections in MRTG compatible format (intended to be called with -c automatically by MRTG), quit exits the interactive mode, reset resets the byte counters of a channel, rr changes the current group to be of type "Round Robin", show shows an overview and the status of all channels including the incoming and out- going transfer volume in bytes. The output is sorted by groups. Additionally the current connections (c) and the maximum allowed connections (maxc) are printed, version prints out the version and MAXGROUPS and MAXCHANNELS constants at compile time. p Packetdump: Balance shows all incoming and outgoing data on stdout using a simple always readable external representation of data. This might be useful for debugging and protocol analysis. t Connect Timeout: the default timeout trying to establish a connection to any destination can be changed using this option. The default timeout after which a destination is regarded to be currently inaccessible is 5 seconds. T Select Timeout: Timeout for select(), default = 0 (never). This feature is currently untested. EXAMPLES
$ balance smtp host1.test.net host2.test.net Connection to the local SMTP port will be forwarded alterating to the SMTP port on host1 and host2. Balance runs automatically in background. $ balance -b 2001:DB8::1 80 10.1.1.1 10.1.1.2 Balance binds on port 80 of the local IPv6 IP address 2001:DB8::1 and distributes connections to the IPv4 addresses 10.1.1.1 and 10.1.1.2. $ balance -fp imap mailserver Connections to the local IMAP port will always be forwarded to the host "mailserver". Balance stays in foreground and all data is printed in readable format on stdout. $ balance -f 8888 host1 10.1.1.1:8000 Connections to the local port 8888 are forwarded alternating to host1, port 8888 and the host 10.1.1.1, port 8000. Balance stays in foreground connected to the "controlling tty". $ balance imap mailserver1::16 ! mailserver2 Two groups are specified, each containing one channel member. First up to 16 simultaneous connections are forwarded to "mailserver1". As soon as they are consumed, balance proceeds with the next group (1) which will consume all remaining connections forwarding them to the imap ort on "mailserver2". $ balance pop3 host1 host2 host3 ! failover1 Balance does round robin load balancing for the three hosts in the default group 0 for pop3 services. If all three hosts in group 0 fail, all connections are then forwarded to the host "failover1". $ balance telnet target.munich.net::1 Here balance is used to restrict all connections to exactly one at a time forwarding the telnet port. $ balance 8888 localhost::12 ! localhost::4 ! localhost::2 localhost::2 ! localhost:25 This is a simple test, forming 5 groups where balance is self referencing its own services 20 times. This is simply a test which definitely can be tried at home. BUGS
In case that balance is not able to forward the connection to any destination the inital connection to balance is always first accepted and then closed again immediately. This is not in every case the behaviour that would have been seen directly on the destination host. AUTHOR
Thomas Obermair, Inlab Software GmbH (obermair@acm.org) Copyright (c) 2000-2007,2008 by Thomas Obermair (obermair@acm.org) and Inlab Software GmbH (http://www.inlab.de), Gruenwald, Germany. All rights reserved. Balance is released under the GNU GENERAL PUBLIC LICENSE, see the file COPYING in the source code distribution. 2008/04/08 BALANCE(1)
All times are GMT -4. The time now is 12:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy