![]() |
|
|
|
|
|||||||
| 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 07: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 | Display Modes |
|
|||
|
hi,
am new to unix can any one help me to insert data to afile to a specified column. ie to an output file with csv format i should insert some data to 2,3,4 fileds and the data in those fields should move to next fields eg: output file asd,12,12,12,12,1,2,1,1,1,1,1 mnmn,1,12,1,1,1,112,12,1,1,1 insert data after asd 33,44 after mnmn 55,66 ie required o/p 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 |
| Forum Sponsor | ||
|
|
|
|||
|
Quote:
i mean, if u have 10 rows in the file u want to change, u want to insert data into all the 10 rows n in a sequential manner? |
|
|||
|
hi
i am getting an error as below insert: line 1: syntax error near unexpected token `(' insert: line 1: `nawk -F, -v OFS=',' -v asd = '33,44' -v mnmn = '55,66' '$1 == "asd" || $1 == "mnmn" '$1 == "asd" || $1 == "mnmn" { $1= $1 OFS (( $1 == asd ) ? asd : mnmn )}1' myFile.txt' |
|
|||
|
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 |
|
|||
|
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 UNIX.COM |
| Thread Tools | |
| Display Modes | |
|
|