sed basic doubt

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed basic doubt
# 1  
Old 06-26-2011
sed basic doubt

Hi ,

what is the equivalent of below awk in sed.

awk '$1=="ABC"&&$2=="XYZ" {print $0}' infile

# 2  
Old 06-26-2011
sed -n '/^ABC  *XYZ/p'

but there are nuances with tabs and starting spaces (and/or tabs).

With GNU sed this is the same:
sed -r -n '/^[ \t]*ABC[ \t]+XYZ/p' infile

Last edited by yazu; 06-26-2011 at 11:00 AM..
# 3  
Old 06-26-2011
Hi Yazu,

I am trying to apply a sed logic using the below code only if my first column has ABC and second column as XYZ,but the below code is not giving the required output.
sed -r -n  '/^[ \t]*ABC[ \t]+XYZ/ { s/98850|97415|24874//g }'  Duplicate_phone_numbers.csv  > generated.csv

can you please tell me where I am doing it wrong.

# 4  
Old 06-26-2011
Please, give an example of the input.

It's possible you just don't put ";p" after "g" (or maybe you need remove "-n") but I believe there are maybe another problems.

Last edited by yazu; 06-26-2011 at 11:49 AM..
# 5  
Old 06-26-2011
Hi yazu,

Below is my source data


If my first column is AIRTEL and second column is POSTPAID then I am removing the 98850 and 24874 from the 3rd column data

so my out will be as below

Target Data

# 6  
Old 06-26-2011
In every line which begins with "AIRTEL,POSTPAID" this code substitutes the first substring which matches the next pattern: "comma" "number or another number" "comma" by one "comma":

sed -r '/^AIRTEL,POSTPAID/ s/,(98850|24874),/,/'

GNU sed, extended regular expressions.
This User Gave Thanks to yazu For This Post:
# 7  
Old 06-26-2011
Originally Posted by yazu
With GNU sed this is the same:
sed -r -n '/^[ \t]*ABC[ \t]+XYZ/p' infile

Additionally, after XYZ, one would need to test for blanks or end of line.

(Yes, I am aware that what follows is a nitpick. Smilie)

The standard doesn't specify space and tabs but blank characters (which as far as I know is always space and tab regardless of locale, but who knows), so it might be better to use the [:blank:] character class instead of a bracket expression with hardcodes a space and tab.

This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic doubt in UNIX

Hi, I'm new to this and very much interested to learn unix. Can any one explain me the symbols y we use this is scripting(~ and $). It would be great if some one explain with the eg. Thanks Naveen A (2 Replies)
Discussion started by: Pranaveen
2 Replies

2. UNIX for Dummies Questions & Answers

Doubt in sed

Hi All Can some one explain what does the given two sed commands do :confused: sed "s/\'/\\\'/g" | sed 's/\"/\\\"/g' Please find the sample code i used to find out what this is doing , but it has confused me more :wall: $ cat sri1.txt \ ' " $ sed 's/\"/\\\"/g' sri1.txt \ '... (5 Replies)
Discussion started by: Sri3001
5 Replies

3. Shell Programming and Scripting

sed doubt...

Hi, i need find and replace a sting with a new variable having value as spaces in between. Eg: set a = "i am variable" set b = "i am second" sed -e 's/find_string/'$a'/g' -e 's/find2_str/'$b'/g' input_file here it is giving error... How to get an varaible, which is... (6 Replies)
Discussion started by: vasanth.vadalur
6 Replies

4. Shell Programming and Scripting

shell script basic doubt

hi, I am new script learner, so my basic doubt is , how to store value of any command in a variable example $ ls | wc -l i want to stote the output of this in a variable c. so that i can use c in if else loop. and when do we use " ` " symbol in script.. can anyone also tell for... (5 Replies)
Discussion started by: hi2_t
5 Replies

5. Shell Programming and Scripting

Basic SED doubt

Hi Friends!! I want to add a / at the end of a number. for example i have CQ65758 /, in this case i want to shift that backspace one space to the left so the my result becomes CQ65758/. How can i do that with sed. Thanks Adi (3 Replies)
Discussion started by: asirohi
3 Replies

6. UNIX for Dummies Questions & Answers

got a basic doubt on cat-file permissions

Hi all, Today I was just fooling around with directories and faced this. I create a directory 'testdir' and create a file 'myfile' inside it. gandalf@gondor:~$ mkdir testdir gandalf@gondor:~$ cd testdir gandalf@gondor:~/testdir$ touch myfile Then I set the following permissions for the... (7 Replies)
Discussion started by: ranj@chn
7 Replies

7. Shell Programming and Scripting

Shell scripting basic doubt

Hi, I have a script called which consists of a single echo statement like this #/usr/bin/ksh echo "Mani" I changed the mode for the script by giving chmod a+x If I want to execute the scrpt by just giving the name at the command line "", what should I necessarily do?... (3 Replies)
Discussion started by: sendhilmani123
3 Replies

8. Shell Programming and Scripting

Certainly basic doubt about IF

On the below "IF" i test if the user have put the first argument. I also would like to test if the user have written a second argument. So, my doubt is: - How can i evaluate 2 conditions on a if statement? How is the OR created? - How can i to verify if the second argument is non... (1 Reply)
Discussion started by: tmxps
1 Replies

9. UNIX for Dummies Questions & Answers

doubt in sed

hi all, i have a variable exported as VAR=ATTRIB then tried with, echo "tt" | sed 's/^/$VAR/' expected result as ttATTRIB but obtained only, $VARtt i could nt get where i am wrong. Thanks. (3 Replies)
Discussion started by: matrixmadhan
3 Replies

10. Shell Programming and Scripting

doubt it sed

Hello.. i want to use variable in sed.. like sed 's/ROOTMAILID/$variable/g' conf.test but its not working.. please help thanks in advance esham (2 Replies)
Discussion started by: esham
2 Replies
Login or Register to Ask a Question