Re: data manipulation in the file (544 Views)
Reply
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 1 of 21 (544 Views)
Accepted Solution

data manipulation in the file

How can i replace data in a file without opening it?
I want the 5th,6th and 7th character in a file to be replaced by a string
Can anybody help me??
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 2 of 21 (544 Views)

Re: data manipulation in the file

Hi,
and welcome to the forums !
What do you mean by "without opening" ?
In order to read the data you have to open the file!

You can read this earlier thread with a similar problem:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1107862

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 3 of 21 (544 Views)

Re: data manipulation in the file

Hi Peter,
Thank u so much for the welcum
and bout the query..
the link dint help.
here i dont know wat these 5th, 6th and 7th characters are, i just want them replaced

and what i meant by saying without opening the file is that, if u open a file in any editor, its not a prob to replace a character
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 4 of 21 (544 Views)

Re: data manipulation in the file

Hi,
if you want to change 5-7 char of each line:
perl -ple 'substr($_,4,3,"abc")' g.lis > h.lis

Example:
input (g.lis)
12399456
12398456
output (h.lis)
1239abc6
1239abc6
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 5 of 21 (544 Views)

Re: data manipulation in the file

Hi,
or if you only want this to happen on the first line of the file:

perl -ple 'substr($_,4,3,"abc") if $.==1' g.lis > h.lis
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 6 of 21 (544 Views)

Re: data manipulation in the file

Peter,
am really sorry.. i forgot to say i need the solution in shell scripting terms..
since i dont understand perl.
pls
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 7 of 21 (544 Views)

Re: data manipulation in the file

Hi,
in that case the whole things gets a bit more complex. Can you please provide an example line of input, as we need to know if the data is split into fields etc.

Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 8 of 21 (544 Views)

Re: data manipulation in the file

If you want to replace columns 5-7 of every line, you can use sed:
$ sed -e 's/^\(....\).../\1ABC/' file

Where ABC is your replacement string. It can be longer or shorter. "\1" puts back the first 4 chars.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 9 of 21 (544 Views)

Re: data manipulation in the file

Hi,
if sed is ok by you, could you please complete the thread by awarding points to helpful answers and summarising the solution for you. Otherwise please continue the thread.

http://forums1.itrc.hp.com/service/forums/helptips.do?#33 shows how to reward any useful answers given to your questions.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 10 of 21 (544 Views)

Re: data manipulation in the file

I don't think that you can modify data in a file without opening it (even read only, and dumping a modified copy of its content).
When you use those shell redirections, as shown, the shell is doing the opening (and possibly clobbering) for you by system calls behind the scenes.
You could verify by attaching a syscall tracer like tusc, truss, or strace to your processes...
Madness, thy name is system administration
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 11 of 21 (544 Views)

Re: data manipulation in the file

Hi,

>>
since i dont understand perl.
<<

That does not matter, IMHO: There may be solutions in other languages, you won't understand, as well.

We do not have your answer yet: Is the change of these characters required once in that file or in every line of it?
More questions:
- What about lines with less than 7 characters?
- Will newline charactes count?

All line change:
nawk -v str=NEW '{print substr($0,1,4)""str""substr($0,8)}' infile

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 12 of 21 (544 Views)

Re: data manipulation in the file

>Peter: What do you mean by "without opening"?
>Ralph: I don't think that you can modify data in a file without opening it

I can only assume that Reen@ meant the Windows open (run an editor or other magic program based on suffix) and not the nerd definition of open a file.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 13 of 21 (544 Views)

Re: data manipulation in the file

Hi:

Dennis wrote:

> I can only assume that Reen@ meant the Windows open (run an editor or other magic program based on suffix) and not the nerd definition of open a file.

Yes, of course. Unless you can *open* a file, how else can you expect any process to *read* a stream of octets to perform any match and/or substitution!?!

Depending upon the language, an implicit open() may be performed with a read() or write(), but that is purely under-the-covers slight-of-hand. The '-p' Perl switch is actually performing an open() for the file name(s) specified as arguments to the script so this too counts as an open(). Ditto for the 'sed' and the 'awk' solutions.

Regards!

...JRF...
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 14 of 21 (544 Views)

Re: data manipulation in the file

Ok..
I have attached the file.
I dont want the change to be done for all lines.
Only once i want the 5th,6th and 7th character to be replaced with say "AIR".
I have to do this change for many files. And these 3characters will be different for different files.
So, i wont know the characters to be replaced before hand itself.
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 15 of 21 (544 Views)

Re: data manipulation in the file

>I dont want the change to be done for all lines.

(Then you say you want it done for only the first line.)

$ sed -e '1s/^\(....\).../\1AIR/' file

This will create a new file, changing only the first line. You would need to save the output and rename it to the original.

for file in $*; do
sed -e '1s/^\(....\).../\1AIR/' $file > $file.new
mv $file.new $file
done

If the original is read-only, use "mv -f" above.


Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 16 of 21 (544 Views)

Re: data manipulation in the file

Dennis,
what does ^\(....\)... mean?
is it the range?
5th,6th and 7th character....how do i specify?:(
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 17 of 21 (544 Views)

Re: data manipulation in the file

>what does ^\(....\)... mean?

I mentioned that initially. \( & \) assigns a number for the pattern inside, to the first 4 chars, then selects the next 3. "." means it can be an arbitrary char.

Then it puts the first 4 back with "\1", then your replacement string "AIR".
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 18 of 21 (544 Views)

Re: data manipulation in the file

Hi all,
I got a simple solution for this.

cat file|cut -c5-7|read VAR1
sed "s/$VAR1/AIR/" file

will work out fine.
Thanks a lot for the help and patience.
I will do the further procedures and close the thread.:-)
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 19 of 21 (544 Views)

Re: data manipulation in the file

2 line command will work out
Acclaimed Contributor
Dennis Handly
Posts: 25,091
Registered: ‎03-06-2006
Message 20 of 21 (544 Views)

Re: data manipulation in the file

>cat file|cut -c5-7|read VAR1
sed "s/$VAR1/AIR/" file

This depends on columns 5-7 of the first record not appearing ANYWHERE in the rest of the file.

My script only changes that one line and only those columns. You can fix part of it by using "1s/$VAR1/AIR/" and change cat to "head -1".
Advisor
Reen
Posts: 18
Registered: ‎03-12-2007
Message 21 of 21 (544 Views)

Re: data manipulation in the file

Yes Dennis,
I will use head -1 option.
Thank u so much:-)
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.