Using sed to get text between a string and next line after another string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to get text between a string and next line after another string
# 1  
Old 05-30-2014
Using sed to get text between a string and next line after another string

Hi folks!

I'm trying to get a part of a text inside a text file (sudoers actually) but I'm having trouble.

Here is an example of a text:

Code:
Cmnd_Alias    DUMMY = /bin/ls /bin/car /usr/bin/whatever

Cmnd_Alias    TARGET = test test test test test \
                                 moretest moretest moretest \
                                 test test end

Cmnd_Alias    DUMMY2 = /dev/null /bin/bash /bin/tail \
                                /bin/sh /bin/ksh /bin/whatever

What I want to do is to get only the three lines of "TARGET" block (between DUMMY and DUMMY2).
I'm thinking to find some way to get the text between "Cmnd_Alias.*TARGET" and the first occurrance of a line ending different of "\$", but I don't know how to do this.

I've tried this, but it doesn't shows me the last line ("test test end"):

Code:
sed -n '/Cmnd_Alias.*TARGET/,/\\$/ p'

Does anybody have some idea?

Thanks a lot!
# 2  
Old 05-30-2014
Try shell's capabilty to process back slashes:
Code:
while read label name dummy content; do
  case $name in 
    (TARGET) printf "%s\n" "$label $name $content"
  esac
done < file

Or awk's ability to use multiple newlines as record separator..
Code:
awk '/TARGET/' RS= file

# 3  
Old 05-30-2014
Code:
sed -n '/TARGET/,/^$/ p' file

# 4  
Old 05-30-2014
Thanks for the reply, folks. But I figured out how to solve this using SED.
As the "end" line never ends with an special character (like \), I was able to do this:

Code:
sed -n '/^Cmnd_Alias.*target/,/[a-zA-Z 0-9]$/ p'

That works just fine. But now, I'm trying add a new parameter in the end of this text block. This code below worked just fine, but only on Linux:

Code:
sed -n '/Cmnd_Alias.*TARGET/ , /[a-zA-Z 0-9]$/ { /[a-zA-Z 0-9]$/ s/$/ newparameter/g; p}

Unfortunately, this works only with GNU SED. I've tried on Unix Solaris, and it gives me "Unrecognized command"...

Any adeas other than install GNU SED on Unix servers?
# 5  
Old 05-30-2014
Hi, do not use space around the comma with the range match.. /foo/,/bar/ and use a semi-colon before the closing brace : p;}



--
On Solaris when using awk, use /usr/xpg4/bin/awk on Solaris. You can also try /usr/xpg4/bin/sed to see if that makes a difference..
# 6  
Old 05-30-2014
Got perl?
Code:
perl -00 -wpl -e '/TARGET/ and s/$/ newparameter/;' file

# 7  
Old 05-30-2014
Scrutinizer
Great tip, buddy. Removing spaces and inserting ";" was enough, now is everything working on Linux and Unix.

Code:
sed -n '/Cmnd_Alias.*TARGET/,/[a-zA-Z 0-9]$/ { /[a-zA-Z 0-9]$/ s/$/ newparameter/g; p;}

Thanks, folks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Replace text/string with new line

Hello All I have had a requirement where I need to move data to a new line based on a text Input :61: 456 B66666 :61: 878 N78777 :61: 534533534 BNNN Output :61: 456 B66666 :61: 878 N78777 :61: 534533534 BNNN So basically as soon as it encounters :61: it should... (3 Replies)
Discussion started by: kamijia83
3 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

sed: how to replace string by another line

hi experts, i would like to do some html-file replacements and encounter the following problem: after cleaning up the file a bit i'd like to replace a regex with a reference and the content of another line. example: /<title>\(*\) - Wikipedia<\/title>/ should be replaced by \1 (the title... (2 Replies)
Discussion started by: chrisnie
2 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

9. Shell Programming and Scripting

GREP/SED - get string in a line

Hi, I simply try to get a string in a line but I do smth. wrong. Hopfefully you can help me ;) tried smth like: ggrep -Eo " /folder1/folder2/folder3/* end" get_info_file > temp.file I played a bit around but could not specify the end string command... So this is the... (9 Replies)
Discussion started by: unknown7
9 Replies

10. UNIX for Dummies Questions & Answers

grep a string in a line using sed

Hi, I'm trying to grep a string in a line using sed. My original data looks like this: MRTG$ grep -i "System" $file <H1>Traffic Analysis for 15 -- sERITHC3602.t-mobile.co.uk</H1> <TABLE> <TR><TD>System:</TD> <TD>sERITHC3602 in </TD></TR> <TR><TD>Maintainer:</TD> <TD></TD></TR>... (4 Replies)
Discussion started by: viadisky
4 Replies
Login or Register to Ask a Question