ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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
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."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.