1753798 Members
7947 Online
108805 Solutions
New Discussion юеВ

Question about script

 
SOLVED
Go to solution
Anthony_69
Occasional Contributor

Question about script

Hi....wondering if you could help....
i have a file with block of approx 20 lines similar to the below, repeated 100's of times througout the file:

type: 114
id: 3
version: 3
date: 2003-06-25 10:30:55
node: 0
application: 0
length: 75345
class: 234
service: 0
content: 0
report: 0
timer: 0
count time: 0
input field: 0


The problem i that I need to output the line listed "class" and the line listed "date" together to a seperate file. I need this to be repeated from each block of 20 lines throughout the file. Any ideas?
Thanks
12 REPLIES 12
Kenneth_19
Trusted Contributor
Solution

Re: Question about script

Try:

grep -E 'class|date' input_file > output_file

Kenneth
Always take care of your dearest before it is too late
Bill Douglass
Esteemed Contributor

Re: Question about script

If you're putting this pair of lines from each block into the same file, then


grep -e ^date: -e ^class: > /some/file

should do the trick.
Bill Douglass
Esteemed Contributor

Re: Question about script

Sorry, typo in my replyt:



grep -e ^date: -e ^class: /path/to/file > /some/file

Steve Steel
Honored Contributor

Re: Question about script

Hi

grep -e class -e date file > file.out


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Dario_1
Trusted Contributor

Re: Question about script

Hi!

Use the grep command to do what you want. See from the man page:

grep, egrep, fgrep - search a file for a pattern

SYNOPSIS
Plain call with pattern
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] pattern [file ...]

Call with (multiple) -e pattern
grep [-E|-F] [-c|-l|-q] [-bhinsvwx] -e pattern... [-e pattern] ...
[file ...]

Look at the line with the word multiple for details and re-direct to a new file like every other person mentioned (grep -e class -e date file > newfile) Check the man pages for more information.

Regards,

Dario
Anthony_69
Occasional Contributor

Re: Question about script

Thanks guys.
one final thing....
the resultant output is something like this..
.
.
.
.
class: 1234
date: 2003-6-26@08:01am
class: 0000
date: 2003-6-26@08:02am
class: 9999
date: 2003-6-26@08:03am
class: 0000
date: 2003-6-26@08:04am
class: 1234
date: 2003-6-26@08:05am
class: 0000
date: 2003-6-26@08:06am
.
.

From this file, I need to be able to list only the lines with class 0000 and their corresponding date
Jean-Louis Phelix
Honored Contributor

Re: Question about script

hi,

awk '/class: 0000/{print ; getline ; print}' file

Regards.
It works for me (┬й Bill McNAMARA ...)
Jean-Louis Phelix
Honored Contributor

Re: Question about script

Or ...

sed -n '/class: 0000/ {
N
p
}' file

Regards.
It works for me (┬й Bill McNAMARA ...)
Hai Nguyen_1
Honored Contributor

Re: Question about script

Anthony,

Assume that you have the output in a file named output.txt. Below is a script to do what you want:

#!/bin/sh

while read CLASS
do
read DATE
if [ "$CLASS" = "class: 0000" ]
then
echo $CLASS
echo $DATE
fi
done < output.txt

Hai