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

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

 




steve wrote:
> 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
>>> <snip>
>
Or, here's how to do it in a bash script with sed:

#!/bin/bash
#set -x
typeset -i b=0  n=0
 cat -n file1 | while read n f
  do
   echo "n="$n
   if [ "$f" = "BB" ]
    then
     b=$b+1
     echo "b="$b
     g=`sed -n "${b}p" file2`
     sed -i "$n s/$f/$g/"  file1
    fi
 done


echo statements in the above script are there only to show you what line
numbers it's using.  Here's what it's doing:

 The "cat -n | read" statement puts the line numbers and content of the
file1 in variables $n and $f
 Then test $f for BB, if found increment counter variable $b
 Use $b as the *input line number* for the replacement text in file2
 Set variable $g to that text using 'sed -n' of file2
 Then do an in-place replacement in file1 of the correct line using the
value of $n from the read.

Alternatively, instead of 'cat -n | read' you could do:

#!/bin/bash
#set -x
typeset -i b=0  n=0
for d in `grep -n BB file1`
 do
  n=`echo $d | awk -F: '{ print $1 }'`
  f=`echo $d | awk -F: '{ print $2 }'`
  b=$b+1
  g=`sed -n "${b}p" file2`
  sed -i "$n s/$f/$g/"  file1
 done

There's probably a more elegant way of setting the two variables $n and
$f with the awk statement but I'm not sure how to do it.

In any case, both of these scripts worked based on what you gave as the
contents of the two files (FWIW, I found that you can't just use 'cat
file1 | read' as the blank line in the input file get's ignored and thus
your line count get's f'd up.

Good luck.

Kevin

-- 
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