Results 1 to 6 of 6
Hi all,
I facing 2 problems:
problem 1:
How to grep lines ?
I want to grep a line with 2 underscores in a file like this.
Code:
L1field1_L1field2_L1field3_L1field4
L2field1_L2field2_L2field3
...
- 01-11-2008 #1
Questions on sed and grep
Hi all,
I facing 2 problems:
problem 1:
How to grep lines ?
I want to grep a line with 2 underscores in a file like this.
The line with two underscores,which is :Code:L1field1_L1field2_L1field3_L1field4 L2field1_L2field2_L2field3 L3field1_L3field2
L2field1_L2field2_L2field3
problem 2:
How to use sed with \ character ?
I want to replace word called "word_123" into "word\123"
replace _ with \ using sed.
Cheers- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------
- 01-11-2008 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
To replace the underscore with a backslash in lines with 2 underscores:
RegardsCode:awk 'BEGIN{FS="_"}NF==3{print $1 "\\" $2 "\\"$3;next}{print}'
- 01-11-2008 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
shell script
Code:#!/bin/sh while read -r line do before=`printf $line|wc -c` after=`printf $line | tr -d '_' | wc -c` n=`echo "$before - $after" | bc` if [ $n -eq 2 ];then echo $line | sed 's/_/\\/g' fi done < "file"
- 01-11-2008 #4Linux Newbie
- Join Date
- Jan 2008
- Location
- UK
- Posts
- 211
Hello, out of interest unless you really need or want to use sed, Perl on the command line is very quick.
at prompt write:
perl -p -i -e 's/word_123/word\\123/g' then the file name
- 01-11-2008 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 01-17-2008 #6
Thanks guyz for your help
- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------


Reply With Quote
