need shell script to read particular records from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need shell script to read particular records from a file
# 1  
Old 05-19-2009
need shell script to read particular records from a file

i am reading an i/p file input.txt as below and want to read all filenames as in highlighted in bold below and put them in a different file output.txt. can someone help me with a shell script to do this? thanks in advance

regards
brad

input.txt
---------
START
TYPE:OPT
INIT_SEQ:01
FINAL_SEQ:01
DATETIME:20090424174603 FILENAME:TESTDAY.0428.20090424174530.01.OP FILE_SIZE:462 NUMBER_DOCUMENTS:1
DATETIME:20090424174604 FILENAME:TESTDAY.0428.20090424174540.01.OP FILE_SIZE:462 NUMBER_DOCUMENTS:1
DATETIME:20090424174603 FILENAME:TESTMON.0428.20090424174530.01.OP FILE_SIZE:786 NUMBER_DOCUMENTS:1
DATETIME:20090424174604 FILENAME:TESTMON.0428.20090424174540.01.OP FILE_SIZE:786 NUMBER_DOCUMENTS:1
TOTAL_FILES:4
TOTAL_DOCUMENTS:2
TOTAL_ACCOUNTS:1

output.txt
----------
TESTDAY.0428.20090424174540.01.OP
TESTDAY.0428.20090424174540.01.OP
TESTMON.0428.20090424174530.01.OP
TESTMON.0428.20090424174540.01.OP
# 2  
Old 05-19-2009
if you have Python
Code:
#!/usr/bin/env python
for line in open("file"):
    if line.startswith("DATETIME"):
        print line.split(":")[2].split()[0]

output
Code:
# ./test.py
TESTDAY.0428.20090424174530.01.OP
TESTDAY.0428.20090424174540.01.OP
TESTMON.0428.20090424174530.01.OP
TESTMON.0428.20090424174540.01.OP

awk
Code:
awk -F"[: ]" '/DATETIME/{print $4}' file


Last edited by ghostdog74; 05-19-2009 at 11:17 PM..
# 3  
Old 05-19-2009
Unfortunately i dont have python and I need only shell script to perform this task. any help shall be greatly appreciated.
# 4  
Old 05-20-2009
Code:
awk -F'[:| ]' '$4{print $4}' input.txt > new.file

# 5  
Old 05-20-2009
Or

Code:
sed -n '/^DATETIME/{s/^DATETIME:.*FILENAME:\(.*\) FILE_SIZE.*/\1/g;p}' file


-Devaraj Takhellambam
# 6  
Old 05-20-2009
thanks guys. all of the scripts works awesome.

thanks again.
# 7  
Old 05-20-2009
And then some more:

Code:
$
$ perl -ne 'split/[: ]/; /^DATETIME:/ && print $_[3],"\n"' input.txt
TESTDAY.0428.20090424174530.01.OP
TESTDAY.0428.20090424174540.01.OP
TESTMON.0428.20090424174530.01.OP
TESTMON.0428.20090424174540.01.OP
$
$
$ grep ^DATETIME: input.txt | cut -d: -f3 | cut -d' ' -f1
TESTDAY.0428.20090424174530.01.OP
TESTDAY.0428.20090424174540.01.OP
TESTMON.0428.20090424174530.01.OP
TESTMON.0428.20090424174540.01.OP
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to filter records in a zip file that contains matching columns from another file

Not sure if this is the correct forum for this question. I have two files. file1.zip, file2 Input: file1.zip col1, col2 , col3 a , b , 0:0:0:0:0:c436:9346:d40b x, y, 0:0:0:0:0:880:39f9:c9a7 m, n , 0:0:0:0:0:80c7:9161:fe00 file2.txt col1 c4:36:93:46:d4:0b... (1 Reply)
Discussion started by: anil.v
1 Replies

2. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

3. Shell Programming and Scripting

How to read a file through shell script?

hi all, i have to read a file using shell script for example my csv file is like this Tid Inputfille Inputfilepath 1 ABC_20141218.idr /export/home/him60t1/input 2 ABC_20141219.idr /export/home/him60t1/input1 what i have to do is if on my command line i... (1 Reply)
Discussion started by: ramsavi
1 Replies

4. Shell Programming and Scripting

How to read a particular records from a file?

Hi All Can anybody let me know the code to read a particular record from a file For e.g. File Name: File.txt File content: Script_path=/abc/def/script/ File_path=/xyz/data/ Business Date=19990905 SERVER_NAME=Server DATABASE_NAME=Database Login=NewUser Password=NewPassword ... (3 Replies)
Discussion started by: Siddhartha9833
3 Replies

5. Shell Programming and Scripting

How to read records in a file and sort it?

I have a file which has number of pipe delimited records. I am able to read the records....but I want to sort it after reading. i=0 while IFS="|" read -r usrId dataOwn expire email group secProf startDt endDt smhRole RoleCat DataProf SysRole MesgRole SearchProf do print $usrId $dataOwn... (4 Replies)
Discussion started by: harish468
4 Replies

6. Shell Programming and Scripting

How to read each 2 records from a file

Hi, I have 10000 records in my test.dat file All records are under the following format a,12,45,bn,c a,16,46,bn1,c a,18,47,bn2,c a,12,47,bn3,c a,11,49,bn4,c I have to read each 2 records and assign it into a temp file .Can anybody help me in this? Thanks (3 Replies)
Discussion started by: kavithakuttyk
3 Replies

7. Shell Programming and Scripting

how to read dbf file in shell script and to convert dbf file usinf shell script

Hi all, I am new to shell scripting. I have dbf file and I need to convert it into csv file. OR, can i read the fields from a .dbf file and OR seprate the records in dbf file and put into .csv or txt. Actually in the .dbf files I am getting , the numbers of fields may vary in very record and... (6 Replies)
Discussion started by: gauara
6 Replies

8. UNIX for Dummies Questions & Answers

update records in a file using shell script...

Hi, I have a file with 6 columns. First 3 columns together make unique record. I have a variable ($v) which hold a value that is obtained by a caliculaion. I have to replace value in 5th columnn with the value of the variable ($v). The value $v is caliculated from col4 and col6 values. ... (2 Replies)
Discussion started by: new_learner
2 Replies

9. Shell Programming and Scripting

unix shell script which inserts some records into a file located in remote servers...

All, I need to write an unix shell script which inserts some records into a file located in remote servers. * Get the input from the user and insert according the first row. It should be in ascending order. 123451,XA,ABA 123452,XB,ABB 123453,XC,ABC 123455,XE,ABE 123456,XF,ABF 123458,XG,ABG... (2 Replies)
Discussion started by: techychap
2 Replies

10. Shell Programming and Scripting

read records from a file

Hi all, I have a requirement where I need to read records one by one from a file. I have tried this below code: while read mLine do echo 'Line = '${mLine} done < input_file --- But the problem here is im getting the records with removed spaces. --Supposer if the record is like... (3 Replies)
Discussion started by: srilaxmi
3 Replies
Login or Register to Ask a Question

Featured Tech Videos