Extract the lines from input file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the lines from input file
# 8  
Old 07-11-2010
Hi, Try this...
Code:
date1=`date -d "20100705" '+%d\/%b\/%Y' | cut -c2-`
date2=`date -d "20100706" '+%d\/%b\/%Y' | cut -c2-` 

sort -y -T /dir/having/enough_free_space/ -k2 test | sed -n "/$date1:06:/,/$date2:09:/p"

# 9  
Old 07-11-2010
Hi
Solution without sort since its a huge file:

File containing reference dates:

Code:
$ cat text
05/Jul/2010:07
06/Jul/2010:08

Script:

Code:
$ cat try

#!/usr/bin/ksh

  awk -F "[:/]" '
  BEGIN{
    a["Jan"]="01";
    a["Feb"]="02";
    a["Mar"]="03";
    a["Apr"]="04";
    a["May"]="05";
    a["Jun"]="06";
    a["Jul"]="07";
    a["Aug"]="08";
    a["Sep"]="09";
    a["Oct"]="10";
    a["Nov"]="11";
    a["Dec"]="12";
    getline < "text"
    date=int($1 a[$2] $3 $4 "00")
    getline < "text"
    date1=int($1 a[$2] $3 $4 "00")
   }{
     x=int(substr($1,length($1)-1) a[$2] $3 $4 $5)
     if (x >=date && x<=date1)
       print;
  }' infile

where infile is your input file

On running
Code:
$./try

b    05/Jul/2010:07: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


Guru.
# 10  
Old 07-11-2010
Quote:
Originally Posted by pravin27
Hi, Try this...
Code:
date1=`date -d "20100705" '+%d\/%b\/%Y' | cut -c2-`
date2=`date -d "20100706" '+%d\/%b\/%Y' | cut -c2-`

sort -y -T /dir/having/enough_free_space/ -k2 test | sed -n "/$date1:06:/,/$date2:09:/p"

IMO this isn't going to work, since it will be coincidental that an alphabetic sort works in this particular example. The line should be sorted by date which is not trivial. Also, the sed statement will only work if both begin stamp and end stamp happen to be present, which is not guaranteed.

---------- Post updated at 11:33 ---------- Previous update was at 10:16 ----------

Try a script like this, which does not call any external programs:
Code:
getdate()
{
  IFS="$IFS:/"
  set -- $*
  case $2 in
    Jan) mon=01;;
    Feb) mon=02;;
    Mar) mon=03;;
    Apr) mon=04;;
    May) mon=05;;
    Jun) mon=06;;
    Jul) mon=07;;
    Aug) mon=08;;
    Sep) mon=09;;
    Oct) mon=10;;
    Nov) mon=11;;
    Dec) mon=12;;
  esac
  echo $3$mon$1$4$5$6
}
var1=20100705
var2=20100706
startdate=${var1}070000
enddate=${var2}080000

while read line
do
  stamp=${line##*[[:space:]]}
  linedate=$(getdate "$stamp")
  if [ $startdate -le $linedate ] && [ $enddate -gt $linedate ]; then
    printf "%s\n" "$line"
  fi
done <infile >outfile

and see if it is fast enough. Otherwise it will need to be awkified....

Last edited by Scrutinizer; 07-11-2010 at 06:38 AM..
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