LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 04: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, 06:01 PM   #2
rosehosting.com
Member
 
Registered: Jun 2012
Location: Missouri, USA
Posts: 236

Rep: Reputation: 64
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, 03: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 02:53 PM
bash reading multiple files line by line. danraider Programming 3 03-08-2011 07:54 AM
BASH: Each line of multiple text files gets added to one line Gavin Harper Programming 3 09-12-2010 07:31 PM
Search values within multiple files line to line Chrizzieej Programming 5 09-26-2008 04:11 PM
Comparing two files to get matched contents in another file using shell script pooppp Linux - Networking 3 08-05-2008 12:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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