Can we give multiple patterns to a sed command???


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can we give multiple patterns to a sed command???
# 1  
Old 03-23-2007
Can we give multiple patterns to a sed command???

HI,

I want to know can multiple pattern be given inside a sed statement..

Input:

Code:
aa_bb_cc.Mar-22-2007
cc_dd_ee.Mar-21-2007
aa_1002985_952.xml
aa_bb_032207.txt
aa_bb_cc_10002878.dat

Output:

Code:
aa_bb_cc
cc_dd_ee
aa.xml
aa_bb.txt
aa_bb_cc.dat

I got this command from Ghostdog
Code:
sed 's/_[0-9]\{1,\}//g' file > newfile

which removes the numerals following _

Is there a way to specify another pattern within the same above said sed command to remove Mar-22-2007 to get the output as mentioned above....

Thanks for you help,
Kumar.

Last edited by rbatte1; 01-13-2017 at 07:33 AM.. Reason: Added CODE tags
# 2  
Old 03-23-2007
Code:
sed 's/_[0-9]\{1,\}//g;s/\..*-[0-9]\{1,\}$//' file

# 3  
Old 03-23-2007
Give a try on this...

Quote:
sed -e 's/[_[0-9]\{1,\}]//g' -e 's/\.[A-Za-z]\{3\}-[0-3][0-9]-[0-9]\{4\}//' filename
# 4  
Old 03-23-2007
Quote:
Originally Posted by anbu23
Code:
sed 's/_[0-9]\{1,\}//g;s/\..*-[0-9]\{1,\}$//' file

Hi Anbu,

i'm sorry,i specified the wrong input..

Input:

Code:
aa.bb_cc.Mar-22-2007
aa.dd_ee.Mar-21-2007
aa.bb_cc_ee.Mar-22-2007
aa_1002985_952.xml
aa_bb_032207.txt
aa_bb_cc_10002878.dat

Output:

Code:
aa.bb_cc
aa.dd_ee
aa.bb_cc_ee
aa.xml
aa_bb.txt
aa_bb_cc.dat

can you please give me a modified sed command...

Jacoden,

I did try out your command but it is giving the same input file as output...its not filtering out anything...

Thanks for you help guys,
Kumar.

Last edited by rbatte1; 01-13-2017 at 07:33 AM.. Reason: Added CODE tags
# 5  
Old 03-23-2007
Quote:
Originally Posted by kumarsaravana_s
Hi Anbu,

i'm sorry,i specified the wrong input..

Input:

Code:
aa.bb_cc.Mar-22-2007
aa.dd_ee.Mar-21-2007
aa.bb_cc_ee.Mar-22-2007
aa_1002985_952.xml
aa_bb_032207.txt
aa_bb_cc_10002878.dat

Output:

Code:
aa.bb_cc
aa.dd_ee
aa.bb_cc_ee
aa.xml
aa_bb.txt
aa_bb_cc.dat

can you please give me a modified sed command...

Jacoden,

I did try out your command but it is giving the same input file as output...its not filtering out anything...

Thanks for you help guys,
Kumar.
Code:
sed 's/_[0-9]\{1,\}//g;s/\.[a-zA-Z]\{3\}-[0-9]\{2\}-[0-9]\{4\}$//' file


Last edited by rbatte1; 01-13-2017 at 07:34 AM.. Reason: Retro-added CODE tags in quote
# 6  
Old 03-23-2007
Quote:
Originally Posted by anbu23
Code:
sed 's/_[0-9]\{1,\}//g;s/\.[a-zA-Z]\{3\}-[0-9]\{2\}-[0-9]\{4\}$//' file


Thanks a lot Anbu....It is working perfectly fine...Can you please explain me the second pattern...

Thanks and Regards,
Kumar
# 7  
Old 03-23-2007
Quote:
Originally Posted by kumarsaravana_s
Thanks a lot Anbu....It is working perfectly fine...Can you please explain me the second pattern...

Thanks and Regards,
Kumar
Code:
s/\.[a-zA-Z]\{3\}-[0-9]\{2\}-[0-9]\{4\}$//

. is a regular expression character which matches any single character except NUL.But we want to match dot in the input so we used \ to turn off the special meaning of the dot.

[a-zA-Z]\{3\}- matches three alphabetic character followed by hypen

[0-9]\{2\}- matches two digits followed by hypen

[0-9]\{4\} matches four digits

$ match the preceding regular expression at the end of the line

s/\.[a-zA-Z]\{3\}-[0-9]\{2\}-[0-9]\{4\}$// removes the string matching this regular expression at the end of line

Last edited by rbatte1; 01-13-2017 at 07:36 AM.. Reason: Added ICODE tags and removed colour for clarity of code
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed command to extract the string between 2 patterns but having some particular value

Hi - i have one file with content as below. ***** BEGIN 123 ***** BASH is awesome ***** END ***** ***** BEGIN 365 ***** KSH is awesome ***** END ***** ***** BEGIN 157 ***** KSH is awesome ***** END ***** ***** BEGIN 7123 ***** C is awesome ***** END ***** I am trying to find all... (4 Replies)
Discussion started by: reldb
4 Replies

2. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

3. Shell Programming and Scripting

[Solved] My sed command not give me a satisfy result

This is my command echo "Test" | sed -f <(sed -e 's/.*/s,&,gI/' mydic) In mydic file,containing 2 columns delimit by comma (,) a,AlphabetA . . . e,AlphabetE . . s,AlphabetS . t,AlphabetT test,testedd . . zebra,zebraaaa The expect result is testedd (0 Replies)
Discussion started by: Runicer
0 Replies

4. Shell Programming and Scripting

Multiple sed in one command

Hi, How do I run multiple sed on a file similar to the below in one command? I can't global substitute the 401 as it can occur elsewhere in the row and needs to be maintained if it does. sed "s/,40128/,128/" test2.csv > test3.csv sed "s/,40192/,192/" test3.csv > test4.csv sed ... (6 Replies)
Discussion started by: ksexton
6 Replies

5. Shell Programming and Scripting

Search multiple patterns in multiple files

Hi, I have to write one script that has to search a list of numbers in certain zipped files. For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB) I have... (10 Replies)
Discussion started by: vsachan
10 Replies

6. Shell Programming and Scripting

SED command using multiple input files

What is the syntax to use multiple input files in a SED command. i.e. substitute a word with a phrase in every file in a directory. for every file in /usr/include that has the word "date" in the file grep -l '\<date\>' /usr/include/*.h find each occurrence of the word "time" in the file &... (3 Replies)
Discussion started by: sheoguey
3 Replies

7. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies

8. Shell Programming and Scripting

how to use multiple files in sed with w command

i have a command like : sed -n 's/^* /&/w even' <file if i want to write to multiple files like sed -n 's/^* /&/w zero two three' < file its not working it is taking "zero two three" as a single file i want to write to 3 seperate files . pls can anyone help me (2 Replies)
Discussion started by: santosh1234
2 Replies

9. UNIX for Dummies Questions & Answers

Sed command over multiple lines in csh??

hi i have a long sed command in a csh script that won't fit on 1 line. how do i break it up correctly over multiple lines? this doesn't seem to work in csh: sed -e s/template/$IP.$NN/ \ -e s/NRG/6/ \ -e s/inputf/$IS.$NN/ \ -e s/SHIFT/10.0/ <template.egsinp > $IP.$NN.inp i get: sed:... (1 Reply)
Discussion started by: tuathan
1 Replies

10. Shell Programming and Scripting

how to grep for lines between 2 give patterns?

Hello shell gurus :) how to grep for lines between 2 give patterns? thanks (1 Reply)
Discussion started by: melanie_pfefer
1 Replies
Login or Register to Ask a Question