LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-10-2011, 06:36 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
Create files for specific output.


Hi,

I would like to create a bash script that takes the following text and split in different files. The output is:

Code:
JOB ID: 1
JOB NAME: Test1
01012010 Message: Success1
01012010 Message: Success2
JOB ID: 2
JOB NAME: Test2
02012010 Message: Success3
02012010 Message: Success4
I would like to create one file for JOB ID 1, and another file for JOB ID 2.

Any help, please?
 
Old 03-10-2011, 06:40 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

You state yourself that you would like to create a bash script. What have you got already and where do you need help? LQ users are not in the habit of providing ready made solutions but are at their best solving problems.

If the 'blocks' in the file are identical, you can loop through the file line by line, create a file based on the JOB ID and copy all lines following the JOB ID instance to that file until you encounter the next JOB ID.

Kind regards,

Eric
 
Old 03-10-2011, 06:55 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
But the problem is that I don't know how can I find the end of each section in bash, to redirect the content of each section to file.

I don't want the solution. I want a suggestion on how can I find the start and the end of each section, and for each section I would like to redirect the output.

I've really don't have any clue on how to do it, and I've beginners knowledge of bash script.
 
Old 03-10-2011, 07:34 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hi,

Ok, let's take it from the start using the basics. You'll need to loop through your text file and process it line by line.
Code:
cat yourfile.txt | while read line
do
this part will process your textfile line by line and 'do' something. Now, the first thing to do is check if it (the line that's being processed) contains ID and if it does get the number of the job id:
Code:
case "$line" in
*ID*)
JOBNR=`echo "$line" |awk -F: '{ print $2 }'`;
At this point we've got the jobnumber of the first instance of JOB ID we encountered and have saved it to the variable JOBNR. Now create a file that includes that JOBNR in its name to be unique.
Code:
touch output"$JOBNR".txt;
;;
If the blocks are identical then the following lines will not contain ID, so we just copy all following lines to the newly created file until we encounter a line that contains ID again.
Code:
*)
echo "$line" >>output"$JOBNR".txt
;;
esac
done
That should take care of the basics.

Some good reading to learn the basics of the commands used: Bash Guide for Beginners, AWK and of course the man pages.

Put it all together, add what's missing and see if it works. Mind you, this is only the functional part, a very small and simple script that does what you need with the kind of content you pointed out.

Kind regards,

Eric

Last edited by EricTRA; 03-10-2011 at 07:36 AM.
 
Old 03-10-2011, 08:00 AM   #5
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Thank you Eric for your response. I'll start it from your solution.
 
Old 03-10-2011, 08:01 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

You're welcome. Be sure to post if there's something you don't understand and go through the links I posted.

Kind regards,

Eric
 
Old 03-10-2011, 08:19 AM   #7
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
Thank you for your help, your solution solved my problem.

I've really had no idea on how to do it.
 
Old 03-10-2011, 08:21 AM   #8
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

Glad to be of help and to hear you found a solution. Thanks also for marking the thread solved.

Kind regards,

Eric
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Create a file from two files with specific search conditions.(a bit Difficult) vysakh@gmail.com Linux - Server 4 05-06-2010 10:18 AM
Output specific line in file cgkee Linux - Newbie 5 09-15-2009 04:08 AM
Remove specific text in output line dwarf007 Linux - General 8 07-04-2007 12:28 PM
filter out lines containing specific keywords from output kpachopoulos Linux - General 2 03-12-2007 09:29 PM
cat: output specific number of lines mikeshn Linux - Software 3 12-31-2003 01:15 PM


All times are GMT -5. The time now is 02:27 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration