Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-31-2004, 03:36 AM   #1
LQ Newbie
Registered: Aug 2004
Posts: 2

Rep: Reputation: 0


I have a text file as follows:

Line1....... 1111ABC
Line2....... 1134ABC
Line3....... 1149ABC
Line4....... 2140ABC

I just want to replace all ABC to DEF and increment all number by 1 if the number start with 11

The output file should be:

Line1....... 1112ABC
Line2....... 1135ABC
Line3....... 1150ABC
Line4....... 2140ABC

Can anyone tell me how to write a sed script to handle this case?


Last edited by glam1; 09-03-2004 at 08:37 AM.
Old 08-31-2004, 05:56 AM   #2
Registered: Oct 2003
Location: EU
Distribution: RH7.2 Mdk9.2
Posts: 39

Rep: Reputation: 15
I'm no sed guru but i see a problem with the increment operation. Afik sed doesn't do maths. And it's not just a matter of replacing 1 with2, 2 with 3,... , there is the question of the carry on.
My way out of that would be to use a scripting langage with regular expressions.
Here is my suggestion in Python:

# type the following to run the pgm:
# python inputfile_name

import sys, os, re, string

def parse_file(lines):
    #outfile= open(output_filename, 'w')
    for nextline in lines:
        #print nextline
        mtch="^ *(Line[0-9]\.+) *([0-9]+)(.+)", nextline)    
        if mtch :
            #print mtch.groups()
            if len(mtch.groups()) < 3 :
                   raise 'badly formatted line'
            m2=  int(
            if str(m2)[0]=='1':
                m2 +=1
            if m3 =='ABC':
                m3 = 'DEF'
            print m1,' ',m2,m3
            #outfile.write(m1,' ',m2,m3)    # uncomment here and above to write to a file

def main():
    filename= sys.argv[1]
        f= open(filename, 'r')

        lines =f.readlines()
        st ="can't open file" + filename
        raise st


if __name__ == '__main__':

Old 08-31-2004, 12:28 PM   #3
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,724

Rep: Reputation: 434Reputation: 434Reputation: 434Reputation: 434Reputation: 434
OK, let's have some fun with shell scripting, another programming language that has regular expressions and that can do maths

sed 's/ABC/DEF/' ${1:?Missing argument, Usage: $0 file} | while line=$(line)
  number=$(expr "$line" : '[^1]*\(11[0-9]*\)DEF')
  prefix=$(expr "$number" : "\(.\{2\}\)")
  [[ "$prefix" = 11 ]] && echo $line | sed 's/'$number'/'$(expr $number + 1)'/' || echo $line


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
shell script problem on sed. chooi Programming 3 02-11-2006 12:35 AM
sed script help/ideas rjcrews Programming 2 07-30-2004 03:59 PM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 10:40 PM
sed & grep script? dolvmin Linux - Software 20 09-22-2003 07:30 AM
sed in small BASH script OhLordy Linux - General 1 08-29-2003 12:32 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:27 AM.

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