Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-29-2008, 04:49 PM
|
#1
|
Member
Registered: Jun 2006
Posts: 126
Rep:
|
modify field
I have a huge pipe delimited file. And I need to modify only the fifth field (apply an if or case statment on it). How can we do that and still having other fields intact.
eg.
Before
File1
After
File1
Code:
F1|F2|F3|(F4>10? F4-1:F4+1) |F5
|
|
|
07-29-2008, 05:13 PM
|
#2
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
You can use awk or perl, setting the field separator to '|', and output the first four fields as is, and perform your calculations on field five.
Do all lines have exactly 5 fields?
|
|
|
07-29-2008, 05:23 PM
|
#3
|
Member
Registered: Jun 2006
Posts: 126
Original Poster
Rep:
|
not 5 fields, but say N fields and its the same for all the lines
|
|
|
07-29-2008, 05:37 PM
|
#4
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
awk -F'|' 'BEGIN {OFS="|"} /\|/ {print $1,$2,$3,$4,$5}' data_file
Now, you change $5 to be what you want.
|
|
|
07-29-2008, 06:48 PM
|
#5
|
Member
Registered: Jun 2006
Posts: 126
Original Poster
Rep:
|
Quote:
awk -F'|' 'BEGIN {OFS="|"} /\|/ {print $1,$2,$3,$4,`expr length $5` > 2 ?:$5}' data_file
|
I tried to modify the code as above... basically to check if the length of the fifth field is greater than 2, if it is then do nothing else retain the $5 field.
but the expression above gives awk syntax error.
Quote:
awk -F'|' ' -v f8=`expr length $5` BEGIN {OFS="|"} /\|/ {print $1,$2,$3,$4,f8>2?:$5}' data_file
|
The above statment as well fails due to awk syntax error .
|
|
|
07-29-2008, 07:00 PM
|
#6
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Why are you calling expr within awk ? awk is perfectly capable of determining the length of a string.
Here's the GNU awk manual.
http://www.gnu.org/manual/gawk/html_node/index.html
Spend some time actually reading it to learn about awk syntax and commands. I see from your other threads this is an ongoing issue, so its worth your time to learn.
Build one small awk statement at a time; when you get them working individually, then put them together. In the case above, try printing out the length of a string entirely from within awk.
Last edited by Mr. C.; 07-29-2008 at 07:03 PM.
|
|
|
07-29-2008, 08:27 PM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426
|
I note in your example you're actually modifying the 4th field, not the 5th... Or even 3rd field if you're talking offsets...
|
|
|
07-31-2008, 03:24 PM
|
#8
|
Member
Registered: Jun 2006
Posts: 126
Original Poster
Rep:
|
I brushed through awk a bit and went ahead with this code ... but it gives me syntax error. Tried to
Code:
awk -F'|' 'BEGIN {OFS="|"} {if (length($4)>2) $4="" else $4=$4; print $0}' data_file
|
|
|
07-31-2008, 07:06 PM
|
#9
|
Senior Member
Registered: Jun 2008
Posts: 2,529
Rep:
|
Remove the else $4=$4. Not only is it a no-op ($4 already equals $4 !), but the syntax is incorrect (you'd need a semicolon just before the else.
|
|
|
07-31-2008, 11:51 PM
|
#10
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
awk already has an "internal if", so in your case, there's no need for if/else
Code:
awk 'BEGIN{OFS="|";FS="|"}length($4)>2{$4=""}1' file
|
|
|
08-01-2008, 11:26 AM
|
#11
|
Member
Registered: Jun 2006
Posts: 126
Original Poster
Rep:
|
Thanks for the feedback.
|
|
|
All times are GMT -5. The time now is 04:22 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|