Re: sed, awk, or something...help meeeeeeeeeee

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 09/28/2009 11:18 PM, Joerg Bergmann wrote:
Am 28.09.2009 19:38, schrieb Kanwar Ranbir Sandhu:
 Hi All,

 I don't know if I should use sed for what I'm about to ask, but it seems
 like a good idea.  If awk is better, or something else entirely, that's
 fine too.

 I have two files.  File "1" looks like this:

 AA
 BB
 CC
 DD

 AA
 BB
 CC
 DD

 File "2" looks like this:

 BBBB1
 BBBB2
 BBBB3

 So, "BB" in file "1" always occurs in the same spot (i.e. between lines
 AA and CC).  Knowing that, how do I replace the first occurrence of "BB"
 in file "1" with "BBBB1" from file 2, the second occurrence of "BB" in
 file "1" with "BBBB2" from file 2, and so on?

 I think a bash 'for' or 'while' loop may be useful here, too.  But, it's
 the sed/awk/whatever bits I don't know how to do.  I've read some of the
 man/info page, looked up sed help on the net, etc.  I'm still not sure
 how to do the above with sed.

 Thanks in advance!

 Regards,

 Ranbir

Probably it's not what you want, but I would prefer writing a

50 lines standard C program for that task...

Seriously ?? ...no, really, seriously ?

I'd recommend using python.

something like:
--------------------------------------
file1_lines = open(file1).readlines()
file2_lines = open(file2).readlines()

modified_file_lines = []

for line in file1_lines:
    if line == 'BB\n':
        modified_file_lines.append(file2_lines.pop(0))
    else:
        modified_file_lines.append(line)

open("modified.out", 'w').write(modified_file_lines)
--------------------------------------

Of course, i've gone for clarity rather than efficiency here. If you understand that, I'll gladly show you how to write it more efficiently.

cheers,
- steve

--
random non tech spiel: http://lonetwin.blogspot.com/
tech randomness: http://lonehacks.blogspot.com/
what i'm stumbling into: http://lonetwin.stumbleupon.com/

--
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux