Tuesday, August 23, 2016

Unix commands


Best site for Unix:-
http://www.grymoire.com/Unix/Sed.html

1) SED:
Sed Command :-
1) s for substitution:-
sed s/day/night/ new
echo day | sed s/day/night/
o/p=night

single quote
echo Sunday | sed 's/day/night/'

Another important concept is that sed is line oriented. Suppose you have the input file:

one two three, one two three
four three two one
one hundred

and you used the command

sed 's/one/ONE/'
The output would be

ONE two three, one two three
four three two ONE
ONE hundred


Note that this changed "one" to "ONE" once on each line. The first line had "one" twice, but only the first occurrence was changed. 
That is the default behavior. If you want something different, you will have to use some of the options that are available. I'll explain them later.
s  Substitute command
/../../  Delimiter
one  Regular Expression Pattern Search Pattern
ONE  Replacement string


2)The slash as a delimiter:-

The character after the s is the delimiter. It is conventionally a slash, because this is what ed, more, and vi use. It can be anything you want, however.
If you want to change a pathname that contains a slash - say /usr/local/bin to /common/bin - you could use the backslash to quote the slash:


sed 's/\/usr\/local\/bin/\/common\/bin/' new

Gulp. Some call this a 'Picket Fence' and it's ugly. It is easier to read if you use an underline instead of a slash as a delimiter:

sed 's_/usr/local/bin_/common/bin_' new
Some people use colons:

sed 's:/usr/local/bin:/common/bin:' new
Others use the "|" character.

sed 's|/usr/local/bin|/common/bin|' new

3)Using & as the matched string:-

Sometimes you want to search for a pattern and add some characters, like parenthesis, around or near the pattern you found. It is easy to do this if you are looking
for a particular string:

sed 's/abc/(abc)/' new

This won't work if you don't know exactly what you will find. How can you put the string you found in the replacement string if you don't know what it is?

The solution requires the special character "&." It corresponds to the pattern found.

sed 's/[a-z]*/(&)/' new

4:-Using \1 to keep part of the pattern:-

The "\1" is the first remembered pattern, and the "\2" is the second remembered pattern. Sed has up to nine remembered patterns.

If you wanted to keep the first word of a line, and delete the rest of the line, mark the important part with the parenthesis:

sed 's/\([a-z]*\).*/\1/'

echo abcd123 | sed 's/\([a-z]*\).*/\1/'

This will output "abcd" and delete the numbers.

If you want to switch two words around, you can remember two patterns and change the order around:

sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/'
If you want to detect duplicated words, you can use

sed -n '/\([a-z][a-z]*\) \1/p'

5:-/g - Global replacement:-

Most UNIX utilities work on files, reading a line at a time. Sed, by default, is the same way. If you tell it to change a word, it will only change the first
occurrence of the word on a line. You may want to make the change on every word on the line instead of the first. For an example, 
let's place parentheses around words on a line. Instead of using a pattern like "[A-Za-z]*" which won't match words like "won't," 
we will use a pattern, "[^ ]*," that matches everything except a space. Well, this will also match anything because "*" means zero or more.
The current version of Solaris's sed (as I wrote this) can get unhappy with patterns like this, and generate errors like "Output line too long" or even run forever. 
I consider this a bug, and have reported this to Sun. As a work-around, you must avoid matching the null string when using the "g" flag to sed.
A work-around example is: "[^ ][^ ]*." The following will put parenthesis around the first word:

sed 's/[^ ]*/(&)/' new
If you want it to make changes for every word, add a "g" after the last delimiter and use the work-around:

sed 's/[^ ][^ ]*/(&)/g' new


6:-/p - print:-

By default, sed prints every line. If it makes a substitution, the new text is printed instead of the old one. If you use an optional argument to sed,
"sed -n," it will not, by default, print any new lines. I'll cover this and other options later. When the "-n" option is used, the "p" flag will cause 
the modified line to be printed. Here is one way to duplicate the function of grep with sed:

sed -n 's/pattern/&/p'



No comments:

Post a Comment

 BEST PYSPARK LEARNING SITES https://www.youtube.com/watch?v=s3B8HXLlLTM&list=PL2IsFZBGM_IHCl9zhRVC1EXTomkEp_1zm&index=5 https://www...