Sed or awk : pattern selection based on special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed or awk : pattern selection based on special characters
# 1  
Old 09-06-2011
Power Sed or awk : pattern selection based on special characters

Hello All,

I am here again scratching my head on pattern selection with special characters.


I have a large file having around 200 entries and i have to select a single line based on a pattern.

I am able to do that:

Code:

Code:
 
cat mytest.txt | awk -F: '/myregex/ { print $2}'

Output is as below:

Code:
 
//mytestserver.bon.com

now i have to split this output and pass only
Quote:
mytestserver
to a variable

i am trying this sed command ..but somehow this is not solving the purpose.

Any help where i am going wrong

Code:
 
cat mytest.txt | awk -F: '/myregex/ { print $2}' | sed 's/^[/  ^.]//'


Thanks
Usha
# 2  
Old 09-06-2011
Please post the line from the input file that you're interested in.
# 3  
Old 09-06-2011
Code:
echo '//mytestserver.bon.com' | sed -r 's //([^.]*).* \1 '
mytestserver

Smilie
This User Gave Thanks to yazu For This Post:
# 4  
Old 09-06-2011
No need to cat mytest.txt...
Code:
awk -F: '/myregex/ { print $2}' mytest.txt | sed 's;/\{1,\};;'

This User Gave Thanks to shamrock For This Post:
# 5  
Old 09-06-2011
Code:
awk -F"[:/]" '/myregex/ { print $(NF-2)}' mytest.txt

This User Gave Thanks to itkamaraj For This Post:
# 6  
Old 09-06-2011
awk

Hi,
Try this,
Code:
awk -F: '/myregex/ {gsub(/\//,"",$2);split($2,a,"."); print a[1];}' input_file

cheers,
Ranga:-)

Last edited by Franklin52; 09-06-2011 at 12:54 PM.. Reason: Please use code tags for data and code samples, thank you
This User Gave Thanks to rangarasan For This Post:
# 7  
Old 09-13-2011
Thanks Ranga.. Its perfect...

Cheers,
Usha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace Pattern with another that has Special Characters

Hello Team, Any help would be much appreciated for the below scenario: I have a sed command below where I am trying to replace the contents of 'old_pkey' variable with 'new_pkey' variable in a Soap request file (delete_request.txt). This works fine for regular string values, but this new_pkey... (8 Replies)
Discussion started by: ChicagoBlues
8 Replies

2. Shell Programming and Scripting

Precede and Append characters using sed/awk based on a pattern

I have an input file which is similar to what I have shown below. Pattern : Data followed by two blank lines followed by data again followed by two blank lines followed by data again etc.. The first three lines after every blank line combination(2 blank lines between data) should be... (2 Replies)
Discussion started by: bikerboy
2 Replies

3. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

4. Shell Programming and Scripting

Need an awk / sed / or perl one-liner to remove last 4 characters with non-unique pattern.

Hi, I'm writing a ksh script and trying to use an awk / sed / or perl one-liner to remove the last 4 characters of a line in a file if it begins with a period. Here is the contents of the file... the column in which I want to remove the last 4 characters is the last column. ($6 in awk). I've... (10 Replies)
Discussion started by: right_coaster
10 Replies

5. Shell Programming and Scripting

how to get data from hex file using SED or AWK based on pattern sign

I have a binary (hex) file I need to parse to get some data which are encoded this way: .* b4 . . . 01 12 .* af .* 83 L1 x1 x2 xL 84 L2 y1 y2 yL By another words there is a stream of hexadecimal bytes (in my example separated by space for better readability). I need to get value stored in... (3 Replies)
Discussion started by: sameucho
3 Replies

6. Shell Programming and Scripting

SED equivalent for grep -w -f with pattern having special characters

I'm looking for SED equivalent for grep -w -f. All I want is to search a list of patterns from a file. Also If the pattern doesn't match I do not want "null returned", rather I would prefer some text as place holder say "BLANK LINE" as I intend to process the output file based on line number. ... (1 Reply)
Discussion started by: novice_man
1 Replies

7. Shell Programming and Scripting

awk search pattern with special characters passed from CL

I'm very new to awk and sed and I've been struggling with this for a while. I'm trying to search a file for a string with special characters and this string is a command line argument to a simple script. ./myscript "searchpattern" file #!/bin/sh awk "/$1/" $2 > dupelistfilter.txt sed... (6 Replies)
Discussion started by: cue
6 Replies

8. Shell Programming and Scripting

sed delete pattern with special characters

Hi all, I have the following lines <b>A gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) <b>B gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) <b>J gtwrhwrthwr text hghthwrhtwrtw </b><font color='#06C'>; text text (text) and I would like to... (5 Replies)
Discussion started by: stinkefisch
5 Replies

9. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question