UNIX Scripting

Hi Guys,

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.

File example:

command: /data/esb/etl/bods_exported_script
machine: V_lty-sap-fhgfhfgh
Owner: Vinoth
std_out_file: "$$GLB_LOGDIRR11U
alarm_if_fail: 1

command: /data/esb/etl/bods_exported_script
machine: V_lty-sap-crm-prbogfhgfhg
Owner: Ram
std_out_file: "$$GLB_LOGDIRR11U
alarm_if_fail: 1

In the above file i need only machine name and its corresponding owner to be copied in seperate file.

Your help is much appreciated

Hi Vinoth,

you can try the next command:

cat $file | grep -A 1 machine

it will take every line containing "machine" and the next line

also

cat $file  |  egrep "^machine|^Owner"

should do the trick

cheers
Marco

Thanks for your quick reply Marco!!

But it doesnt works. I tried both the comment. It simply got hung:(

Hi Vinoth,

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

Hi Macro,

am using ksh. Is there any other command??

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.

Hey Macro its working now but its fetching only machine value not the owner. I removed the left spaces.

Output

cat script.txt | egrep "^machine|^Owner"
machine: V_jdgfjhdsfjgjkfdskmhj

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.

grep -e ^Owner -e ^machine file

make the grep case-insensitive by changing the command to

cat script.txt | egrep -i "^machine|^Owner

Thanks a lot Macro!!!!It works:)

You can give awk a try:

awk '/^machine/||/^Owner/' file

BTW, this is useless use of cat

cat $file | egrep -i "^machine|^Owner"

This should be sufficient:

egrep -i "^machine|^Owner" $file

Hi Vinoth, great.

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??????

Useless use of cat is a waste of time, and it costs you an extra process.

Using:

cat file | grep expression
  • uses two processes,
  • 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:

  1. use only one process,
  2. only reads the data in file once, and
  3. 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.

2 Likes

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 :slight_smile:

If you didn't know, "useless use of cat" is a fairly well-known phrase in Unix scripting.

Incidentally, in this case I don't think using a pipeline is simpler or easier to understand than just supplying the filename as a parameter...

You may try:

sed -n '/^machine/p;/^Owner/p' infile

thanks

(afaik)

egrep -A1  "^machine" script.txt > outputfile

should be the same as:

grep -e -A1  "^machine" script.txt > outputfile

You missed the -A1 , as well as there is no need for cat :wink:

Hope this helps

Almost...

egrep ...

is the same as:

grep -E

on systems that both have egrep (which is now obsolescent) and have a -E option in grep (which some very old versions do not have).

awk -F" " '/machine|Owner/ { print}' file