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 06:33 AM
How to insert new line in the data file using the script Sona UNIX for Dummies Questions & Answers 2 08-22-2006 02:17 AM
find the position in a file and insert the data there isingh786 HP-UX 5 04-11-2006 10:31 PM
how to insert data in database based on text file? forevercalz Shell Programming and Scripting 9 12-20-2005 11:40 PM
sed, insert data from a file to another? ctcuser Shell Programming and Scripting 4 05-03-2005 02:43 PM

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 10-23-2006
babu@shell babu@shell is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 12
hi

thanks napolean,

the inputs will be in afile and one more thing

the input we search for asd or mnmn and insert accordingly
this will takes place for earch search
  #2 (permalink)  
Old 10-23-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
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

  #3 (permalink)  
Old 10-24-2006
babu@shell babu@shell is offline
Registered User
  
 

Join Date: Oct 2006
Posts: 12
hi

thank u gostdog74
but i am a newer to unix
i don't know python
  #4 (permalink)  
Old 10-24-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
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.
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 12:34 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