What do you want exactly? To replace only the first occurrence of either `initrd' or `kernel' in the whole stdin text? Or simply replace `kernel' with `kernelXXX' in this particular variable? And what does NFR mean - may be it should be FNR (record number)?
It is important to specify we look for kernel or initrd. "initrd (UUID=... " or "kernel (UUID=..." .
Because there also can occur words like "unhide (UUID=" or "hide (UUID=" or similar.
It should not remove the title ... and other lines ...
In fact $block doesn't need to have the kernel or init attributes. It can be block for Linux or Windows with map command. So if no kernel or initrd in it, then save it untouched.
Products "/boot(UUID=" instead "/boot (UUID="
and "/boot" instead "/boot (UUID=eab515e9-"
Edit: I wanted to specify that I need to remove something (UUID=...) that follows the kernel or initrd word.
In my original code I tried to identify UUID format \(UUID=[-0-9a-f]*\ not to be confused with block list (hd?,?). (I am not sure if UUID can have big letters)
I think it is not lucky: sub(/[()]/,"",$2) it removes the parantheses whatever it it uuid od hd. Or Map! map (hd0) (hd1)
I want to ask you about this: r=$3$4; does it mean, you rely on IFS=$' '? How we get $3 and $4 ?
Sorry. It is always hard to me to explain what I work with and where I am going to. I always try to simplify things therefor I 1st publish the simplest form... To better understand. But I see this doesn't work and it is just on the contrary...
No worries - as said, best try to gather what you want to do with a complete example of the input (describing cases etc. with comments) and a complete example of the wanted output.
There is a lot of more people here in the forum which will be able to help for sure - having a break from work for today, see you tomorrow
It is good you showed me how to identify the 1st found result of (kernel|init).
Now I would need to access the content of parenthesis (.*) by some referencer.
I will do a remake. In this example I use //1 as a referencer (it is kust to show where I want to access the captured word).
I have this code:
/^(kernel|initrd)( +)(\(UUID=[-0-9a-f]*\)?)(.*)/ && f!=1 {f=1;
print "uuid"; print \\1;
next }
/^(kernel|initrd) / && f==1 {
next }1' f=0
Is some referencer to access the parenthesis? The FS - spaces and $1,$2,$3 referencer is a problem for me. I would better not use it. I don't know if the \\1 referencer is only for gensub? If I could use this I would use print \\1\\2\\3\\4 to print the content of parenthesis.
---------- Post updated at 08:54 PM ---------- Previous update was at 08:07 PM ----------
I have found interesting thread about storing data and backreferences . So I don't know if is it possible to do it in awk. Maybe if somebody would do it in perl.
Hi, here I am again. I have learned some basic about perl and I am bringing a solution. If zou know better way how to do it in perl, show it to me. I am newbie to perl
The text I work with 3 lines:
block='title Sata Mandriva\nkernel (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)/boot/vmlinuz BOOT_IMAGE=linux root=(UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c) resume=UUID=e12487ff-6d6f-44c4-9e03-33f545b3b798 splash=silent vga=788\ninitrd (UUID=eab515e9-bc3e-4024-9f01-55fddaa0fb1c)/boot/initrd.img'
Now it is on one line, but later I get it to array with 3 rows.
---------- Post updated at 10:23 PM ---------- Previous update was at 06:00 PM ----------
Step A) This code removes 1st UUID=... from the line with kernel (or initrd) and move it to newline as uuid UUID=...
1) I needed to recognize if the initrd or kernel was found - therefor the step A was done now.
2) If step A is done, then I do second part. I print the initrd without UUID and without "uuid"
B) I do increment $n++ for setting the array in which I have the lines.
C) ($1 eq "") is necessary to recognize if kernel or initrd was find. If not, then I need to put original line as value to the array.
block=$(
echo "$block" | perl -e '
$/="\\n";
chomp (@ia=<STDIN>);
foreach $i (@ia) {
$n++; @p[$n]=$i;
if ($q!=1)
{
$i =~ /^(kernel|initrd)( +)(\(UUID=[-0-9a-f]*\)?)(.*)/ ;
if ($1 eq "") { @p[$n]=$i; } else { # if it is not kernel nor initrd I have to set the original value for this line
$u=$3;
$q=1;
$a="$1 $2$4\n";
$u =~ s/[()]//g;
@p[$n]="\\nuuid $u\\n$a\\n";
}
} elsif ($q==1) { $q=2; }
if ($q==2)
{
$i =~ /^(kernel|initrd)( +)(\(UUID=[-0-9a-f]*\)?)(.*)/ ;
if ($1 eq "") { @p[$n]=$i; } else { # if it is not kernel nor initrd I have to set the original value for this line
$b="$1 $2$4\n";
@p[$n]="$b\\n";
}
};
}
print @p; $q=0;
'
);