ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a record and need to update a particular field in that record.
Here is my code:
TargetFld=`echo TargetRec|cut -d',' -f3`
TargetFld=`sed -e s/cycle/$cycle/ $TargetLoc`
TargetRec=`echo $1|awk -F ',' '{print $1","$2","$TargetLoc","$4","$5","$6","$7","$8","$9","$10","$11}'`
As you can see i am just printing all fields one by one...
First of all is this correct approach?
Is there any better way to update the 3rd field in a record.
Is there any better way to update the 3rd field in a record.
Yes. Using the sub or the gsub function you can alter the content of a field. After having followed the advice by Tinkster, you can try something like:
Code:
'{sub($3,replacement,$3); print}'
In addition to the man page of awk suggested above, you can consult the GNU Awk User's Guide... a must-read!
This didnt wont as there is a problem with $week substitution.
if i give as below, it's working.
Newstr=`echo $str| awk -F ',' { sub($4/200901); print`
I heard that, in ksh and bash all the variables are global.
So, can't I use $week which is outside awk statement in the above awk statment.
Is this due to variable scoping.
And by the way, i didnt find "awk -v" option useful in my above problem.
I am sorry, am I missing something there...
Thanks for your responses guys.
Suresh
Last edited by suresh.chola; 01-14-2010 at 04:16 AM.
Sorry, Suresh, but your code is a bit unreadable and full of syntax errors. It's difficult to provide a good reply to your questions under these condition.
Please, use CODE tags to embed your code when posting (to do that you can switch to Advanced mode, then select the lines you want to embed and press the # (hash) button in the task bar at the top of the text box). This will also preserve indentation and spacing. Furthermore, try to copy/paste the lines from your terminal or from your script to avoid typos as possible.
It's because $week in awk is not the same variable as $week in bash.
You have to understand that awk has it's own, slightly different, variable system. In addition, even as a bash variable, the ' hard-quoting turns off substitution for it.
As tinkster said, you have to use the -v option (or careful quoting), in order to import the bash variable into the awk statement.
Unlike bash, awk variables are substituted by name only, with no '$' in front of them. When the dollar sign is included, awk will print the contents of the column (or whatever the substituted value refers to) from the input text. Using $wk in the above would make awk try to print the contents of column number 2009, for example.
Also note the (") soft-quoting of the -v value. Unlike single-quotes, double-quotes allow for bash variable substitution.
Finally, backticks (`) are discouraged in bash these days. The $() form is recommended as more robust and readable. See here.
Last edited by David the H.; 01-15-2010 at 03:10 AM.
Reason: fixed typo
David the H. already provided an exhaustive explanation (thanks). Just a little add-on to explain why you got a weird result in the second test.
The syntax of the sub function in awk is:
Code:
sub(regexp, replacement [, target])
as you can see you may pass two or three arguments. You chose to pass only two, so that the substitution is applied to the whole line (record in awk).
The regexp, that is the regular expression that matches the part of the string you want to substitute is $4 in your statement, that is "<year>".
The replacement is $week, that in awk code - as David pointed out - is a reference to a field. week in this case is a variable to which you didn't assign any value (inside the awk code), so that it retains a value of 0. Therefore, the resulting substitution is:
Code:
sub($4,$0)
which means substitute the text matching the fourth field with the whole record. The result is indeed:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.