Replace cat and grep with <


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace cat and grep with <
# 1  
Old 07-22-2014
Linux Replace cat and grep with <

Hello
someone told me to use
OS=`awk '{print int($3)}' < /etc/redhat-release`

instead of
OS=cat /etc/redhat-release | `awk '{print int($3)}'`

any idea for the reason ?
# 2  
Old 07-22-2014
Both versions will achieve the same result. The first is more efficient and should be the one of choice, as awk will open and read the input file directly. The second will make cat print the input file to its stdout which then is piped to and read by awk (stdin), spoiling a process creation (cat).
# 3  
Old 07-22-2014
Quote:
Originally Posted by nimafire
Hello
someone told me to use
OS=`awk '{print int($3)}' < /etc/redhat-release`

instead of
OS=cat /etc/redhat-release | `awk '{print int($3)}'`

any idea for the reason ?
Sorry RudiC, but I have to disagree completely. (The first backquote in the 2nd example is misplaced.)

The first sequence sets OS to a list of the 3rd fields on every line of the file /etc/redhat-release.

The second sequence sets OS to cat and feeds the output produced by running the command /etc/redhat-release through the command found in the 3rd field typed into the script that is running this command sequence. And, when the pipeline finishes, the original state of OS is restored (unset if it hadn't been set, or set to the value it had before the above command if it had been set). Probably not what you wanted.

You could get the same results as the 1st command sequence much less efficiently using the command sequence:
Code:
OS=`cat /etc/redhat-release | awk '{print int($3)}'`

The 1st command sequence reads the data in /etc/redhat-release once, this reads the data in that file twice and writes it once. The 1st command sequence has to fork() and exec one process (awk); this has to fork() and exec two processes (cat and awk).
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 07-22-2014
Thanks, Don Cragun. I'm utterly afraid I need my eyeglasses reground and polished...
# 5  
Old 07-22-2014
Quote:
Originally Posted by RudiC
Thanks, Don Cragun. I'm utterly afraid I need my eyeglasses reground and polished...
I almost missed it, too. I know I need to have my eyeglass prescription updated, but I think the problem here is that I initially saw what we all expected to see instead of what was there.
# 6  
Old 07-22-2014
Hi.

A good reason to use $( pipeline ) as opposed to ` pipeline ` whenever available ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Highlighting specific column in cat/grep

I have a large * delimited text file and need to highlight specific columns on specific lines. The file looks similar to this: ABC*01*00*01*00000 000000 *00000 000000 *35*0*0*0*0*20000*0*0*1*0000*00*4*0*3*0*0*1*0* *35*0000*001*4*1OT2*0148*0*0*0*0*0*0*6A7801B0**TEST1... (1 Reply)
Discussion started by: say170
1 Replies

2. UNIX for Dummies Questions & Answers

Grep or cat The Whole Directory PROBLEMS :(

Hi Guys This is my first post so I am not sure how things go here. I'm sorry if I'm breaking the rule or something. Feel free to correct me about that :) So as I was saying... I'd been trying to grep this folder containing 900,000 txt files but seems no luck. I get either "No such file... (6 Replies)
Discussion started by: Nexeu
6 Replies

3. Shell Programming and Scripting

Incredibly inefficient cat | grep script

Hi there, I have 2 files that I am trying to work on. File 1 contains a reference list of unique subscriber numbers ( 7 million entries in total) File 2 contains a list of the subscriber numbers and their tariff (15 million entries in total). This file is in the production system and... (12 Replies)
Discussion started by: Cludgie
12 Replies

4. UNIX for Dummies Questions & Answers

Grep and cat combined

Hello, i need to search one word (snp1) from many files and copy the content of the columns of this word in new file. example: file 1: SNP BP CHR P snp1 1 3 0.01 snp2 2 2 0.05 . . file 2: SNP BP CHR P snp1 1 3 0.06 snp2 2 2 0.3 output... (6 Replies)
Discussion started by: biopsy
6 Replies

5. Shell Programming and Scripting

replace cat huge_multiplefiles |sort |uniq -c

Does any one know any command runs faster than this to give duplicate numbers cat file1 file2 ....file 100 | sort |uniq -c thanx (3 Replies)
Discussion started by: quincyjones
3 Replies

6. Shell Programming and Scripting

grep or cat using sed

Is there a way using grep or cat a file to create a new file based on whether the first 9 positions of each record is less than 399999999? This is a fixed file format. (3 Replies)
Discussion started by: ski
3 Replies

7. Shell Programming and Scripting

cat -n and grep

I am not sure if using cat -n is the most efficient way to split a file into multiple files, one file per line in the source file. I thought using cat -n would make it easy to process the file because it produces an output that numbers each line that I could then grep for with the regex "^ *$i".... (3 Replies)
Discussion started by: kapu
3 Replies

8. Shell Programming and Scripting

cat /etc/passwd and grep -v on /etc/shells

Hi All, I'd like to do this cat /etc/passwd and grep -v on the /etc/shells list I'd like to find all shell that doesn't exist on the /etc/passwd. Is there an easy way without doing a egrep -v "/bin/sh|/bin/bash................"? How do I use a file /etc/shells as my list for... (4 Replies)
Discussion started by: itik
4 Replies

9. UNIX for Advanced & Expert Users

cat and grep not working

I am trying to cat a file and then grep that file for a number. I can do it fine on other files but this particular file will not do anything. I tried running it on an older file from the same device but it is just not working. The file is nothing more than a flat file on a unix box. Here is just a... (3 Replies)
Discussion started by: jphess
3 Replies

10. Shell Programming and Scripting

basic cat replace string

trying to exclude hostnames ending in "s" from a host file: # cat hosts ssef ssefd ssefsfff ssefsfs # for x in `cat hosts`; do echo "${x/*s}" ;done ef efd fff # How can I echo/or not echo only 'ssefsfs' ?? thanks (4 Replies)
Discussion started by: prkfriryce
4 Replies
Login or Register to Ask a Question