- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- sed command usage
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-07-2007 11:51 PM
тАО01-07-2007 11:51 PM
sed command usage
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.
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2007 12:06 AM
тАО01-08-2007 12:06 AM
Re: sed command usage
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2007 12:12 AM
тАО01-08-2007 12:12 AM
Re: sed command usage
# 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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2007 12:12 AM
тАО01-08-2007 12:12 AM
Re: sed command usage
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2007 12:21 AM
тАО01-08-2007 12:21 AM
Re: sed command usage
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-08-2007 12:59 AM
тАО01-08-2007 12:59 AM
Re: sed command usage
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-12-2007 10:13 PM
тАО01-12-2007 10:13 PM
Re: sed command usage
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-12-2007 11:31 PM
тАО01-12-2007 11:31 PM
Re: sed command usage
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