Extract the lines from input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the lines from input file
# 1  
Old 07-08-2010
Extract the lines from input file

This is the sample input file

Code:
b    05/Jul/2010:07:00:10
a    05/Jul/2010:06:00:10
b    05/Jul/2010:07:00:10
c    05/Jul/2010:07:10:10
d    05/Jul/2010:08:00:10
e    05/Jul/2010:09:00:10
f    05/Jul/2010:10:00:10
h    05/Jul/2010:11:00:10
i    05/Jul/2010:12:00:10
j    05/Jul/2010:13:00:10
k    05/Jul/2010:14:00:10
l    05/Jul/2010:15:00:10
m    05/Jul/2010:16:00:10
n    05/Jul/2010:17:00:10
o    05/Jul/2010:18:00:10
p    05/Jul/2010:19:00:10
q    05/Jul/2010:20:00:10
a    05/Jul/2010:21:00:10
b    05/Jul/2010:22:00:10
v    05/Jul/2010:23:00:10
g    06/Jul/2010:01:00:10
k    06/Jul/2010:02:00:10
i    06/Jul/2010:03:00:10
j    06/Jul/2010:04:00:10
k    06/Jul/2010:05:00:10
l    06/Jul/2010:06:00:10
m    06/Jul/2010:07:00:10
n    06/Jul/2010:08:00:10
n    06/Jul/2010:09:00:10


I have a file, how to extract only the lines between the timestamp 05/Jul/2010:07 to 06/Jul/2010/2010:08

I mean, 05/Jul/2010:06:00:10,06/Jul/2010:09:00:10,etc should not be in the output file.

from 5th July, after 7:00 AM till next day 8:00AM should be in the output file
where 05 is three day old date and 06 is 2 day old date.

The input file is very large, and how get the output file much faster
# 2  
Old 07-08-2010
Hi, Try this

Code:
sort -k2 filename| sed -n '/05\/Jul\/2010:07:00:10/,/06\/Jul\/2010:08:00:10/p'

# 3  
Old 07-08-2010
Can you please explain me how it works.

I want delete all the lines, in which timestamp doesn't falls in between 05/Jul/2010:07 to 06/Jul/2010:08.

---------- Post updated at 06:13 AM ---------- Previous update was at 06:00 AM ----------

File contains 19739530 lines. Is sorting expensive. Is there any other way?
# 4  
Old 07-08-2010
Hi,
1) Sort the file on 2nd column
2) Using SED select the range of lines using pattern

Code:
sort -k2 filename| sed -n '/05\/Jul\/2010:07:00/,/06\/Jul\/2010:08:00/p'



---------- Post updated at 07:28 AM ---------- Previous update was at 07:15 AM ----------

Hi,

You may want to use the -T <directory> option to force sort to use a scratch directory that has enough free space. By default it uses TMPDIR - usually /tmp or var/tmp.

If you need to sort the file quickly use -y option with no argument for the option. This starts sort with the maximum free memory allowed or available.
Code:
sort -y -T /dir/having/enough_free_space/ -k2 test | sed -n '/05\/Jul\/2010:07:00/,/06\/Jul\/2010:08:00/p'

# 5  
Old 07-09-2010
Thanks,

If the input file is

Code:
b    05/Jul/2010:07:00:10
a    05/Jul/2010:06:00:09
b    05/Jul/2010:07:00:10
c    05/Jul/2010:07:10:16
d    05/Jul/2010:08:00:10
e    05/Jul/2010:09:00:10
f    05/Jul/2010:10:00:10
h    05/Jul/2010:11:00:10
i    05/Jul/2010:12:00:20
j    05/Jul/2010:13:00:10
k    05/Jul/2010:14:00:10
l    05/Jul/2010:15:00:30
m    05/Jul/2010:16:00:10
n    05/Jul/2010:17:00:10
o    05/Jul/2010:18:00:10
p    05/Jul/2010:19:00:40
q    05/Jul/2010:20:00:10
a    05/Jul/2010:21:00:10
b    05/Jul/2010:22:00:50
v    05/Jul/2010:23:00:20
g    06/Jul/2010:01:00:10
k    06/Jul/2010:02:00:10
i    06/Jul/2010:03:00:14
j    06/Jul/2010:04:00:10
k    06/Jul/2010:05:00:18
l    06/Jul/2010:06:00:10
m    06/Jul/2010:07:00:10
n    06/Jul/2010:08:00:19
n    06/Jul/2010:09:00:10

After sorting the file, I want all the line, starting from 05/Jul/2010:06:00:00 to 06/Jul/2010:10:00:00 into another file.
# 6  
Old 07-09-2010
If the data file has the below data;

Code:
b    05/Jul/2010:07:00:10
a    05/Jul/2010:06:00:09
b    05/Jul/2010:07:00:10
c    05/Jul/2010:07:10:16
d    05/Jul/2010:08:00:10
e    05/Jul/2010:09:00:10
f    05/Jul/2010:10:00:10
h    05/Jul/2010:11:00:10
i    05/Jul/2010:12:00:20
j    05/Jul/2010:13:00:10
k    05/Jul/2010:14:00:10
l    05/Jul/2010:15:00:30
m    05/Jul/2010:16:00:10
n    05/Jul/2010:17:00:10
o    05/Jul/2010:18:00:10
p    05/Jul/2010:19:00:40
q    05/Jul/2010:20:00:10
a    05/Jul/2010:21:00:10
b    05/Jul/2010:22:00:50
v    05/Jul/2010:23:00:20
g    06/Jul/2010:01:00:10
k    06/Jul/2010:02:00:10
i    06/Jul/2010:03:00:14
j    06/Jul/2010:04:00:10
k    06/Jul/2010:05:00:18
l    06/Jul/2010:06:00:10
m    06/Jul/2010:07:00:10
n    06/Jul/2010:08:00:19
n    06/Jul/2010:09:00:10

then try the below command

Code:
 
 sed -n '/05\/Jul\/2010:06:00:09/,/06\/Jul\/2010:09:00:10/ p' data > output

This will redirect the output lines to file "output". Hope this helps.
# 7  
Old 07-09-2010
sed -n '/[5\/Jul\/2010:06:00:00/,/[6\/Jul\/2010:09:00:00/p' soredfile.tsv > output
sed: -e expression #1, char 50: unterminated address regex

Code:
b    [5/Jul/2010:07:00:10
a    [5/Jul/2010:06:00:09
b    [5/Jul/2010:07:00:10
c    [5/Jul/2010:07:10:16
d    [5/Jul/2010:08:00:10
e    [5/Jul/2010:09:00:10
f    [5/Jul/2010:10:00:10
h    [5/Jul/2010:11:00:10
i    [5/Jul/2010:12:00:20
j    [5/Jul/2010:13:00:10
k    [5/Jul/2010:14:00:10
l    [5/Jul/2010:15:00:30
m    [5/Jul/2010:16:00:10
n    [5/Jul/2010:17:00:10
o    [5/Jul/2010:18:00:10
p    [5/Jul/2010:19:00:40
q    [5/Jul/2010:20:00:10
a    [5/Jul/2010:21:00:10
b    [5/Jul/2010:22:00:50
v    [5/Jul/2010:23:00:20
g    [6/Jul/2010:01:00:10
k    [6/Jul/2010:02:00:10
i    [6/Jul/2010:03:00:14
j    [6/Jul/2010:04:00:10
k    [6/Jul/2010:05:00:18
l    [6/Jul/2010:06:00:10
m    [6/Jul/2010:07:00:10
n    [6/Jul/2010:08:00:19
n    [6/Jul/2010:09:00:10



---------- Post updated at 04:47 AM ---------- Previous update was at 04:13 AM ----------

Thanks, this worked fine.

Another problem is

I have the date in the variable as 20100705 and 20100706.

How to convert the date as 5/Jul/2010 and 6/Jul/2010 and pass the dates as a variable in the sed command


sort -k2 tmp.tsv | sed -n '/\[5\/Jul\/2010:06:00:00/,/\[6\/Jul\/2010:09:00:00/p' > tab.tsv

---------- Post updated at 07:02 AM ---------- Previous update was at 04:47 AM ----------

How to pass the variable
$date1
$date2
where $date1 is ' 6\/Jul\/2010'
and $date2 is '7\/Jul\/2010'
sort -k4 tmp.tsv | sed -n '/\[$date1:06:00:00/,/\[$date2:09:00:00/p' > tab.tsv
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to extract file prefix and from input to use in output

In the bash below which does execute I am trying to extract the contents of ${id} is 1234, as ${id} stores the variable that changes each time. After the path is removed the contents of ${id} are stored in pref, so they can be used in the output. Currently I am not able to extract the 1234 in the... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Extract lines from a file

Hi all; Here is my file which contains a list of files (recent versions of files are in red). This file is dynamic, files versions can change at any time (versions can increment) filename ------------------------------------------------------- ... (8 Replies)
Discussion started by: chercheur111
8 Replies

3. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

4. Shell Programming and Scripting

How to extract certain lines from a file?

Hi guys I have a several thousands line file in the following format: n817 -------------------------------------------------- n842 -------------------------------------------------- n877 -------------------------------------------------- n513 /bb/data/rmt2db.lrl:JBSKDB 31915 75... (4 Replies)
Discussion started by: aoussenko
4 Replies

5. Shell Programming and Scripting

Extract particular lines from a file

Hi all, I have a file with many records with information as given below ID A16L2_HUMAN Reviewed; 619 AA. AC Q8NAA4; A5PL30; B2RPK5; Q658V4; Q6PID3; Q8NBG0; DT 20-MAY-2008, integrated into UniProtKB/Swiss-Prot. DT 20-MAY-2008, sequence version 2. DT ... (1 Reply)
Discussion started by: kaav06
1 Replies

6. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

7. Shell Programming and Scripting

input text at certain lines of a file

Hi, I have an xml file which will be edited by the user. I would like to get input from user and insert that at line 40 of the xml file. PLease can some one help me to know how to insert the text at specified line using shell script. (6 Replies)
Discussion started by: sunrexstar
6 Replies

8. Shell Programming and Scripting

how do i join two lines from a input file

HI all, i have a input file,i have to join 2nd line into firstline and 4th line into 2nd line and so on.. for this we also consider odd no. of line.It's operate on original input file but output file should temp file. like .. filename=cdr.cfg line1 line2 line3 line4Desired output should be... (9 Replies)
Discussion started by: suryanarayan
9 Replies

9. Shell Programming and Scripting

extract particular lines from text file

I have two files file A which have a number in every row and file B which contains few hundred thousand rows with about 300 characters in each row (csv) What I need is to extract whole rows from B file (only these which numbers are indicated in A file) I also need to use cygwin. Any... (7 Replies)
Discussion started by: gunio
7 Replies

10. Shell Programming and Scripting

How to extract a sequence of n lines from a file

Hi I want to be able to extract a sequence of n lines from a file. ideas, commands and suggestions would be highly appreciated. Thanks (4 Replies)
Discussion started by: 0ktalmagik
4 Replies
Login or Register to Ask a Question