Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a code that is looking for a specific pattern in my log files. Usually it begins with Association ID = (This is what I am looking for in parenthesis). Now the code I have made so far is throwing a syntax:

echo "Please enter the UID: ";
read uid

uidAssoc(){
        arg1=$1
        echo $arg1
        for arg1 in $(cat ~/jlog/server.log); do echo $word;done
        | sed 's/.AssociationID = \([*])\'
}
uidAssoc $uid

Can anyone throw suggestions as to how to correct the sed command and have it echo out the results found using it?

I have tried utilizing grep instead:

echo "Please enter the UID: ";
read uid

#Will parse through server log files for associaton
uidAssoc(){
        arg1=$1
        echo $arg1
        for arg1 in $(cat ~/jlog/server.log); do echo $word; done|
        grep Association ID ~/jlog/server.log |
        grep -E -o "[a-z0-9]" ~/jlog/server.log
}
uidAssoc $uid
exit

Here is my input:

    Please enter the UID:
1.2.124.113532.80.22187.2757.20140503.123509.1190654364

And for output:

l
e
o
p
i
c
g
e
n
t
e
a
n
r
o
c
e
s
s
e
d
i
n
s
t
a
n
c
e
1
2
1
2
4
1
1
3
5
3
2
8
0
2
2
1
8
7
2
7
share|improve this question
1  
Is the ID strictly alphanumeric? If so you could do \(\w*\) at the end. Also, it seems that your final )\ may indeed want to be \) –  musher Jun 16 at 17:59
    
it is alphanumeric –  ryekayo Jun 16 at 18:00
    
I did that but this is what I got for a response: sed: -e expression #1, char 26: unterminated s' command. And furthermore the script freezes after that, even after I put exit; inside and outside the function –  ryekayo Jun 16 at 18:05
    
@ryekayo, it's better if you add samples of input and output. –  Sneetsher Jun 16 at 18:44
1  
What is your script actually supposed to do? at the moment it seems like a random collection of cats, pipes and variables which have no particular relation to one another. –  steeldriver Jun 16 at 19:07

2 Answers 2

Insert this in your sed segment. Your output is sendin newline at the end of each character.

Replace sed (after | ):

sed ':a;N;$!ba;s/\n/ /g' 's/.AssociationID = ([*])\'

This will read the whole file in a loop, then replaces the newline(s) with a space.

Update: explanation.

create a label via :a
append the current and next line to the pattern space via N
if we are before the last line, branch to the created label $!ba 
($! means not to do it on the last line (as there should be one final newline)).
finally the substitution replaces every newline with a space on the pattern space
(which is the whole file).

You may need to tweak this for your final output but, this should cure the one character per line problem with sed.

share|improve this answer
up vote 0 down vote accepted

I figured out a bulk of this at home. I have the code snippet below and i just gotta tweak around with it to figure out the rest of it:

echo "Please enter the UID: ";
read uid

#Will parse through server log files for associaton
uidAssoc(){
        arg1=$1
        echo $arg1
        for arg1 in $(cat ~/jlog/server.log); do echo $word;done |
        awk 'match($i, /Association ID = /){ print substr($i, RSTART,RLENGTH) 
        }
        ' ~/jlog/server.log
}
uidAssoc $uid
exit
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.