![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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.. |
|
||||
|
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 |
|
||||
|
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 |
|
||||
|
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. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|