LinuxQuestions.org
Visit Jeremy's Blog.
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 06-25-2012, 05:25 PM   #1
jlrod912
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Rep: Reputation: Disabled
Writing multiple files from one file after comparing each line


I have one text input file that contains times (HHMMSS) on each line. Each line is compared with the next line and if a condition occurs (time between each consecutive line is less then 90 seconds) the lines are written to a file. The input file may contain several lines that are duplicate lines or consecutive lines that are more than 90 seconds apart. I want to read the input file and write as many separate files that contain consecutive times that meet the time test of 90 seconds or less. I already have the formula to compare the times. Does anyone have any suggestions on how to write this in a bash script? Thanks in advance for your help.

Example of 1 input file that should generate 3 output files follows:
FileIn.txt
120749
120749
120749
120914
121040
121205
121331
121456
121621
121747
121912
122038
122203
122328
122452
122617
122743
122743
122743
122908
123034
123512
123800
124310
124320
124330

There should be three separate output files as follows:
FileOut1.txt
120914
121040
121205
121331
121456
121621
121747
121912
122038
122203
122328
122452
122617

FileOut2.txt
122908
123034

FileOut3.txt
124310
124320
124330
 
Old 06-25-2012, 07:01 PM   #2
rosehosting.com
Member
 
Registered: Jun 2012
Posts: 222

Rep: Reputation: 62
If you want to use plain bash, go with arrays.

First create an array from your file

Code:
while read line
do
    array+=("$line")
done < FileIn.txt
Loop through the array, and use your formula

Code:
for (( i = 0 ; i < $((${#array[@]}-1)) ; i++ ))
do
    #Your formula

done
For example to compare if time between each consecutive line is less then 90 seconds
Code:
    if [ $((${array[$i]}+90)) -lt ${array[$(($i + 1))]} ] ;then 
        echo "${array[i]}"
    fi
 
Old 06-26-2012, 04:05 PM   #3
jlrod912
LQ Newbie
 
Registered: Jun 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Question

Thank you, that code works great. How do I implement the part where I make the 3 separate output files
from the one input file after I have all the times in one array? I am having problems putting all the grouped times that meet the less than 90 seconds requirement in separate files. Thanks in advance again for your help.


Quote:
Originally Posted by rosehosting.com View Post
If you want to use plain bash, go with arrays.

First create an array from your file

Code:
while read line
do
    array+=("$line")
done < FileIn.txt
Loop through the array, and use your formula

Code:
for (( i = 0 ; i < $((${#array[@]}-1)) ; i++ ))
do
    #Your formula

done
For example to compare if time between each consecutive line is less then 90 seconds
Code:
    if [ $((${array[$i]}+90)) -lt ${array[$(($i + 1))]} ] ;then 
        echo "${array[i]}"
    fi
 
  


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
[SOLVED] Comparing two files and looking for the same line based on field - awk sopier Programming 8 12-26-2011 03:53 PM
bash reading multiple files line by line. danraider Programming 3 03-08-2011 08:54 AM
BASH: Each line of multiple text files gets added to one line Gavin Harper Programming 3 09-12-2010 08:31 PM
Search values within multiple files line to line Chrizzieej Programming 5 09-26-2008 05:11 PM
Comparing two files to get matched contents in another file using shell script pooppp Linux - Networking 3 08-05-2008 01:11 AM


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