Update specific field in a line of text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Update specific field in a line of text file
# 1  
Old 01-13-2013
Blade Update specific field in a line of text file

I have a text file like this:

Code:
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 following works:
Code:
awk -F: '{if($1~sub&&$2~lec){$3=newMark}}1'  sub="subject1" lec="LecturerA" newMark=100 OFS=":" test.txt

But if I want to pass sub and lec to another variable from user input , it does not work:

Code:
sb="subject1"
lect="LecturerA"
mark=100

Code:
awk -F: '{if($1~sub&&$2~lec){$3=newMark}}1'  sub=$sb lec=$lect newMark=$mark OFS=":" test.txt

any idea?
Thanks.

Last edited by Scrutinizer; 01-13-2013 at 05:26 AM.. Reason: code tags
# 2  
Old 01-13-2013
You cannot use sub as a variable name since it is a built-in function in awk.

Last edited by Scrutinizer; 01-13-2013 at 05:40 AM..
# 3  
Old 01-13-2013
i can change to other variable names , but it still does not work..
# 4  
Old 01-13-2013
I tried to reproduce this using variable subj, but I got:

Code:
$ awk -F: '{if($1~subj && $2~lec){$3=newMark}}1' subj="subject1" lec="LecturerA" newMark=100 OFS=":" infile
subject1:LecturerA:100

subject2:LecturerA:40
$ b=subject1 lect=LecturerA mark=100
$ awk -F: '{if($1~subj&&$2~lec){$3=newMark}}1' subj="$sb" lec="$lect" newMark="$mark" OFS=":" infile
subject1:LecturerA:100

subject2:LecturerA:40

What is your OS and version?
# 5  
Old 01-13-2013
Oh i mean , if variable $sb and $lect contain string with while space ,example , sb="Java Program" and lect="Mr Dylan" , it does not work (mean mark still unchanged).

Code:
$ sb="Java Program"   lect="Mr Dylan" mark=100

$ awk -F: '{if($1~subj&&$2~lec){$3=newMark}}1' subj="$sb" lec="$lect" newMark="$mark" OFS=":" infile.txt

# 6  
Old 01-13-2013
in your original example there were no double quotes around the shell variable references:
Code:
awk -F: '{if($1~subj&&$2~lec){$3=newMark}}1' subj="$sb" lec="$lect" newMark="$mark" OFS=: infile.txt

That would be my guess, is the problem
# 7  
Old 01-13-2013
Thanks, double quote is the problem...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies

3. Shell Programming and Scripting

Perl to update field based on a specific set of rules

In the perl below, which does execute, I am having trouble with the else in Rule 3. The digit in f{8} is extracted and used to update f accordinly along with the value in f. There can be either - * or + before the number that is extracted but the same logic applies, that is if the value is greater... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 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

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... (5 Replies)
Discussion started by: vytenis
5 Replies

8. Shell Programming and Scripting

Update a specific line in a file while reading sequentially

All, I know this is a very naive question but I could not find a way to get this working! I have a file with values like input.file Value1 Value2 server1/mylogin,mypasswd Value3 Value4 And in my code, I am reading the file line by line and processing it. #! /bin/ksh... (6 Replies)
Discussion started by: bharath.gct
6 Replies

9. Shell Programming and Scripting

how to cut a field of a line in a text file?

Hi, I have text file which contains lines like : a/a/a/a/.project b/b/b/b/b/.project c/c/c/.project d/.project e/e/e/e/.project i want for all lines the last word .project should be removed and the file should look like : a/a/a/a/ b/b/b/b/b/ c/c/c/ .... how to proceed... (7 Replies)
Discussion started by: bhaskar_m
7 Replies

10. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies
Login or Register to Ask a Question