unable to use new line in sed search pattern.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unable to use new line in sed search pattern.
# 1  
Old 08-05-2010
unable to use new line in sed search pattern.

hi ,
my lilo.conf is as shown below :
Code:
prompt
    default=Primary
    read-only

image=/boot/bzImage
    label=Primary
    root=/dev/md0
    append="reboot=t md=0 ip=off panic=5 quiet console=ttyS0,115200n81"
    read-only

image=/boot/bzImage
    label=Recover
    root=/dev/sda3
    append="reboot=t ep=off panic=5 quiet console=ttyS0,115200n81"

image=/boot/bzImage
    label=SecRoot
    root=/dev/sda2
    append="reboot=t ip=off panic=5 quiet console=ttyS0,115200n81"

i am writing a tool which takes as input Primary/SecRoot/Recover, based on which i should replace the line "image=/boot/bzImage".

For example if i give SecRoot as input then

image=/boot/bzImage
label=SecRoot

should get replaced by

image=/secroot/boot/bzImage
label=SecRoot

I tried many options of sed, but could not correctly implement it. any help is greatly appreciated.
# 2  
Old 08-06-2010
Try this:
Code:
awk -v var="SecRoot" '
NR==1{s=$0;r="image=/" tolower(var);next}
$0 ~ var{sub("image=",r,s)}
{print s; s=$0}
END{print s}' file

These 2 Users Gave Thanks to Franklin52 For This Post:
# 3  
Old 08-06-2010
For Fanklin52

If you get a chance, could you explain that awk? I'm having a desperate time wrapping my head around how it's selecting the line before "label=SecRoot"
# 4  
Old 08-06-2010
It is working for me franklin.
Thanks a lot, for your help , could you please explain this command.

Last edited by success; 08-06-2010 at 07:45 AM..
# 5  
Old 08-06-2010
Quote:
Originally Posted by port43
If you get a chance, could you explain that awk? I'm having a desperate time wrapping my head around how it's selecting the line before "label=SecRoot"
Quote:
Originally Posted by success
It is working for me franklin.
Thanks a lot, for your help , could you please explain this command.
Sure.
Code:
awk -v var="SecRoot" '
NR==1{s=$0;r="image=/" tolower(var);next}
$0 ~ var{sub("image=",r,s)}
{print s; s=$0}
END{print s}' file

Explanation:
Code:
-v var="SecRoot"

Set variable var with the pattern.
Code:
NR==1{s=$0;r="image=/" tolower(var);next}

If line number is 1, assign the current line to the variable s, assign the new string to the variable r (image=/secroot) and read the next line.
Code:
$0 ~ var{sub("image=",r,s)}

If the current line matches with the pattern, substitute the previous line.
Code:
{print s; s=$0}

Print the previous line (variable s) and assign the current line to variable s.
Code:
END{print s}

Print the last line.
# 6  
Old 08-06-2010
Code:
# var="SecRoot" ; sed "/image=\/boot\/bzImage/{s/=\(.*\)$/=\/"$(echo $var|sed 'y/PRS/prs/')"\1/}" infile
prompt
    default=Primary
    read-only
image=/secroot/boot/bzImage
    label=Primary
    root=/dev/md0
    append="reboot=t md=0 ip=off panic=5 quiet console=ttyS0,115200n81"
    read-only
image=/secroot/boot/bzImage
    label=Recover
    root=/dev/sda3
    append="reboot=t ep=off panic=5 quiet console=ttyS0,115200n81"
image=/secroot/boot/bzImage
    label=SecRoot
    root=/dev/sda2
    append="reboot=t ip=off panic=5 quiet console=ttyS0,115200n81"

# 7  
Old 08-06-2010
Thank you, Franklin52!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

2. Shell Programming and Scripting

Search a pattern in a line and remove another pattern

Hi, I want to search a pattern in a text file and remove another pattern in that file. my text file look like this 0.000000 1.970000 F 303 - 1.970000 2.080000 VH VH + 2.080000 2.250000 VH VH + 2.250000 2.330000 VH L - 2.330000 2.360000 F H + 2.360000 2.410000 L VL - 2.410000 ... (6 Replies)
Discussion started by: sreejithalokkan
6 Replies

3. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

4. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

5. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

6. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

7. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

8. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I think you ppl did not get my question correctly, let me explain I have 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: ... (1 Reply)
Discussion started by: imas
1 Replies

9. UNIX for Dummies Questions & Answers

modify a particular pattern starting from second line of the search pattern

Hi, I am new to this forum and i would like to get help in this issue. I have a file 1.txt as shown: apple banana orange apple grapes banana orange grapes orange .... Now i would like to search for pattern say apple or orange and then put a # at the beginning of the pattern... (2 Replies)
Discussion started by: imas
2 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question