Is the text below the commented command the contents of /tmp/mediacheck? I don't see any spaces on any line. There are no duplicate input lines in that file. Why do you need to sort it? If you just want a count of lines that aren't empty. This is much simpler than your current code:
If there are more fields in your input and you really meant that you only want to count lines that contain at least one <space> character, please give us a real example of what is in /tmp/mediacheck.
The following worked for me to count the "/tmp/mediacheck" file.
But i was trying to achieve is when i enter media string into a file "/tmp/mediacheck", it leaves a space at the end of the file. I want to have the script ignore the character space at the end of the file.
How can i have the script cat "/tmp/mediacheck" and ignore the EOF character space.
The following worked for me to count the "/tmp/mediacheck" file.
But i was trying to achieve is when i enter media string into a file "/tmp/mediacheck", it leaves a space at the end of the file. I want to have the script ignore the character space at the end of the file.
How can i have the script cat "/tmp/mediacheck" and ignore the EOF character space.
Your terminology does not match the language used when writing documentation describing the behavior of UNIX and Linux systems. Therefore, we do not understand your question.
The cat utility never ignores any character. It has options (which vary from system to system) that allow it to transform certain characters, to add an indicator that visually displays non-printing characters, to add line numbers, or to squeeze sequences of multiple adjacent blank lines to a single empty line; but it never "ignore"s characters.
The EOF character and the space character are not the same. In ASCII and in Unicode's UTF-8 encoding, there is no EOF character (it is a condition encountered after reading the last byte from an input file, and <space> is an 8-bit character having the decimal value 32.
You said that the command that I supplied earlier:
is doing what you wanted. The UNIX terminology for what this command does is:
Quote:
Print a count of the number of non-empty lines in the file /tmp/mediacheck."
It uses the awk utility. It does not need the cat, sort, grep, or nl utility to get the job done; and the output it produces is nothing like the output you showed when you asked for help.
The title of this thread is "Grep line with all string in the lines and not space."
By definition a line is a string of one or more characters terminated by a <newline> character. By definition an empty line is a line whose only character is the <newline> character.
The command:
in the pipeline in your question copies standard input to standard output deleting input that contains an uppercase or lowercase <space> character.
(And since <space> is not an alphabetic character, the -i option has no effect in this command.) Since you said you were getting 5 when you wanted 4, and you showed 5 lines after the pipeline, I assumed that you were trying to ignore empty lines and print a count of the other lines. If this was your goal, another way to do it that looks more like your original pipeline would have been:
The cat command is not needed. It creates an additional process and reads the input and copies it to standard out. This could have been done by making /tmp/mediacheck an operand to sort, grep, or awk or by using the shell to redirect the input of any of these commands.
The sort command transformed your input file to:
removing all duplicated lines from the output. But since it seems that there were no duplicate lines in your input file and the order of the lines in the file doesn't make any difference to anything in the rest of your pipeline, calling sort creates another process, causes the input to be read again, sorted, and written again but in no way affects the final output with the input shown above.
The grep command read the input again and wrote every line that did not contain a <space> character to its standard output. Since you show 5 input lines and you reported by wc -l displayed 5, we can assume that none of the lines in your input file contained a <space> character. Therefore, calling grep created another process, read the file contents again, and wrote the file contents again without affecting the results of the pipeline.
The awk command in your pipeline printed an output line for each input line that it read from its standard input. If there had been more than one input field on one or more of the input lines, it would have thrown away all fields following the first field. But, since none of your input lines had more than one field, all this command did was create another process, read the data again, and write the data again without affecting the result of invoking this pipeline.
So with the input you had, the pipeline you provided could have been replaced by the command:
and produced the same results you got.
Using the UNIX philosophy of providing filters in a pipeline to perform the steps needed to complete a task, you could have gotten the results you wanted with the command:
which starts two processes, reads (most) of the data twice, writes most of the data once, provides a count of the non-empty lines. Your original pipeline started five processes, read all of the data five times, wrote all of the data four times. A straight translation of this pipeline into a awk script would be:
which only starts one process, reads the data once, and never write anything but the desired count of non-empty lines. It does this by throwing away empty lines and counting the remaining lines. Using:
ignores lines that are empty and counts lines that are not empty using one less line of awk programming with the same results.
I hope this helps you understand why the solutions we provided didn't use cat or grep and why we tried to get you to explain where the <space> characters were that you were talking about (when you never showed us any <space> characters in your input).
Last edited by Don Cragun; 08-10-2012 at 10:34 AM..
I have a file xyz with the following content
PPPL 0123
PPPL 0006
POFT 0923
POFT 1111
WENT 2323
SEND 2345
I also have another file named MasterFile where it contains the above mentioned data million times with different digits at the end for example some times it contains SEND 9999 or WENT... (4 Replies)
I have a large dataset with following structure;
C 0001 Carbon
D SAR001 methane
D SAR002 ethane
D SAR003 propane
D SAR004 butane
D SAR005 pentane
C 0002 Hydrogen
C 0003 Nitrogen
C 0004 Oxygen
D SAR011 ozone
D SAR012 super oxide
C 0005 Sulphur
D SAR013... (3 Replies)
Hey guys,
I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this.
I'm looking to grep for the string ERROR from the following log up until any... (6 Replies)
hi!
i'm trying to get grep to do an exact match for the following pattern but..it's not quite working. I'm not too sure where did I get it wrong. any input is appreciated.
echo "$VAR" | grep -q '^test:]name'
if ; then
printf "test name is not found \n"
fi
on... (4 Replies)
I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like:
<ROW>
some information
more information
</ROW>
I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Hi,
I am trying to grep a string which has two words separated by space.
I used a script to grep the string by reading the string in to a variable
command i used in the script is
echo "enter your string"
read str
grep $str <file>
it is working fine when the entered string is a single... (3 Replies)
It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space.
Example of text file;
line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)