Grep line with all string in the lines and not space.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep line with all string in the lines and not space.
# 1  
Old 08-09-2012
Grep line with all string in the lines and not space.

I want to write the syntax so does not count line with no space.

So currerntly it is showing lines as 5, but i want to show 4.

Code:
# cat /tmp/mediacheck | sort -u  | grep -vi " " | awk '{print $1}' | wc -l

BA7552
BAA002
BAA003
BAA004

# 2  
Old 08-09-2012
Does the line you do not want have blanks (ascii 32) in it?

Please post the output of
Code:
od -c /tmp/mediacheck

od makes a big block of data on the screen, we need to see your problem line and a good line. Probably not the entire file.
# 3  
Old 08-09-2012
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:
Code:
awk '/./ {cnt++} END {print cnt}' /tmp/mediacheck

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.
# 4  
Old 08-09-2012
The output you have posted makes no sense for the line you have pasted. wc -l would print a number.

It's possible that a tab or something has convinced awk that the first field is blank, rather than a space...

If you're using awk, there's no point using grep too. awk can check if something's blank by itself and thereby shrink your pipe chain.

Also see Useless Use of Cat.

Code:
sort -u < /tmp/mediacheck | awk '$1 { print $1 }'

# 5  
Old 08-10-2012
The following worked for me to count the "/tmp/mediacheck" file.

Code:
awk '/./ {cnt++} END {print cnt}' /tmp/mediacheck


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.
# 6  
Old 08-10-2012
Have you tried my code? Does it ignore your space?
# 7  
Old 08-10-2012
Quote:
Originally Posted by Junes
The following worked for me to count the "/tmp/mediacheck" file.

Code:
awk '/./ {cnt++} END {print cnt}' /tmp/mediacheck


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:
Code:
awk '/./ {cnt++} END {print cnt}' /tmp/mediacheck

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:
Code:
grep -vi " "

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:
Code:
cat /tmp/mediacheck | sort -u  | grep -v "^$" | awk '{print $1}' | wc -l

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:
Code:
BA7552
BAA002
BAA003
BAA004

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:
Code:
wc -l /tmp/mediacheck

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:
Code:
grep -v "^$" /tmp/mediacheck | wc -l

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:
Code:
awk '/^$/ {next}
        {cnt++}
END {print cut}

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:
Code:
awk '/./ {cnt++}
END {print cut}

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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command in Linux in a script where the search string has a space

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)
Discussion started by: knijjar
4 Replies

2. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

3. Shell Programming and Scripting

Grep a string and count following lines starting with another string

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)
Discussion started by: Syeda Sumayya
3 Replies

4. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

5. Shell Programming and Scripting

grep for a string until instance of a space

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)
Discussion started by: terrell
6 Replies

6. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

7. UNIX for Dummies Questions & Answers

Matching exact string with blank space using grep

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)
Discussion started by: jazzaddict
4 Replies

8. Shell Programming and Scripting

Print lines between two lines after grep for a text string

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)
Discussion started by: jbruce
7 Replies

9. AIX

grep not working when search string has a space in it

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)
Discussion started by: sekhar gajjala
3 Replies

10. Shell Programming and Scripting

Break lines up into single lines after each space in every line

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)
Discussion started by: lewk
7 Replies
Login or Register to Ask a Question