The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
unable Insert data from .dat file to .xls can anybody help me kreddy2003 Shell Programming and Scripting 1 05-28-2008 02:33 AM
How to insert new line in the data file using the script Sona UNIX for Dummies Questions & Answers 2 08-21-2006 10:17 PM
find the position in a file and insert the data there isingh786 HP-UX 5 04-11-2006 06:31 PM
how to insert data in database based on text file? forevercalz Shell Programming and Scripting 9 12-20-2005 08:40 PM
sed, insert data from a file to another? ctcuser Shell Programming and Scripting 4 05-03-2005 10:43 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 10-23-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
sorry - bad copy/paste:
Code:
nawk -F, -v OFS=',' -v asd='33,44' -v mnmn='55,66' '$1 == "asd" || $1 == "mnmn"  {$1= $1 OFS (($1==asd) ? asd : mnmn)}1' myFile.txt
Reply With Quote
Forum Sponsor
  #9  
Old 10-23-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Python alternative

Code:
#!/usr/bin/python
import fileinput
for lines in fileinput.FileInput("input.txt", inplace=1): #inplace edit
 	a = lines.strip().split(",")
 	if a[0] == "asd":
 		insertstring = ",33,44," 
 	if a[0] == "mnmn":
 		insertstring = ",55,66," 
 	print a[0] , insertstring , ','.join(a[1:])
output:
Code:
asd ,33,44, 12,12,12,12,1,2,1,1,1,1,1
mnmn ,55,66, 1,12,1,1,1,112,12,1,1,1
Reply With Quote
  #10  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Posts: 12
hi

thank u gostdog74
but i am a newer to unix
i don't know python
Reply With Quote
  #11  
Old 10-23-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Quote:
Originally Posted by ghostdog74
Python alternative

Code:
#!/usr/bin/python
1.import fileinput
2.for lines in fileinput.FileInput("input.txt", inplace=1): #inplace edit
3. 	a = lines.strip().split(",")
4. 	if a[0] == "asd":
5. 		insertstring = ",33,44," 
6. 	if a[0] == "mnmn":
7. 		insertstring = ",55,66," 
8. 	print a[0] , insertstring , ','.join(a[1:])
1. fileinput is a Python standard module for looping over files and processing them. So we import it into the script.
2. Here we loop over the lines in the file, using for loop. The "inplace = 1" argument says to perform modification to the file "without creating a temporary file", ie inplace editing.
3. Strip the line of newlines and then split them up using ',' as delimiter. Eg asd,12,12,12,12,1,2,1,1,1,1,1 will become ['asd', '12', '12', '12', '12', '1', '2', '1', '1', '1', '1', '1']. In Python , strings are immutable, do i use a list
4-7. to refer to elements of a list in Python, we use indexes. eg a[0] refers to the first element, which is "asd". The second element will be a[1] which is '12' and so on.
8. a[1:] means get from index 1 (element 2) till the end of the list.
",".join ( a[1:] ) means to join ['12', '12', '12', '12', '1', '2', '1', '1', '1', '1', '1'] back to a string 12,12,12,12,1,2,1,1,1,1,1 , using commas as delimiters.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0