Visit Our UNIX and Linux User Community


Returning text after find


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Returning text after find
# 1  
Old 04-11-2008
Returning text after find

Hey guys, I'm sure someone will have asked this before but I searched for ages and couldn't find a post.

Anyhow, I have a datafile and I want to search for "UserID : " in it and then return the characters following this until it hits a pipe.

e.g.

Running the comand against the following data:

[01/01/2008] [INFO] |BLAH BLAH|UserID : 12345| RANDOMTEXT
[01/01/2008] |BLAH BLAH|UserID : 54321| RANDOMTEXT | RANDOMTEXT
[INFO] |BLAH BLAH|UserID : 666| RANDOMTEXT

Would return:

12345
54321
666

If any of you have an idea how to do this I would be most grateful. I'm using a Solaris box by the way. Thanks Smilie
# 2  
Old 04-11-2008
Code:
cut -d": " -f 2 filename | cut -d"|" -f 1

# 3  
Old 04-11-2008
Hmmmm, thanks but using cut -d": " -f 2 filename | cut -d"|" -f 1

... I get "cut: invalid delimiter"

I tried it without the space after the ":" but unfortunately some of the data includes ":" that aren't in "UserID : " (admittedly I didn't put that in my example data so I'll change it now)

[01:01:2008] [INFO] |BLAH BLAH|UserID : 12345| RANDOMTEXT
[01:01:2008] |BLAH BLAH|UserID : 54321| RANDOMTEXT | RANDOMTEXT
[INFO] |BLAH BLAH|UserID : 666| RANDOMTEXT

Any thoughts? Smilie
# 4  
Old 04-11-2008
I should do something like this with sed:

Code:
sed 's/.*: \(.*\)|.*/\1/' file

Regards
# 5  
Old 04-11-2008
Code:
sed -n 's/.*|UserID : \([^|]*\)|.*/\1/p'

# 6  
Old 04-11-2008
Worked a treat. Thanks for you help (again!) Smilie
# 7  
Old 04-11-2008
Picks the field from the line

As an awk fan I thought I'd offer this little snippet which pulls out the whole field for manipulation rather than simply the string following the 'userid:'

>cat rant
sdf|asdf|asfgadg|shddh|userid:sdg|sdgdg|sfhdfs|
sdf|asdf|asfgadg|shddh|userid:sdg|sdgdg|sfhdfs|
sdf|asdf|asfgadg|shddh|userid:sdg|sdgdg|sfhdfs|
...
>awk 'BEGIN {RS="|"; FS=":"} /userid/ {print $2}' rant
sdg
sdg
sdg
...
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #461
Difficulty: Medium
Simple Network Time Protocol (SNTP) is a less complex implementation of NTP, using the same protocol but without requiring the storage of state over extended periods of time.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Programming

CGI script is returning raw text instead of getting processes

I am not a CGI/Unix expert. I was installing Cognos and as part of it we have the gateway which uses the cgi-bin The application was working fine untill the server is recreated with taking backup from the old server. so for testing I created a sample cgi file as test.cgi created at... (1 Reply)
Discussion started by: golden83
1 Replies

3. UNIX for Dummies Questions & Answers

Find commmand returning search path with the result set

OS Platform : Oracle Linux 6.5 We are creating a shell script to purge old log files . It uses find command with rm in it. The syntax is find <Path of Log Directory> -exec rm -fr {} \; Example: find /tmp/test3 -exec rm -fr {} \; For rm command , we use -r option to... (4 Replies)
Discussion started by: kraljic
4 Replies

4. Shell Programming and Scripting

Finding pattern in a text file and returning a part of the word

Dear All, assume that we have a text file or a folder of files, I want to find this pattern followers*.csv in the text file , and get * as the output. There are different matches and * means every character. Thank you in advance. Best, David (1 Reply)
Discussion started by: davidfreed
1 Replies

5. UNIX for Dummies Questions & Answers

Find text in file

Hello i a script: #!/bin/sh count=0 for iname in `cat mysong` do for cname in `cat mysong` do if then count=`expr $count + 1` fi done echo "word: $iname - found in the text: $count times" count=0 donethe proplem: how i... (2 Replies)
Discussion started by: levitmic
2 Replies

6. UNIX for Dummies Questions & Answers

Find command returning bad status--

would like to remove the post (8 Replies)
Discussion started by: vk39221
8 Replies

7. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

8. UNIX for Advanced & Expert Users

find -type d returning files as well as directories

Can anyone see why the following command returns all files and not just the directories as specified? find . -type d -exec ls -F {} \; Also tried find . -type d -name "*" -exec ls -F {} \; find . -type d -name "*" -exec ls -F '{}' \; -print Always returns all files :-\ OS is... (2 Replies)
Discussion started by: tuns99
2 Replies

9. Shell Programming and Scripting

find text but replace a text beside it

I have an html file that looks like this (this is just a part of the html file): <td colspan="3" rowspan="1" style="text-align: center; background-color: rgb(<!-- IDENTIFIER1 -->51, 255, 51);"><small><!-- IDENTIFIER2 -->UP</small></td> This is to automatically update the status of the... (4 Replies)
Discussion started by: The One
4 Replies

10. UNIX for Advanced & Expert Users

find command not returning any result

I am looking for all the header files (*.h).. which as per documentation of the UNIX system shouldbe there. I am using find / -name *.h -print But it does't give anything. My question is under what condition the "find" condition will fail to find the file? What is the work around. ... (4 Replies)
Discussion started by: rraajjiibb
4 Replies

Featured Tech Videos