LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   PLEASE HELP! Shell Script to do the following.... (https://www.linuxquestions.org/questions/programming-9/please-help-shell-script-to-do-the-following-918193/)

Yow 12-11-2011 12:01 PM

PLEASE HELP! Shell Script to do the following....
 
Dear All,

I've been trying to write a shell script that does the following, with no success! :(:


- Read a file and search for a particular string

- Redirect everything before that string to File1

- Redirect everything after that string to File2

I look forward to receiving help.

Best Regards;
Yow

asimba 12-11-2011 12:11 PM

Code:

while read line   
do   
    command 


done <filename

you can add grep and if to redirect the file out as you choose.

TB0ne 12-11-2011 12:17 PM

Quote:

Originally Posted by Yow (Post 4547268)
Dear All,
I've been trying to write a shell script that does the following, with no success! :(:

- Read a file and search for a particular string
- Redirect everything before that string to File1
- Redirect everything after that string to File2

I look forward to receiving help.

We'll be glad to HELP you, but we're not going to do your homework FOR YOU. If you've had no success, that means you've tried...so, post what you've written, and what error(s) you get, and we'll be glad to assist. Otherwise, reference your textbooks, ask your teacher for help, or look up any of the bash scripting tutorials you can easily find on Google, like this one:
http://tldp.org/LDP/abs/html/

Break the task down into steps. How would you read a file? How would you search for a string? Save a position in a file? Even keying in "linux bash read file search for string" into Google pulls up lots of examples:
http://linuxconfig.org/Bash_scripting_Tutorial

Yow 12-11-2011 12:18 PM

Thanks for the response TBone, and I totally agree with you.

The reason I came here is because I wanted help quickly as I need this for something at work that needs to go live tonight.

My problem is as follows:

Lets say I have the following text file. What I need is a script to read the file and redirect the first conversation (Before "Disconnected", with Maria) to File1, and then the second conversation (After "Disconnected", with John) to File2.

=====================
Hi Maria? How're you doing?
I'm doing well, how about you?
I'm good too.
Disconnected
Hi John, what's going on?
It's been a while.
======================

I'll very much appreciate the response, and I need this badly :(

TB0ne 12-11-2011 12:36 PM

Quote:

Originally Posted by Yow (Post 4547285)
Thanks for the response TBone, and I totally agree with you.
The reason I came here is because I wanted help quickly as I need this for something at work that needs to go live tonight.

At WORK??? Honestly, I have a VERY hard time believing this is a work assignment, and not homework. If it IS for work, why would they give the assignment to someone who has no experience in bash scripting, and give them a deadline of one night? And what purpose/task would parsing a simple text file have in a business enterprise? Even if it was for evidence gathering in a lawsuit, they need the unedited/unaltered files, not ones that have been tampered with/edited.
Quote:

My problem is as follows:
Lets say I have the following text file. What I need is a script to read the file and redirect the first conversation (Before "Disconnected", with Maria) to File1, and then the second conversation (After "Disconnected", with John) to File2.
=====================
Hi Maria? How're you doing?
I'm doing well, how about you?
I'm good too.
Disconnected
Hi John, what's going on?
It's been a while.
======================
I'll very much appreciate the response, and I need this badly :(
We understand what you're asking. And again I reiterate: you say you've had no success; post what you've written/tried, and we'll help. The two tutorials I gave you (which I found with a Google search, as you could have), gave you all the tools and examples you'll need to get this done, specifically the second link.

So:
  • Read the file into an array
  • Define a string to match
  • Read array1 into array2, until you hit the match string
  • Output array2 into your first file
  • Output the remaining bit of the array1 into your second file

Cedrik 12-11-2011 01:20 PM

Code:

csplit bla.txt /Disconnected/
:p

asimba 12-11-2011 01:24 PM

Quick and dirty script .

modify it as as need be.

Code:


search_disconnect=0
disconnected=0

while read line ; do

        #search for word disconnect
        search_disconnect=`echo $line | grep -i disconnect`

        #if found -  flag disconnect
        if  [ ! -z  "$search_disconnect" ]; then
                disconnected=1
        fi

        if [ $disconnected -eq 0 ]; then
                echo $line >> file1
        else
                echo $line >> file2
        fi


done < chat
echo "... done."



All times are GMT -5. The time now is 10:15 AM.