aspose file tools*
The moose likes Linux / UNIX and the fly likes using sed to chnage data in file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "using sed to chnage data in file" Watch "using sed to chnage data in file" New topic
Author

using sed to chnage data in file

minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
I have data like as shown below

2233|a.k. shukla|g.m.|6000
9876|jai sharma|director|7000
5678|sumit chakrobarty|d.g.m.|6000
2365|barun sengupta|director|7800
5423|n.k.gupta|chairman|5400

I want to change data permnantly in the file.
for example I want to change salary for employee id "5678" to "10000"

Which command should I try to do so.

I tried sed, but output does not reflect to the file.


Minal Silimkar
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29244
    
139

Minal,
Can you show the sed command you used? Maybe you were missing an option? For example, GNU's sed command uses the "i" option for in place search/replace. If your sed has no such option, you could use pipes.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
I tried command given below


but it changes all the places where pattern "6000" matches.

I want to change salary only for empcode "5678".

I tried


What should I do now?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29244
    
139

Minal,
You need to do this in one step. Piping will remove lines from your file because only lines matching the first reg exp will go on to the second statement. You are getting the error because sed is smart enough to know it can't do an "in-place" substitution from a pipe. It needs an actual filename for that. You can try creating a regular expression that matches the whole line you are looking for. You can use \1 to replace just the 6000 part.

I think this might be easier to do in awk than sed though. Something like:
awk -F "|" '{ if ( $1 == 5678) print $1"|"$2"|"$3"|"10000" }' yourFile

I don't have access to a UNIX system, so the syntax might be a bit off. This is the idea though.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

I agree with Jeanne in that this has to be done in one step and that it is easier with another language such as awk.
Originally posted by Jeanne Boyarsky:
I think this might be easier to do in awk than sed though. Something like:
awk -F "|" '{ if ( $1 == 5678) print $1"|"$2"|"$3"|"10000" }' yourFile

I don't have access to a UNIX system, so the syntax might be a bit off. This is the idea though.


Since I do have access to a Unix system, I can suggest the following changes:

Basically: adding an else statement to the if, and adding a missing quotation mark around the 10000.

A simple sed solution might be to use a regex matching syntax to work out what line to apply a given change to. E.g.:

Note the carat (^) states that the line must begin with 5678 for the match to work. Since there is only one instance of 5678 in the file it may not be needed - you choose.

As Jeanne suggests, using a regular expression with the \1 syntax to identify which field(s) are to be left alone so you only change one field will make this more accurate:

Who says sed is hard to read?

Basically in that case I have 3 regular expressions in the substitution command: the first which will match any numeric; the second that will match any alphabetic characters or the special characters '|', '.', or ' '; and the third which will match any numeric. Note that the second regular expression is very greedy - it matches the name, the position, and the delimiters all around both fields. I then output the first match (\1), the second match (\2) and the magic replacement (10000). By the way - sed only remembers 9 matches to regular expressions, which is how I was able to get away with having \210000 - there was no way for sed to get confused about whether I wanted the 21st match or the 210th match ...

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29244
    
139

Originally posted by Andrew Monkhouse:
Basically: adding an else statement to the if, and adding a missing quotation mark around the 10000.

Thanks Andrew. Nothing like running code to see all it's shortcomings
minal silimkar
Ranch Hand

Joined: Nov 25, 2007
Posts: 133
Originally posted by Andrew

sed '/^5678/ s/6000/10000/' emp.lst


The following command made the permannant changes in my file (emp.lst)



Thank you Jeanne and Andrew for your guidence.
[ May 26, 2008: Message edited by: minal silimkar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using sed to chnage data in file
 
Similar Threads
Inserting newline character
Use of SED in shell scripts
Command to add lines to multiple files
Replacing a word in a text file
Using variables in awk