I need your help. I have a file with around 10,000 lines in which i need to get the machine and owner name and paste in the seperate file. I have multiple entries fr machine and owner i need to fetch the data accordingly.
I am using bash, don't know about you. When I do a small test, it is working fine:
> vi test.txt (I copied your text in)
> file=test.txt
> cat $file | egrep "^machine|^Owner"
machine: V_lty-sap-fhgfhfgh
Owner: Vinoth
machine: V_lty-sap-crm-prbogfhgfhg
Owner: Ram
I can see the information you are trying to query is from AutoSys / W.C.C. If so, then please be aware that the file created with autorep can contain spaces at the beginning of lines (due to boxes in a box). If the whole file is left aligned (as in your example) it should work. Create a smaller version of your file for testing
You could take a look in the manpage of grep and egrep. I think egrep is not supported under ksh and for grep you can try to find an option that suits your needs
> man grep
Please be aware that also the OS could be a delimiting factor. I am working under bash on Linux (RHEL) and it might differ from your environment.
I think the trouble is he's literally using cat $file without setting file. but there is no reason to cat and pipe a file. it's a useless use of cat. grep works on files.
for those calling things useless...... In my opinion using `cat` as an example is easy to understand, so useful. There are many one-liners (without useless things) which take hours to understand what is really happening. My scripts are easy to read, how 'bout yours??????
causes the data in file to be read by cat , written by cat , and read again by grep , and
runs slower
than using:
grep expression file
or
grep expression < file
both of which:
use only one process,
only reads the data in file once, and
run faster.
That is why we call the first form a useless (and inefficient) use of cat .
With a little more experience, you will find either of the latter forms of these command lines just as easy to read as the first form.
If you call it inefficient, I fully agree. As mentioned before, useless is a different thing. With your experience you should have noticed that the request for information was from a noob. By keeping it simple (but indeed not efficient) it is easier to understand. It would have been better if I had given the efficient solution as well. But in that case also more efficient solutions for data handling (like Perl) should be mentioned (without saying that shell scripting is useless for data handling)
and for your information, I never said this was hard for me to read