scmuser created the topic: grep and sed questions with shell script
Search for a pattern with grep and sed and look for a file with find. Test the return value of each command when it displays no output. What conclusions would you draw?
Tag: Grep
Remove blank line using Grep and sed
scmuser created the topic: Remove blank line using Grep and sed
Hi,
How do you remove blank line using grep and sed in shell scripting?
rajeshkumar replied the topic: Re: Remove blank line using Grep and sed
grep -v "^$" filename > newfilename
The ^$ within the quotes is a regular expression: ^=beginning of line, $=end of line, with no characters between.
To store output to another file use redirection operator:
$ sed ‘/^$/d’ /tmp/data.txt > /tmp/output.txt
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
rajeshkumar replied the topic: Re: Remove blank line using Grep and sed
The above script will work only if you have no character in the blank line but most of the time it can happen that Tab can be possle in blank line….
so for this situation u should try following..
sed '/^[PRESS TAB]*$/d' filename
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Unix Command: Grep – Quick Reference – Pattern – Examples – Options
Grep scans its input for a pattern, and can display the selected pattern, the line numbers of the filenames where the pattern occurs, The command uses the following syntax
grep options pattern filesname(s)
grep searches for pattern in one or more filenames.
Example for Grep command:
- grep “sales” emp.lst
- grep “director” emp1.lst emp2.lst
- grep ‘jai sharma’ emp.lst
- grep “jai Sharma $var” emp.lst
—————-Grep options—————————
Ignoring Case (i) When you look for a name, but you are not sure of the case, grep offers the –i (ignore) option which ignores case for patteen matching.
> Grep –i ‘agarwal’ emp.lst
Deleting Lines or Inverse(-v): -v (inverse) option selects all except lines containing the pattern. Thus, you can create a file other list containing all but director.
> Grep –v “director” emp.lst > other list
Displaying line Numbers (-n): The –n(number) options displays the line numbers containing the pattern, along with the lines:
> grep –n ‘marketing’ emp.lst
Counting Line Containing patterns (-c): The –c (count) option counts the number of lines containing the pattern ( which is not the same as number of occurrences).
- grep –c director emp.lst
- grep –c director emp*.lst
Displaying Filenames (-l): The –l (list) option displays only the files names of files containing the pattern.
> grep –l ‘manager’ *.lst
Matching Multiple Patterns(-e): With the –e option, you can match the three agarwals by using grep like this:
> grep –e “Agarwal” –e “aggarwal” –e “agarwal” emp.lst
Taking patterns from a file (-f): we can place all patterns in a separate file, one pattern per lin. Grep takes inputs from there with the –f option:
> grep –f pattern.lst emp.lst