![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#8
|
||||
|
||||
|
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
|
| Forum Sponsor | ||
|
|
|
#9
|
|||
|
|||
|
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:])
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 |
|
#10
|
|||
|
|||
|
hi
thank u gostdog74 but i am a newer to unix i don't know python |
|
#11
|
|||
|
|||
|
Quote:
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. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|