LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-01-2004, 03:16 PM   #1
eicherlist
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Rep: Reputation: 0
find a string and write it somewhere


Hi

I searching for a way to solve this problem.

Searching in one or more files for a string. The content after that searched string is relevant. This relevant text should copied to another file without carrier returns. (a single line )

Does anybody now how to do this

Thanks for any help

Rene
 
Old 01-01-2004, 04:04 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Which language?
 
Old 01-01-2004, 04:10 PM   #3
eicherlist
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Original Poster
Rep: Reputation: 0
I have a rh9 box
bash scripting or perl

thank you for replay
Rene
 
Old 01-01-2004, 04:14 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Code:
cat <your files> | fgrep -A2 "<your string>" | sed -e 1d -e 's/\n/ /' >your_output_file
 
Old 01-01-2004, 04:32 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Oops, code in previous post does not put all lines on 1 line.
This one does:
Code:
cat <your files> | fgrep -A2 "<your string>" | sed -e 1d -e ':a; $!N; s/\n/ /; ta' >outputfile.txt
(Change the number in "-A2" to the number of lines (after the foudn string) you want in the output)
 
Old 01-01-2004, 04:58 PM   #6
eicherlist
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Original Poster
Rep: Reputation: 0
really close
thank you Hko

I need the stuff after the matching string. Now comes the hole line. Do you now a solution for this?

Thanks again
Rene
 
Old 01-01-2004, 06:34 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by eicherlist
I need the stuff after the matching string.
Do you mean the stuff on the same line after the matching string?
But also the next two lines after the line the string was found in?
Code:
#!/bin/bash

STRING="your string"

cat yourfiles | sed -n -e "/$STRING/ {; s/.*$STRING\(.*\)/\1/; N; N; s/\n/ /g; P; }" >output.txt
Put more or less times 'N; ' in to have more or less lines that come after the line wih the matcing string.

Last edited by Hko; 01-01-2004 at 06:38 PM.
 
Old 01-01-2004, 06:45 PM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Or are you not interested in the lines after a match, but only want the content after the matching string on the same line?
Code:
cat yourfiles | sed -n -e "s/.*your string\(.*\)/\1/p"
 
Old 01-02-2004, 05:06 AM   #9
eicherlist
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Original Poster
Rep: Reputation: 0
Hi Hko

Sorry I was not excatly. Your last example comes the solution very close. It does catch the entries I need.

But I tried by myself to build in the hack the result to write on one line.
I tried:
cat yourfiles | sed -n -e ':a; $!N; s/.*yoursting\(.*\)/\1/p; ta' > neu
:-(

Do you now this trick also? And could you please explain your solution. It looks so very cryptic... :-)
Thanks again
Rene
 
Old 01-02-2004, 06:21 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
OK. antoher try:
Code:
#!/bin/bash

STRING="yourstring"

cat yourfiles | sed -n -e "s/.*$STRING\(.*\)/\1/p" | sed ':a; $!N; s/\n/ /; ta'
Hope this is what you're looking for.

(at this moment I don't have time to explain, maybe later. Do you already know some basics about sed?)
 
Old 01-02-2004, 07:05 AM   #11
eicherlist
LQ Newbie
 
Registered: Dec 2003
Posts: 11

Original Poster
Rep: Reputation: 0
YES thats what I need.
and
no im really new at this stuff...
you now maybe some good links to start with hyroglific (or what ever) :-))
Thank you again
Rene
 
Old 01-02-2004, 10:07 AM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
sed reads a file line by line. It edits it according to the sed-script, and then prints the line. After that it read the next line, and runs the sed-script on it from the start. Bu default sed prints every line wether it was changed or not. The "-n" switch tells it not to do that, but only print when the sed-script says so.

The sed command "s/some string/replacement/" replaces "some string" with "replacement" in a each line. You can use "regular expressions" ("regexp") to match some string. Regexp's work similar to file-name matching with wildcards in the shell (cat *.txt), but more complex and with more possibilities. A dot ( . ) means "any character" and a asterisk ( * ) means any number of the character before it. So:

s/.*some string/replacement/

Will replace everything from the start of the line up to and including "some string" with "replacement". In the replacement you can use "\1" to refer to the part in the matched string that is in between "\(" and "\)", so:

s/.*some string\(.*\)/\1/p

Will replace any entire line that has "some string" in it with the stuff that is between "\(" and "\)", which is, in this case, the stuff after "some string" until the end of the line. The "p" tells sed to print the line only if a replacement was done (remember the "-n" switch).

To have the results on one line, we want to remove the newline characters ("\n"). to do this the results are piped ( | ) through another sed command.

This time sed has to print everything, so no "-n" switch here. the only thing it has to do is replace newlines with spaces. We can do this with "s/\n/ /". But there's one problem: sed does things line by line, so normally newlines are not available. To solve this, the "N" command appends the next line into sed's editing buffer (called "pattern space" in sed jargon). Now two lines are in sed's buffer, seperated by a newline ("\n") and we can replace the "\n" with a space " ". But this still works only for one extra line. We need to read every line, one by one, into the buffer and replace the newline:
Code:
:a
$!N
s/\n/ /
ta
":a" is just a label. It is a mark where to jump to later.
"$!N" means: read next line into the buffer (N), exept for the last line ('$' means "last", and '!' means "not")
"s/\n/ /" replaces a newline with a space.
"ta" means: if before there was a replacement, jump to the place in the script labeled ":a". If there is another line in the input, it will always find a newline, and thus will always jump to ":a". If there are no more lines in the input, it wil not jump, end the script, and then output the edited buffer.

The above sed-script is multi-line sed script (for clarity), but it can be put on one line by using semicolons ( ; ) to seperate the different sed-commands.

For more info about sed:

Type "info sed" in a terminal.
and/or read: http://go.to/sed-faq

Last edited by Hko; 01-02-2004 at 10:12 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace string Johng Programming 9 01-13-2010 04:50 AM
Find a value following a string sgracelin Linux - Newbie 1 07-22-2005 08:41 AM
How to find a string in a directory? iclinux Linux - Newbie 2 07-02-2005 08:20 AM
how to write n saved a string to a file? geminigal Programming 3 06-08-2005 08:40 AM
Exercise 1-9. Write a program to copy its input to its output, replacing each string zombi3 Programming 3 12-21-2003 02:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration