The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Replacement for sed dbsurf Shell Programming and Scripting 0 01-25-2008 06:47 PM
Retrieve 5th Field to Last Field !! jobbyjoseph UNIX for Dummies Questions & Answers 3 05-16-2007 04:20 AM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 06:39 AM
add increment field when first field changes azekry Shell Programming and Scripting 2 11-14-2005 04:21 PM
cpu replacement. help IMPTRUE UNIX for Dummies Questions & Answers 3 07-25-2003 09:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-25-2008
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 138
field replacement

Hi,

I have a CSV file with around 1000 rows (lines) and 1000 columns (fields).
I need to replace mth lines's nth field with a value. (E.g., 400th field of 300th line). how can i do that?

Pl advise.

Thanks
Prvn
  #2 (permalink)  
Old 09-25-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,539
what have you tried?
  #3 (permalink)  
Old 09-25-2008
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 138
I tried extracting the fields other than to be replaced. Using "echo" i could again write to another file with new value.

It worked but looked very inefficient. i guess it can be easily achieved with "awk". Pl advise.


Prvn
  #4 (permalink)  
Old 09-25-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,539
show your code.
  #5 (permalink)  
Old 09-25-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
get the input from the user like lineno and columnno the rest is done by this awk
Code:
 
awk -F"," -v var1=$lineno -v var2=$columnno 'NR == var1{gsub(var2,"value")}{print $0}' filename
  #6 (permalink)  
Old 09-25-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Actually you could simply do

Code:
awk -F, -v line=$m -v col=$n 'NR == line { $col="value" }1' filename
  #7 (permalink)  
Old 09-25-2008
prvnrk prvnrk is offline
Registered User
  
 

Join Date: Jul 2007
Posts: 138
Thanks Vidhyadhar and era.

Era - when i used your solution, replacement done but "," also vanished.

#cat /tmp/test
1,2,3
4,5,6
7,8,9
#gawk -F, -v line="2" -v col="2" 'NR == line { $col="value" }1' /tmp/test
1,2,3
4 value 6
7,8,9
#

Vidhyadhar - when i ran your solution, o/p remains as acutal file contents (no replacements done).


Thanks
Prvn
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0