LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-11-2011, 12:01 PM   #1
Yow
LQ Newbie
 
Registered: May 2010
Posts: 5

Rep: Reputation: 0
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
 
Old 12-11-2011, 12:11 PM   #2
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
Code:
while read line    
do    
    command  


done <filename
you can add grep and if to redirect the file out as you choose.
 
Old 12-11-2011, 12:17 PM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Yow View Post
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
 
Old 12-11-2011, 12:18 PM   #4
Yow
LQ Newbie
 
Registered: May 2010
Posts: 5

Original Poster
Rep: Reputation: 0
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

Last edited by Yow; 12-11-2011 at 12:19 PM.
 
0 members found this post helpful.
Old 12-11-2011, 12:36 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,633

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by Yow View Post
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

Last edited by TB0ne; 12-11-2011 at 12:47 PM.
 
Old 12-11-2011, 01:20 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
csplit bla.txt /Disconnected/
 
1 members found this post helpful.
Old 12-11-2011, 01:24 PM   #7
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
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."
 
  


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
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
help with execute mulitple shell script within shell script ufmale Programming 6 09-13-2008 12:21 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

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

All times are GMT -5. The time now is 02:16 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