Retreive string between two chars


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retreive string between two chars
# 15  
Old 03-18-2007
Quote:
Originally Posted by danland
when i run a grep and put the result in a file (in the prompt mod) everything is okay and i dont that problem.
but I have written a script that when running it , it does the following:

1. backups the log file (that one that is written into)
2. creating a new tmp file
3. searching the file using grep

when looking inside the new grep file i have this problem.

What are the differences between the code you run at the prompt and the code in the script? Post the actual code. Do not retype it.

What are the differences in the environment between the prompt and that of the script? How do you run the script?

Are you sure you are grepping the same file in both cases? What does the file contain?

# 16  
Old 06-25-2007
one more thing to ask

hello everyone again,
I have a little problem.
When i am running a 'grep' ( 'grep LOGIN *' ) on some log files (each new line is represented by a file called IMI_acc3_**.log. as you can see i get 5 lines that contain same fields but different data , for instance:
"
IMI_acc3_070625_0055.log:IMI|DEBUG|25.06.2007 10:48:48|10580|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972V\ALLfXkZdbKB6o_cU7s2c89bwK9vg8N4VwR5MP84Rg92lR3Bv36eBsA--]
IMI_acc3_070625_0055.log:IMI|DEBUG|25.06.2007 10:52:17|10591|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972fm\4 CMV\cine99matic]
IMI_acc3_070625_0056.log:IMI|DEBUG|25.06.2007 10:59:59|10601|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972CMV\testing]
IMI_acc3_070625_0057.log:IMI|DEBUG|25.06.2007 11:02:21|10607|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972CMV\testing]
IMI_acc3_070625_0058.log:IMI|DEBUG|25.06.2007 11:03:51|10608|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972CMV\testing]
grep: imi.server.sock: No such device or address".

Now how can i extract the one before the last line "IMI_acc3_070625_0058.log:IMI|DEBUG|25.06.2007 11:03:51|10608|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972CMV\testing]"

is it possible to do it with 'awk' command ?
# 17  
Old 06-25-2007
Code:
grep ... | sed -n 'N;/IMI_acc3_070625_0058.log/{P;q;};D'


Last edited by anbu23; 06-25-2007 at 08:44 AM..
# 18  
Old 06-25-2007
what does the "N/***/P" represent??

Thx
daniel
# 19  
Old 06-25-2007
hello anbu23,
Thx for the message but i didin't explain myself...

regardless of what is written, i need to retrieve the line (one before the last).
What you gave me is a search according to "IMI_acc3..." and this i dont want.
when running the command i recieve nothing.


Appreciate your help,

Daniel
# 20  
Old 06-25-2007
Quote:
Originally Posted by danland
hello anbu23,
Thx for the message but i didin't explain myself...

regardless of what is written, i need to retrieve the line (one before the last).
What you gave me is a search according to "IMI_acc3..." and this i dont want.
when running the command i recieve nothing.


Appreciate your help,

Daniel
Code:
grep ... | sed -n 'N;${P;q;};D'

# 21  
Old 06-25-2007
Can you please explain what the option represent???

I ran the command but i get the wrong line

// before adding the change
[root@acc3 imi]# grep LOGIN * | grep 972525403496
IMI_acc3_070625_0055.log:IMI|DEBUG|25.06.2007 10:48:48|10580|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972525403496:0:0\ariel_nackash\3 CMV\ALLfXkZdbKB6o_cU7s2c89bwK9vg8N4VwR5MP84Rg92lR3Bv36eBsA--]
IMI_acc3_070625_0055.log:IMI|DEBUG|25.06.2007 10:52:17|10591|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972525403496:0:0\arieln14@fastmail.fm\4 CMV\cine99matic]
IMI_acc3_070625_0056.log:IMI|DEBUG|25.06.2007 10:59:59|10601|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972525403496:0:0\mobilemailqa138\1 CMV\testing]
IMI_acc3_070625_0057.log:IMI|DEBUG|25.06.2007 11:02:21|10607|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972525403496:0:0\mobilemailqa138\1 CMV\testing]
IMI_acc3_070625_0058.log:IMI|DEBUG|25.06.2007 11:03:51|10608|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972525403496:0:0\mobilemailqa138\1 CMV\testing]
grep: imi.server.sock: No such device or address

// after writing what you recommended.

[root@acc3 imi]# grep LOGIN * | grep 972525403496 | sed -n 'N;${P;q;};D'
grep: imi.server.sock: No such device or address
IMI_acc3_070625_0057.log:IMI|DEBUG|25.06.2007 11:02:21|10607|1073802272| : IMAP COMMAND RECV: [ABCD0 LOGIN 972525403496:0:0\mobilemailqa138\1 CMV\testing]

as you can see i am getting the wrong line. can you please explain what the options mean so i could try to understand by myself....

Thx,
Daniel
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - print only the chars that match a given set in a string

For a given string that may contain any ASCII chars, i.e. that matches .*, find and print only the chars that are in a given subset. The string could also have numbers, uppercase, special chars such as ~!@#$%^&*(){}\", whatever a user could type in without going esoteric For simplicity take... (1 Reply)
Discussion started by: naderra
1 Replies

2. Shell Programming and Scripting

Add an string at every x chars

Hi All, I have a file fo around 15k bytes which i need to insert a string " + "at every 250 bytes. I found some ideas here using perl to split into lines and tried to addapt it but the results where not satisfactory for instance i tried to change #!/usr/bin/perl $teststring =... (9 Replies)
Discussion started by: kadu
9 Replies

3. UNIX for Dummies Questions & Answers

How to search for a string with special chars?

Hi guys, I am trying to find the following string in a file, but I always get pattern not found error, not sure what is missing here. Can you help please? I do a less to open the xrates.log and then do a /'="18"' in the file and tried various combinations to search the below string. String... (8 Replies)
Discussion started by: santokal
8 Replies

4. Shell Programming and Scripting

XML - Split And Extract String between Chars

Hi, I am trying to read the records from file and split into multiple files. SourceFile.txt <?xml version="1.0" encoding="UTF-8"?>... (2 Replies)
Discussion started by: unme
2 Replies

5. Shell Programming and Scripting

Remove duplicate chars and sort string [SED]

Hi, INPUT: DCBADD OUTPUT: ABCD The SED script should alphabetically sort the chars in the string and remove the duplicate chars. (5 Replies)
Discussion started by: jds93
5 Replies

6. Shell Programming and Scripting

[Solved] print chars of a string

how can i print all the chars of a string one by line? i have thought that use a for cicle and use this command inside: ${VARIABLE:0:last}but how can i make last? because string is random P.S. VARIABLE is the string or can i make a variable for every chars of this string? this was my idea... (10 Replies)
Discussion started by: tafazzi87
10 Replies

7. UNIX for Dummies Questions & Answers

regexp: match string that contains list of chars

Hi, I'm curious about how to do a very simple thing with regular expressions that I'm unable to figure out. If I want to find out if a string contains 'a' AND 'b' AND 'c' it can be very easily done with grep: echo $STRING|grep a|grep b|grep c but, how would you do that in a single... (9 Replies)
Discussion started by: jimcanoa
9 Replies

8. Shell Programming and Scripting

Repeatable chars in a string

I have a string I keep appending too upto certain amount of chars. Is there some sort of way for me to check the string to see if I hit my limit of repeatable characters? For example, assume I allow for 2 repeatable chars, this will be a valid string Xxh03dhJUX, so I can append the last... (3 Replies)
Discussion started by: BeefStu
3 Replies

9. Shell Programming and Scripting

Matching 2 chars of a string that repeat

Hello Unix gurus, I have a gzipped file where each line contains 2 street addresses in the US. What I want to do is get a count for each state that does not match. What I have so far is: $ gzcat matched_10_09.txt.gz |cut -c 106-107,184-185 | head -5 CTCT CTNY CTCT CTFL CTMA This cuts... (5 Replies)
Discussion started by: sitney
5 Replies

10. UNIX for Dummies Questions & Answers

Extracting the last 3 chars from a string using sed

Hi. Can I extract the last 3 characters from a given string using sed ? Why the following doesn't work (it prints the full string) : echo "abcd" | sed '/\.\.\.$/p' doesn't work ? output: abcd Thanks in advance, 435 Gavea. (7 Replies)
Discussion started by: 435 Gavea
7 Replies
Login or Register to Ask a Question