error while replacing a string by new line character in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting error while replacing a string by new line character in sed
# 1  
Old 04-27-2009
error while replacing a string by new line character in sed

hi,

when i am doing the following things getting error
Can anyone please suggest

i have a file where there is a line like the following
branch=dev sdf dev jin kilii fin kale boyle dev james dev

i want to search the existance of dev in the above line.

cat "$file" | sed -n '/'$branch'p' | sed 's/'$name'/&\n/g' | grep $name |wc -l

where
$file=filename
$branch=line containing branch
$name=dev

while i am doing the above result is 1,but it should be 4

Please suggest
# 2  
Old 04-27-2009
Quote:
Originally Posted by millan
hi,

when i am doing the following things getting error
Can anyone please suggest

i have a file where there is a line like the following
branch=dev sdf dev jin kilii fin kale boyle dev james dev

i want to search the existance of dev in the above line.

cat "$file" | sed -n '/'$branch'p' | sed 's/'$name'/&\n/g' | grep $name |wc -l

where
$file=filename
$branch=line containing branch
$name=dev

while i am doing the above result is 1,but it should be 4

Please suggest
Why not use a single sed statement ?

Code:
sed -e "s/\(${branch}.*\)${name}/\1\n/g" ${file}

# 3  
Old 04-27-2009
The follwing is the correct sed (syntactically)

cat "$file" | sed -n ''$branch''p'' | sed 's/'$name'/&\n/g' | grep $name |wc -l

don't know whether it will gives you correct OP or not

What exactly you need ?..i mean your requirement.

You posted

where
$file=filename
$branch=line containing branch
$name=dev

but if you pass any numeric value for $brach say 1 or 2. The sed will print tht particular line and again some other searches and replaces. As always ther will be only one line , wc -l will result in 1.

How ever if u want to search for the existence of a word called "dev" in a specific line the following will do the job :

Code:
cat "$file" | sed -n ''$branch''p'' |deroff -w | sort | uniq -c  |grep $name | awk '{ print $1} '


Last edited by panyam; 04-27-2009 at 06:41 AM..
# 4  
Old 04-28-2009
Hi Panyam,

Thanks for ur reply.
Can u please give some idea about deroff command.
i have seen its man page but cannt understand .

Please help.
# 5  
Old 04-28-2009
Hi Milan,

In this context of use "deroff -w" , the output is a word list, one ``word'' per line, with all other characters deleted.

I too not that much aware of it's use. From google i found that :

"Deroff reads each file in sequence and removes all nroff and troff(1) requests and non-text arguments, backslash constructions, and constructs of preprocessors such as eqn(1), pic(1), and tbl(1). Remaining text is written on the standard output"

If you are interested u can go through the link to find more about nroff and troff:

Ch 8 -- Basic Formatting with troff/nroff
# 6  
Old 04-28-2009
Hi Panyam,

Actally the line is of the form as given below
branch=dev:sdf:dev:jin:kilii:fin:kale:boyle:dev:james:dev

i have set IFS=:
then execute

cat "$file" | sed -n ''$branch''p'' |deroff -w | sort | uniq -c |grep $name | awk '{ print $1} '

but it is not giving no fo dev present.
Plese reply
# 7  
Old 04-28-2009
Dear Milan ,

It's working Fine for me.

TEST>cat file
branch=dev:sdf:dev:jin:kilii:fin:kale:boyle:dev:james:dev

TEST>IFS=:

TEST>cat "$file" | sed -n ''$branch''p'' | deroff -w
branch
dev
sdf
dev
jin
kilii
fin
kale
boyle
dev
james
dev
TEST>cat "$file" | sed -n ''$branch''p'' | deroff -w | sort |uniq -c |grep $name | awk '{print $1>
4

Chk it once again the branch and name values your passing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sed to repeat every character on same position from the upper line replacing whitespace

Hello is it possible with awk or sed to replace any white space with the previous line characters in the same position? I am asking this because the file I have doesn't always follow a pattern. For example the file I have is the result of a command to obtain windows ACLs: icacls C:\ /t... (5 Replies)
Discussion started by: nakaedu
5 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. UNIX for Dummies Questions & Answers

Replacing special character with sed

Hi All, I have a text file that contains I1SP2 *=*=Y=M=D001D My requirement is to replace all occurrence of =* to =Z expected o/p is I1SP2 *=Z=Y=M=D001D I have tried with sed 's/=*/=Z/g' file sed 's!\=*!\=Z/g' file sed 's!\=*!\=Z!g' file sed 's!\=\*!\=Z!g' file but its not... (3 Replies)
Discussion started by: gotamp
3 Replies

4. Shell Programming and Scripting

Sed: delete on each line before a character and after a character

Hi there, A total sed noob here. Is there a way using sed to delete everything before a character AND after another character on each line in a file? The deletion should also delete the indicating characters(here: an opening and a closing parenthesis). The original file would look like... (3 Replies)
Discussion started by: bnbsd
3 Replies

5. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

6. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

7. Shell Programming and Scripting

Replacing the last character for each line in a file

Hello, I have a csv file and will like to replace the last character of each line in the file with Z (20 Replies)
Discussion started by: 123script
20 Replies

8. Shell Programming and Scripting

Replacing a character string in a file

Hi there, I have a paramater file that looks like this :- IRL|07122005|27389|VTIEpay|email address|5|200 When my program finishes I want to replace the seventh field. the existing code is like this cat <<-EOF | ed -s $PARFILE 1,$ g/^$ICO/s/$prvdate/$TODAY/ 1,$... (13 Replies)
Discussion started by: rjsha1
13 Replies

9. UNIX for Dummies Questions & Answers

Replacing a character in a line

Hi All I want to replace a character in a line, but position will be different form one iteration to another. So i m keeping the position i a variable. I am trying with following code pos=3 echo "Hello World, Good Morning" | sed 's/\(.\{$pos\}\)./\1Y/' But its not working, Can you... (2 Replies)
Discussion started by: Usha Shastri
2 Replies

10. Shell Programming and Scripting

replacing string with special character ???

the problem is while replacing the old string with new one with the help of SED i am unable to replace the special characters with new strings. how can i do that? i dont want the user to be given the trouble to write '\' before every special characters like * , . , \ , $ , &. sed... (4 Replies)
Discussion started by: imppayel
4 Replies
Login or Register to Ask a Question