LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

Rep: Reputation: 0
sed


Hi,

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?


Thank

Last edited by glam1; 09-03-2004 at 08:37 AM.
 
Old 08-31-2004, 05:56 AM   #2
cdlen
Member
 
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:

Code:
# type the following to run the pgm:
# python pgm_name.py inputfile_name

import sys, os, re, string

def parse_file(lines):
    #outfile= open(output_filename, 'w')
    for nextline in lines:
        #print nextline
        mtch= re.search("^ *(Line[0-9]\.+) *([0-9]+)(.+)", nextline)    
        if mtch :
            #print mtch.group(1),',',mtch.group(2),',',mtch.group(3)
            #print mtch.groups()
            if len(mtch.groups()) < 3 :
                   raise 'badly formatted line'
            m1=  mtch.group(1)
            m2=  int(mtch.group(2))
            if str(m2)[0]=='1':
                m2 +=1
            m3=  mtch.group(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]
    try:
        f= open(filename, 'r')

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

#---------------------------------------------------------------------------
#----------------------------------------------------------------------------

if __name__ == '__main__':
	main()

#----------------------------------------------------------------------------
 
Old 08-31-2004, 12:28 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

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

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


Reply


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


All times are GMT -5. The time now is 12:17 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration