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 07: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 Display Modes
  #1 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Question help me to to insert data

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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-23-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,983
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Code:
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

Last edited by vgersh99; 10-23-2006 at 04:45 AM.
Reply With Quote
  #3 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Quote:
Originally Posted by babu@shell
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
clarify a few things...the data u'll be inserting will be in a file? n the no. of rows in that file is equal to the no. of rows in the file to which u wanna insert?

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?
Reply With Quote
  #4 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Location: Bangalore, India
Posts: 41
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
hi vgersh99


can u plz explain ur code? for a newbie, its difficult to understand the latter half of the code..
Reply With Quote
  #5 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
  #6 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
can u exp the code
Reply With Quote
  #7 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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'
Reply With Quote
  #8 (permalink)  
Old 10-23-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,983
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #9 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,403
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 (permalink)  
Old 10-23-2006
Registered User
 

Join Date: Oct 2006
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
hi

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

Join Date: Sep 2006
Posts: 1,403
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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 UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix export command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script rsync ftp scp recursive segmentation fault(coredump) sftp script snoop unix solaris change ip address stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


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


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

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101