Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Update specific field in a line of text file

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

Viewing all articles
Browse latest Browse all 16232

Trending Articles