replacing field in specific line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing field in specific line in a file
# 1  
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.
# 2  
Old 08-24-2010
The problem is that file I/O has no "insert" operation: If the new data is a different length than the old you need to rewrite everything after the change.

There's way better, faster, and simpler ways than the 'cut' utility to read fields from a data file, but it's difficult to help without seeing the rest of your program. It'd also tell me what other fields from that line you have, since just knowing the balance isn't enough to find and edit that exact line...
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-24-2010
I would definitely like to hear about these 'better, faster and simpler ways'. As I said, I'm new to UNIX and there are plenty of stuff to learn so any advice would be appreciated. The whole script is ~100 lines, so I probably won't paste it here. But if you like to know what other fields of that line are used for, then here it is:

PIN000 - Three last digits are used as a password for user's account (I use PIN in front so the user's entry is more easily read, though I would like to encrypt this field in the future for security reasons.
FirstName.LastName - Used to identify the user.
AccountNumber - I don't use this one for now, but this is a fixed length number. I plan to use this number for transactions between users within the system.

Basically, it's just a simple script used to manage users' finances in a simple 'deposit' 'withdraw' manner. However, it's very limited as of now, cause all the changes done by the user are not saved into his/her user entry (that line in the file).
# 4  
Old 08-24-2010
Maybe something like this,
Code:
wd=10

awk -v wd="$wd" -F: '$NF=$NF-wd' OFS=: file > temp && mv temp file

Try it first on some test files.
This User Gave Thanks to rubin For This Post:
# 5  
Old 08-24-2010
Quote:
Originally Posted by vytenis
I would definitely like to hear about these 'better, faster and simpler ways'.
Here's the basic idea. Instead of creating and waiting for five seperate processes per line, you run read once. It's not even a process, just a builtin -- faster than awk/cut by a factor of 20 or so. And that's just if you're running awk/cut once per loop.
Code:
IFS=":" read PIN FIRST LAST ACCT BALANCE < filename

It reads data one line at a time from standard input(or whatever you redirect into standard input) and puts it into shell variables. It splits based on a special variable, IFS, which is usually space, but can be anything you want.

To loop through the whole file, you would do:

Code:
while IFS=":" read PIN FIRST LAST ACCT BALANCE
do
        # code to do something with the values here
done < filename

Now say you have a single line that you need to split into variables, but don't want to read it from file. Simple:

Code:
read A B C D E F <<< "words words flarg been words more words"

The <<< feeds it that txt on standard input without you having to create a file or pipe.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 09-01-2010
it worked! thanks for the help, people. Just another advantage of using Open-source OS, lots of great people contributing to teaching n00bs Smilie great OS, great community!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question