Selecting data between [ and ]


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting data between [ and ]
# 1  
Old 01-09-2012
Selecting data between [ and ]

Hi Team,
I am searching through log file using
Code:
grep -i '<search_key>' <file_name>|awk '{print $18}'

example outputs are
Code:
[MY_DWH]
[MY_CATEGORY]
[MY_LEARNING_DATA]

I would like to select the data between [ and ] and no other as I am getting some junk characters sometimes which chaging the o/p display format.
Kindly assist. Thanks in advance.

Thanks
Sam


Moderator's Comments:
Mod Comment Use code tags please, see PM.

Last edited by zaxxon; 01-09-2012 at 01:04 PM.. Reason: code tags
# 2  
Old 01-09-2012
If the junk characters aren't [], I'm not sure how removing [] will help, but:

Code:
awk '{ substr($8,2, length($8)-2); }'

If that doesn't do it, you'll need to actually explain what your input data is, so we know why $8 doesn't always work.
# 3  
Old 01-09-2012
Code:
grep -i '<search_key>' <file_name>|awk '{print $18}'| sed 's/\[\([^]]\+\)\]/\1/g'

# 4  
Old 01-09-2012
@skrynesaver

somewhere I see a extra character on wc -c ..
thanks for your help anyways .
I must admit that I am newbie in sed

---------- Post updated at 12:40 PM ---------- Previous update was at 12:11 PM ----------

i found out something like this

Code:
grep -i '<search_key>' <file_name>|awk '{print $18}'|[/FONT]cut -d'[' -f2|cut -d']' -f1

any other way ?



Moderator's Comments:
Mod Comment Start using code tags as mentioned in your starting post and check your PM for the guide if you don't know how and when to use them. Ignoring this gets your more infraction points.

Last edited by zaxxon; 01-09-2012 at 01:46 PM.. Reason: code tags
# 5  
Old 01-09-2012
Did you try my way yet?
# 6  
Old 01-09-2012
Quote:
Originally Posted by sanjaydubey2006
...
i found out something like this

Code:
grep -i '<search_key>' <file_name>|awk '{print $18}'|[/FONT]cut -d'[' -f2|cut -d']' -f1

any other way ?
...
Code:
grep -i '<search_key>' <file_name> | awk '{gsub(/[\]\[]/,"",$18); print $18}'

tyler_durden

Last edited by durden_tyler; 01-11-2012 at 01:07 AM..
# 7  
Old 01-10-2012
@corona688 :

Its not working ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting groups

I have a file with contents like host1.domain.com:9090,host2.domain.com:9090,host3.domain.com:9090 I am looking for such an operation so that, the output should be host1.domain.com:9090 host2.domain.com:9090 host3.domain.com:9090 And also, if the number of entries are more, the... (1 Reply)
Discussion started by: anil510
1 Replies

2. Shell Programming and Scripting

Selecting tables

Hi, Can anyone help me that, How to see the table fields in Oracle database through shell script(ksh). I have tried with the following: sqlplus -s $user/$passwd@$sid << SQL >> session.log select * from Testtab.sql I'm not able to see anything.. Thanks (4 Replies)
Discussion started by: zxcjggu708
4 Replies

3. Shell Programming and Scripting

awk for selecting columns of different data files

Hello friends, How can I use awk commands to obtain selective blocks from different data files. For example file1 a b c d e f g h i file2 1 2 3 4 5 6 7 8 9 output a b 2 3 d e 5 6 g h 8 9 is it possible ? (2 Replies)
Discussion started by: rpf
2 Replies

4. Shell Programming and Scripting

selecting certain files

Hi, I have been working on a punch of codes and I got to a problem I hope to get help on. I have some files that I want to work with but the folder has other files in it. For example: The folder contains these files: path to files: /home/distribution/ ls: b.10; b.11; b.12; b.20; b.222;... (2 Replies)
Discussion started by: iconig
2 Replies

5. Shell Programming and Scripting

Selecting A Block Of Data From A File (AWK)

Ladles and Jellyspoons,I am trying to use, unsucessfully I might add, awk to strip a large block of information from and audit output.The format resembles the following:-----------------------------------------------------------Event: execveTime: ... (3 Replies)
Discussion started by: proc1269
3 Replies

6. Shell Programming and Scripting

about selecting lines

Hello , i got text file like that' C:\Users\Public\Pictures\Sample Pictures\aa.jpg C:\Users\Public\Pictures\Sample Pictures\thumb.jpg C:\Users\Public\Pictures\vv\cc.jpg C:\Users\Public\Pictures\Sample Pictures\ee.jpg C:\Users\Public\aa\Sample Pictures\cvswsr.jpg... (1 Reply)
Discussion started by: davidkhan
1 Replies

7. Shell Programming and Scripting

Selecting a string

Hi, I have a small question... Since i am new to Unix ksh scripting i am having this problem of understanding... Well the thing is that i just want a part of a line for example Data Set=ELIG_Member_20090126.dat Count Total= 8192 Actual Total= 8187 I just want the value of Actual Total... (4 Replies)
Discussion started by: bhagya2340
4 Replies

8. Shell Programming and Scripting

selecting tokens from a string...

i store the output of ls in a variable FL $FL=`ls` $echo $FL f1.txt f2.txt f3.txt f4.txt f5.txt script.sh script.sh~ test.txt now if i want to retrive the sub-string "f1.txt" from $FL we were taught that this is what i have to do $set $FL $echo $1 f1.txt and echo $2 would give... (1 Reply)
Discussion started by: c_d
1 Replies

9. Shell Programming and Scripting

Selecting one file from a list

Hi, I am able to do this by brute force but I am just curious if there is a better way of handling things. Basically the scenario is something like this: There are a number of files in a directory: rib.20071224.1759.gz 24-Dec-2007 17:59 132K rib.20071224.1959.gz 24-Dec-2007... (7 Replies)
Discussion started by: Legend986
7 Replies

10. Shell Programming and Scripting

Selecting a line value

Hello, I just wanted to know if there is a way in UNIX to select a line value from a list of words. there is no line number before each word, hence could not use grep. (4 Replies)
Discussion started by: unibboy
4 Replies
Login or Register to Ask a Question