LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-29-2008, 04:49 PM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
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

Code:
F1|F2|F3|F4|F5
After

File1
Code:
F1|F2|F3|(F4>10? F4-1:F4+1) |F5
 
Old 07-29-2008, 05:13 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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?
 
Old 07-29-2008, 05:23 PM   #3
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
not 5 fields, but say N fields and its the same for all the lines
 
Old 07-29-2008, 05:37 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
awk -F'|' 'BEGIN {OFS="|"} /\|/ {print $1,$2,$3,$4,$5}' data_file

Now, you change $5 to be what you want.
 
Old 07-29-2008, 06:48 PM   #5
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
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 .
 
Old 07-29-2008, 07:00 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
Old 07-29-2008, 08:27 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
I note in your example you're actually modifying the 4th field, not the 5th... Or even 3rd field if you're talking offsets...
 
Old 07-31-2008, 03:24 PM   #8
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
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
 
Old 07-31-2008, 07:06 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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.
 
Old 07-31-2008, 11:51 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 08-01-2008, 11:26 AM   #11
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
Thanks for the feedback.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What field is best in Linux cj_cheema Linux - General 1 10-17-2007 06:18 AM
what does password field mean? hongxing Linux - Software 5 11-28-2005 07:33 PM
TOS field value lucs Slackware 1 03-31-2005 11:55 AM
How to modify the library path variable?modify the Electronkz Linux - Newbie 1 04-13-2004 07:18 AM
modify file access & modify timestamps i2itstud Linux - General 1 05-20-2003 04:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration