What is the difference between cat source.txt | grep x and grep x source.txt? [duplicate] - linux

This question already has answers here:
difference between grep Vs cat and grep
(5 answers)
Closed 8 years ago.
I saw an example where some one did this:
cat source.txt | grep a
But I always do it like:
grep a source.txt
What's the difference between the two?

The first one is a classical “useless use of cat” (UUOC).

Related

Execute agrep within script | grep terminal output [duplicate]

This question already has answers here:
Assign output to variable in Bash [duplicate]
(2 answers)
Closed 2 years ago.
Can I execute a grep in my script below?
echo 5
echo 4
result='çat output.txt | grep flag'
echo $result
The scipt gets used like
./script | ./program > output.txt
The script is used as input for the program, and the output of the program gets put into output, which I want to be able to grep for instantly. At the moment, it seems to finish without doing the grep command
I have the impression you are using single quotes while it should be backtics. Luckily there is something easier to use
result=$(cat output.txt | grep "flag")
The $(some_command) is used for getting the results of some_command.

what is "|" in Ubuntu or Linux in general [duplicate]

This question already has answers here:
What does "|" mean in a terminal command line? [closed]
(3 answers)
Closed 4 years ago.
example :
diskFreeSpace="$(df | grep -v -E 'Filesystem|udev|tmpfs|Home|/dev/sr1|/dev/sr0' | awk '{ print $5-1+1}')"
and hope if you understand what is this command u help me
the symbol is called a "pipe".
The command df | grep is "piping" (redirecting) the standard output of the df command into the standard input of the grep command. In this example the grep command filters the output of the df command. Then the awk commands is for display/format purposes.
Inside the regular expression it is specific to the grep syntax since it is surrounded by quotes it is only considered as an argument passed to grep

Shell AWK changes my parameter [duplicate]

This question already has answers here:
awk print overwrite strings
(1 answer)
How do I use shell variables in an awk script?
(7 answers)
Closed 4 years ago.
When I run below command, the "awk" changes my ip unexpectedly. I could not find how to fix this. Regards.
echo $ip
10.165.6.90
cat text.txt | grep STRING | awk -F'[. ="]' '{print '$ip' ";" $10 ";" $15}'
10.1650.60.9;163840000;X
10.1650.60.9;232525824;Y
10.1650.60.9;232526848;X
10.1650.60.9;232529920;Z
10.1650.60.9;232530944;T

lower case to uppercase conversion [duplicate]

This question already has answers here:
How to convert a string to lower case in Bash
(29 answers)
Closed 9 years ago.
I found solution but it doesn't working http://www.cyberciti.biz/faq/linux-unix-shell-programming-converting-lowercase-uppercase/
[root#mg0016 tmp]# y="this Is A test"
[root#mg0016 tmp]# echo "${y^^}"
-bash: ${y^^}: bad substitution
You can use any one of the following code :
$ tr '[:lower:]' '[:upper:]' < input.txt > output.txt
or
$ sed -e 's/\(.*\)/\U\1/' input.txt > output.txt
I found following one! and it works!
[spatel#mg0016 ~]$ echo "lower" | awk '{print toupper($0)}'
LOWER
Thanks for reply all.

How can I print intermediate results from a pipeline to the screen? [duplicate]

This question already has answers here:
How can I gzip standard in to a file and also print standard in to standard out?
(4 answers)
Closed 7 years ago.
I'm trying to count the lines from a command and I'd also like to see the lines as they go by. My initial thought was to use the tee command:
complicated_command | tee - | wc -l
But that simply doubles the line count using GNU tee or copies output to a file named - on Solaris.
complicated_command | tee /dev/tty | wc -l
But keep in mind that if you put it in a script and redirect the output, it won't do what you expect.
The solution is to tee to the console directly as opposed to STDOUT:
tty=`tty`
complicated_command | tee $tty | wc -l

Resources