The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing a character string in a file rjsha1 Shell Programming and Scripting 13 04-11-2009 02:07 PM
Replacing a character in a line Usha Shastri UNIX for Dummies Questions & Answers 2 04-02-2009 03:08 AM
Replacing a string in nth line maxmave Shell Programming and Scripting 1 06-04-2008 06:32 PM
replacing a string in a file with command line parameter chiru_h Shell Programming and Scripting 5 08-03-2006 10:34 AM
replacing string with special character ??? imppayel Shell Programming and Scripting 4 12-08-2004 05:07 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-27-2009
millan millan is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 11
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 (permalink)  
Old 04-27-2009
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by millan View Post
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 (permalink)  
Old 04-27-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
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 (permalink)  
Old 04-28-2009
millan millan is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 11
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 (permalink)  
Old 04-28-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
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 (permalink)  
Old 04-28-2009
millan millan is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 11
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 (permalink)  
Old 04-28-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0