1755782 Members
4597 Online
108838 Solutions
New Discussion юеВ

sed command usage

 
vthiru
Advisor

sed command usage

Hi,

I would like to insert a word or a character in a file exactly at 100th position or column in the first line. 100th position may also be a blank space.

Thanks in advance.
7 REPLIES 7
Peter Godron
Honored Contributor

Re: sed command usage

Hi,
and welcome to the forums.

please see:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1088879

sed '1 s/./a/100' a.lis > b.lis


Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
James R. Ferguson
Acclaimed Contributor

Re: sed command usage

Hi:

# perl -pe 'if ($.==1) {s/(.{100})(.*)/$1WORD$2/}' file

If you want to update the file "in-place" and preserve a backup copy as "file.old" do:

# perl -pi.old -e 'if ($.==1) {s/(.{100})(.*)/$1WORD$2/}' file

In either case change WORD to be what you want --- a single character or a "word". Only the first record of the file will be updated.

Regards!

...JRF...
vthiru
Advisor

Re: sed command usage

Thanks Peter.

But the command doesn't work if 100th column is a blank space or if the file contains only few characters. Please let me know how to include a character or a word at 100th column.

Thanks in advance.

Peter Godron
Honored Contributor

Re: sed command usage

Hi,
thanks for the feedback.
But the command doesn't work if 100th column is a blank space or if the file contains only few characters. Please let me know how to include a character or a word at 100th column.

I have tested my solution on a short datafile, with the first line being 10 chars long, and the space is not problem.

1234 67890 becomes 1234a67890 with sed '1 s/./a/5'

However it now appears your first line may be shorter than 100 characters (Is this correct ?) and what you want to happen in this case is to buffer the line up to 100 characters and then add your data.

Can you please confirm this and any other 'additional' requirement.
James R. Ferguson
Acclaimed Contributor

Re: sed command usage

Hi (again):

> But the command doesn't work if 100th column is a blank space or if the file contains only few characters. Please let me know how to include a character or a word at 100th column.

OK, then perhaps:

#!/usr/bin/perl -ni.old
if ( $. == 1 ) {
if ( length($_) < 100 ) {
chomp;
printf "%-100s%s\n", $_, "WORD";
}
else {
s/(.{100})(.*)/$1WORD$2/;
print;
}
next;
}
print;

...Once again, change WORD to whatever you want (in both places). This will update your file in-place and keep a backup as ("*.old").

Run as:

# .filter file

Regards!

...JRF...
vthiru
Advisor

Re: sed command usage

Thanks Peter and James. It worked!!
Peter Nikitka
Honored Contributor

Re: sed command usage

Hi,

since you are new to this forum - welcome here! - I wnat to put your attention to the points system:

http://forums1.itrc.hp.com/service/forums/helptips.do?#28

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"