find text enclosed between special characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find text enclosed between special characters
# 1  
Old 01-04-2011
find text enclosed between special characters

Hi,

I'm trying to find all DISTINCT words having _mr in the line and ENCLOSED in '/'.

For eg below is the text in a file..
Code:
/database/new_mr254/1
/database/rawdb/views/new_mr254/1
/database/project/rawdb/tables/new_mr232/1
/database/project/rawdb/views/new_mr253/1
/database/project/rawdb/views/new_prj_mr268/1
/rdu/src/conf/rawdataload/new_mr253/1

output should look like:
Code:
new_mr254
new_mr232
new_mr253
new_prj_mr268


Last edited by Scott; 01-04-2011 at 12:06 PM.. Reason: Code tags
# 2  
Old 01-04-2011
Try:
Code:
awk -F/ '!A[w=$(NF-1)]++{print w}' infile

# 3  
Old 01-04-2011
The following could work

Use the tr command to translate / to new-line characters, putting each entry on one line.
Use the grep command to only display lines meeting the "_mr" requirement.
Finally, use the sort -u command to show only unique entries.

Something like:
Code:
cat myfile | tr "/" "\n" | grep "_mr" | sort -u

displays the file (yea, could do without the cat command, but makes it a little easier to follow the piping)
translate to put everything on its own line
selects based on your requirement
sorts and displays unique values
# 4  
Old 01-04-2011
assuming it does not appear more than once per line :

Code:
sed 's:^.*/\([^/]*_mr[^/]*\).*:\1:' yourfile | sort -u


Last edited by ctsgnb; 01-04-2011 at 12:31 PM..
# 5  
Old 01-04-2011
Thanks Scrutinizer.

The result is not matching what I was expecting:

Code:
awk -F/ '!A[$(NF-1)]++{print $(NF-1)}' test.txt
new_mr254


Last edited by radoulov; 01-04-2011 at 12:24 PM.. Reason: Code tags!
# 6  
Old 01-04-2011
Are you on Solaris (in that case use nawk or /usr/xpg4/bin/awk instead of awk)?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to enter special characters in a text file using vi?

Hi, I need to create a test text file with the special characters \342\200\223 in it and to be able to use sed maybe to delete them I tried doing it using vi by pressing CTRL-V and then typing 342 but it does not work. After pressing CTRL-V and typing 342 it seems to just insert the numbers... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Remove Special Characters Within Text

Hi, I have a "|" delimited file that is exported from a database. There is one column in the file which has description/comments entered by some application user. It has "Control-M" character and "New Line" character in between the text. Hence, when i export the data, this record with the new... (4 Replies)
Discussion started by: tarun.trehan
4 Replies

3. Shell Programming and Scripting

Find out special characters from xml file

Hi....I have a xml file which is having lots of special characters which I need to find out and put the distinct list of those into a text file. The list of special characters is not specific, it can be anything at different point of time. Can anyone help me to find out the same and list out? ... (10 Replies)
Discussion started by: Krishanu Saha
10 Replies

4. Shell Programming and Scripting

HOw to find special characters

I have flat file which has data like this glid¿as_liste¿025175456 How can I print these lines into new file? (4 Replies)
Discussion started by: sol_nov
4 Replies

5. UNIX for Dummies Questions & Answers

Find in Files (special characters)

Well, I've searched the forum, but couldn't find an option, that would help me. I'm really a dummie in unix, so here it goes. I've got like 50k files in a single catalogue. One of them contains a string: Including the box/square brackets. I tried to find it manually, and use some search... (2 Replies)
Discussion started by: kalik
2 Replies

6. Shell Programming and Scripting

Remove special characters from text file

Hi All, i am trying to remove all special charecters().,/\~!@#%^$*&^_- and others from a tab delimited file. I am using the following code. while read LINE do echo $LINE | tr -d '=;:`"<>,./?!@#$%^&(){}'|tr -d "-"|tr -d "'" | tr -d "_" done < trial.txt > output.txt Problem ... (10 Replies)
Discussion started by: kkb
10 Replies

7. Shell Programming and Scripting

remove special characters from text using PERL

Hi, I am stuck with a problem here. Suppose i have a variable which is assigned some string containing special charatcers. for eg: $a="abcdef^bbwk#kdbcd@"; I have to remove the special characters using Perl. The text is assigned to the variable implicitly. How to do it? (1 Reply)
Discussion started by: agarwal
1 Replies

8. UNIX for Dummies Questions & Answers

Find and replace special characters in a file

HI All I need a shell script ehich removes all special characters from file and converts the file to UTF-* format Specail characters to be removed must be configurable. strIllegal = @"?/><,:;""'{|\\+=-)(*&^%$#@!~`"; Please help me in getting this script as my scripting skilla are... (2 Replies)
Discussion started by: sujithchandra
2 Replies

9. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies

10. AIX

How to find special characters??

By more, vi, cat etc commands special characters (few control characters) are not identified. Is there any way to find out those? Thanks Sumit (3 Replies)
Discussion started by: sumitc
3 Replies
Login or Register to Ask a Question