Using the Read command with grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using the Read command with grep
# 1  
Old 10-22-2008
Question Using the Read command with grep

I am still fairly new to unix scripting and I will try my best to explain my question the best I can....

I have a text file called hello.txt, and in this file I need to search every line that contains the words "help" and "cat" and move the respective lines into their own columns in another file called goodbye.txt

the hello.txt file would appear like below

>01/01/01 help 101
>I have a happy dog
>I have a happy cat
>================
>01/01/02 help 102
>I have a sad dog
>I have a sad cat

and I need the goodbye.txt file to appear like

>01/01/01 help 101 I have a happy cat
>01/01/02 help 102 I have a sad cat

any help would be greatly appreciated
# 2  
Old 10-23-2008
Code:
grep -E "help|cat" infile| sed '/help/ {N; s/\n//; s/>/ /2}'
>01/01/01 help 101 I have a happy cat
>01/01/02 help 102 I have a sad cat


Last edited by zaxxon; 10-23-2008 at 01:35 AM..
# 3  
Old 10-23-2008
let me re-word the question :)

I apologize if my request comes across as trivial or somehow looking like school work it was not my intention for it to appear that way, I was merely trying to put what I needed in the most simplest terms because I didn't want to take up a ridiculous amount of space on the thread. Below I have the actual data that I'm working with.

I know I need to utilize the read command in conjunction with my variables in order to find the information needed and redirect into the file I need ($File2) but I also need to be able to test the lines so that if an "Associate" from $File1 doesn't have a value for break, lunch, etc. it will not compromise the final product.

Report=`grep "Report Group" $File1 | grep -v Totals | grep -v ALL | cut -c55-85`
ID=`grep "^ [0-9][0-9][0-9][0-9][0-9][0-9][0-9]" $File1 | cut -c1-10`
Begin=`grep BEGINDAY $File1 | cut -c30-49|grep ^B`
Lunch=`grep LUNCH $File1 | cut -c30-49|grep ^L`

echo $Report >$File2
echo $ID >>$File2
echo $Begin >>$File2
echo $Cleanup >>$File2
echo $Lunch >>$File2
Output:
Report Group: 1 - F/C TM 1 Report Group: 1 - F/C TM 1 Report Group: 3 - F/C TM 3
1111111 2222222 3333333
BEGINDAY = 10.12 BEGINDAY = 13.75 BEGINDAY = 16.77
BREAK = 30.00 BREAK = 15.00
LUNCH = 44.30 LUNCH = 38.93

File 1
10/17/2008 16:51:52 Report Group: 1 - F/C TM 1 User:
Associate 1111111
Work Category Totals: INDIRECT = 107.92 Percentage of Day = 19.13%, Performance = 55.60%
BEGINDAY = 10.12 Percentage of Day = 1.79%, Performance = 0.00%
BREAK = 30.00 Percentage of Day = 5.32%, Performance = 100.00%
BATTERY = 23.50 Percentage of Day = 4.16%, Performance = 0.00%
LUNCH = 44.30 Percentage of Day = 7.85%, Performance = 67.72%
Work Category Totals: DIRECT = 456.34 Percentage of Day = 80.87%, Performance = 110.99%
SELECT = 456.34 Percentage of Day = 80.87%, Performance = 110.99%
==================================================================================================== ============================
10/17/2008 16:51:52 Report Group: 1 - F/C TM 1 User:
Associate 2222222

Work Category Totals: INDIRECT = 82.68 Percentage of Day = 15.52%, Performance = 72.57%
BEGINDAY = 13.75 Percentage of Day = 2.58%, Performance = 0.00%
LUNCH = 38.93 Percentage of Day = 7.31%, Performance = 77.05%
Work Category Totals: DIRECT = 449.95 Percentage of Day = 84.48%, Performance = 97.88%
SELECT = 449.95 Percentage of Day = 84.48%, Performance = 97.88%
==================================================================================================== ============================
10/17/2008 16:51:52 Report Group: 3 - F/C TM 3 User:
Associate 3333333
Work Category Totals: INDIRECT = 100.35 Percentage of Day = 18.13%, Performance = 59.79%
BEGINDAY = 16.77 Percentage of Day = 3.03%, Performance = 0.00%
BREAK = 15.00 Percentage of Day = 5.63%, Performance = 100.00%
BATTERY = 8.50 Percentage of Day = 1.54%, Performance = 0.00%
Work Category Totals: DIRECT = 453.05 Percentage of Day = 81.87%, Performance = 103.49%
SELECT = 453.05 Percentage of Day = 81.87%, Performance = 103.49%
==================================================================================================== ============================

How I would like it to look
Report Group: 1 - F/C TM 1 Associate 1111111 BEGINDAY = 10.12 BREAK = 30.00 LUNCH = 44.30
Report Group: 1 - F/C TM 1 Associate 2222222 BEGINDAY = 13.75 BREAK = 00.00 LUNCH = 38.93
Report Group: 3 - F/C TM 3 Associate 3333333 BEGINDAY = 16.77 BREAK = 15.00 LUNCH = 00.00

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read each file grep a value from that?

Hi Team, in /tmp folder i have thousands of log files i want to read each file and grep a value called "Calling This". Each logfile name is different but it ends with .log. How can i achieve this? Please excuse if i did any mistake by not following forum standards. I will surely follow... (10 Replies)
Discussion started by: darling
10 Replies

2. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

3. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

4. Shell Programming and Scripting

How to grep and count the occurences in while read script?

Hi, I have a file while is the output of lspath command and output of file is #cat lspath.txt Enabled hdisk0 vscsi0 Enabled hdisk0 vscsi1 Enabled hdisk1 vscsi0 Enabled hdisk2 vscsi0 Enabled hdisk2 vscsi1 Missing hdisk3 vscsi0 Enabled hdisk3 vscsi1 Have created script to check state... (7 Replies)
Discussion started by: ksgnathan
7 Replies

5. Shell Programming and Scripting

read from file and grep using shell

Hi Guys, I have a small script which greps for the username reading from stdinput. ./file.sh pattern pattern=$1 grep "blah blah.*$pattern" /home/user/log.txt Instead of typing the pattern everytime i want to read the pattern from a file inside the shell script and execute the... (5 Replies)
Discussion started by: Irishboy24
5 Replies

6. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

7. Shell Programming and Scripting

grep within while read loop

I have a key file $ cat klist 5 N:8855 CASA VERDE ROAD :32827 :ORLAND 5 N:585 MOLLY LANE :30189 :WOODST 5 N:320 NINA ROAD :32304 :TALLAH and a data file, see example of the line below: N:RT 15 & N 7TH STREET :17837 :U SAVE I need to search by key (2nd field) from klist... (6 Replies)
Discussion started by: migurus
6 Replies

8. Shell Programming and Scripting

grep vs do while read

I have inherited the UNIX scripting duties for our team and have only been at this for a few months so I apologize for what may be a simple question. We have a script we are reworking. Part of this script takes a parameter and reads a csv file. When it finds a matching line, the cut command is... (4 Replies)
Discussion started by: 15a0
4 Replies

9. Shell Programming and Scripting

read in variable data from another file - grep

Hello! I think this should be an easy solution. I have a large file with many fields of data. The first field has a unique identifier (a subject number) for every record for a chunk of data. Something like this: There were ten experimental conditions (ec), but the ec is identified by only... (11 Replies)
Discussion started by: ccox85
11 Replies

10. Shell Programming and Scripting

Read file then grep the line

Dear all, I am reading a file that has 1 column. While reading I must find the line references from the another file. The following shell doesn't works. Please help #!/bin/bash while read filename; do grep ${filename} fs_full.dat >> unprocfull.dat; done < unproc.dat But when... (2 Replies)
Discussion started by: mr_bold
2 Replies
Login or Register to Ask a Question