LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > AIX
User Name
Password
AIX This forum is for the discussion of IBM AIX.
eserver and other IBM related questions are also on topic.

Notices


Reply
  Search this Thread
Old 03-31-2010, 08:12 AM   #1
mufy
Member
 
Registered: Oct 2004
Location: Kuwait
Distribution: Currently - AIX | Previously - RHEL 4 ES, FC 10
Posts: 206
Blog Entries: 4

Rep: Reputation: 30
Modify file without opening in vi editor


Hey,

I have a single line (digits) file in which I need to replace, say, the 6th & 7th digits with some other values. How can I do that without actually opening the file - from the CLI for example?

I'm trying to incorporate this in a script so that I do not have to manually open the file to make the change.
 
Old 03-31-2010, 08:18 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
This should be quite straightforward using tools like SED and AWK.

Are you replacing based on position in the line or on some other criteria?

Show a sample of the file before and after the changes.
 
Old 03-31-2010, 08:22 AM   #3
mufy
Member
 
Registered: Oct 2004
Location: Kuwait
Distribution: Currently - AIX | Previously - RHEL 4 ES, FC 10
Posts: 206

Original Poster
Blog Entries: 4

Rep: Reputation: 30
Based on position and the value will be picked from `date +%m` and substituted using a variable.

Before:
20100301201003300000000000

After:
20100301201003310000000000
 
Old 03-31-2010, 08:35 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Ultra-crude solution:

Code:
[mherring@Ath ~]$ a=20100301201003300000000000
[mherring@Ath ~]$ echo $a|sed -r 's/(.{14})../\199/'
Matches the first 16 characacters on the line--replaces 1-14 in kind, and then inserts "99" for #15 and #16.

Same thing, but inserting from a variable:
Code:
[mherring@Ath ~]$ b=99
[mherring@Ath ~]$ echo $a|sed -r "s/(.{14})../\1$b/"
20100301201003990000000000
Note that the SED command string now has to be in double-quotes.
 
Old 03-31-2010, 08:43 AM   #5
mufy
Member
 
Registered: Oct 2004
Location: Kuwait
Distribution: Currently - AIX | Previously - RHEL 4 ES, FC 10
Posts: 206

Original Poster
Blog Entries: 4

Rep: Reputation: 30
This the error I'm getting:

Code:
# a=20100301201003300000000000
# echo $a|sed -r 's/(.{14})../\199/'
sed: Not a recognized flag: r
Usage:  sed [-n] Script [File ...]
        sed [-n] [-e Script] ... [-f Script_file] ... [File ...]
But the above works well on my RHEL4 box.

Last edited by mufy; 03-31-2010 at 08:47 AM. Reason: Add more comment
 
Old 03-31-2010, 09:30 AM   #6
bret381
Member
 
Registered: Nov 2009
Location: Alabama
Distribution: Arch x86_64
Posts: 650

Rep: Reputation: 79
ok, I am definately not a sed expert. just learning myself, but from what I understand the -r option is a GNU specific option and would not work on AIX. Not sure if eliminating the -r option would work on AIX though Maybe with the -e option??

Last edited by bret381; 03-31-2010 at 09:32 AM.
 
Old 03-31-2010, 12:11 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
find the manual for your version of SED. In GNU-BASH, if you don't use the -r flag, then you have to escape the () and maybe the {}.
 
  


Reply

Tags
vi



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command vi -w /etc/aliases is not opening in VIM editor kvijaik Linux - Newbie 6 02-02-2012 07:36 AM
opening files in text editor from the terminal campanula Linux - Newbie 4 03-08-2010 03:51 PM
LXer: Modify and extend the powerful Vim Scripting editor LXer Syndicated Linux News 0 02-03-2010 07:30 AM
How To Modify A File w/o Opening It carlosinfl Linux - General 9 09-23-2008 02:48 PM
modify file access & modify timestamps i2itstud Linux - General 1 05-20-2003 03:34 AM

LinuxQuestions.org > Forums > Other *NIX Forums > AIX

All times are GMT -5. The time now is 10:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration