AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Sed & Grep - UNIX COMMAND

Go down

Sed & Grep - UNIX COMMAND

Post by Lucaci Andrei on Thu Mar 15, 2012 12:49 am

Code:
Grep command

$ grep [cvnqsri] regularexpression file ...

Read the specified files, or if not exist read standard
input, and print to standard output the lines to matck
regularexpression.

The options mean:
-c count the number of lines that is match to regular expression;
-v are considered only lines that NOT check the regular expression;
-n every line of output is preceded by its number of line
in the current file;
-q,-s do not show anything but a return code is setting
(0 if it found at least one line to verify the regular expression,
1 if not found lines, 2 if an error occurred);
-r recursive processing of the directories;
-i ignore case (a == A etc.).

For example, we use a grep command to display (with screen pages),
the list of files and direcrories from the current directory and
its subdirectories, who as owner nume_user:

$ ls-lFR | grep nume_user | more


sed Editor

(Stream Editor) a noninteractive text editor. It read a file (or
in the absence takes the standard input), applied on his
editing commands, then the result is printed on standard output.

$ sed [-n] [-e script] [-f filescript] file ...

-n option indicates that output is not standard output file.
Instead, each individual file is saved with the same name as it
input file.

-e or -f options (mutually exclusive):
"script" contains the sequence of editing commands, desired
is applied to the entrance,
and "filescript" is the name of a file containing such.
The sequence of commands that are intended to be applied to
input files.

The sequence of editing commands appears as rows the following type:

Position EditingCommand

Position indicated a line or an group of lines which is
apply editing command. Position is a regular expression:
- A line number;
- A pair of numbers indicating the first and last line of the group;
- One or two regular expressions in the form /.../ or ?...?;
- Current line (the . character);
- The last line (the $ character);
- Etc.

EditingCommand can be:
- p displays the text frpm Position to the standard output;
- d delete all lines from Position;
- i\ <ENTER> to insert lines before Position.

For example, if file-s file contains three lines:
5 i\
ttttt\
11111

then the following command:
$ sed -f file-s A

insert in the file A, before the 5th line,  two
lines: one with the letters t and the second with 1.
- a\ <ENTER> works similar to \i but insertion
made after the Position. Returning to the example above,
the two lines will be inserted after the 5th line.
- s/regularexpression/string/[flags]
replaces appearance of text that match the regular expression with string.
In the absence of flags to replace only the first occurrence.
If flags is a number n, then replace only the n'th appearance,
if flags is the character 'g' then replace all occurrences.

In the following, two examples will presented:

a) To remove all frames: <digit> <letter> <digit>
from the file fis1. The command is:

$ sed "s/[0-9][a-zA-Z][0-9]//g" fis1

b) To replace the last word in each line with the first word.
Words containing only lowcase letters and digits and are
separated by spaces. The command is:

$ sed "s/\(^[a-z,0-9]*\)\(.*\)\([a-z,0-9]*$\)/\1\2\1/g" fis1

(To note that \ 1, \ 2, ..., \ 9 when they occur in "string"
means first, second, ..., that the 9th regular subexpression
from regular expression)



OBSERVATION ABOUT TEXT FILES IN UNIX AND WINDOWS:
This observation does not necessary yourself sed or grep commands, but
we belive is useful to do here, because it is one of the first Unix laboratories. observatie nu tine neaparat de programarea shell, dar credem ca

Unix text files:
Separation two consecutive lines with a character '\n' (New Line)

Windows text files:
Separation two consecutive lines through two succesive characters '\r\n' (Carriage Return, New Line,)

FTP transfer such a file between two systems is possible that destination
file to be an incorrect text file!
Transfer, for example, a file from Windows to Linux, then open it by joe,
and at the end of each line will show a ^M (representation of \r).
Transfer from Unix to Windows, open it by Notepad, and a single line will
appear instead more lines!
To convince you, see files in question with Unix command:

odd -abc file

and you will see the content of file, including separator characters.

Unix solution:
1) tr -d "\r" <fileWindows >fisierUnix # Remove \r
2) sed -e 's/$/\r/' <fileUnix >fileWindows

Windows solution: Edit and save fileUnix with Edit text editor.

Obviously it will quite easy, but interesting experience, that each
student to write small C (C++) programs for such conversions.

Homework:
(a) SED
    Delete the second and fourth words on each line of a given file.

(b) GREP
    Display all the users running the command "bash"


Last edited by Lucaci Andrei on Thu Mar 15, 2012 1:14 am; edited 1 time in total
avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 25
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Re: Sed & Grep - UNIX COMMAND

Post by Lucaci Andrei on Thu Mar 15, 2012 1:00 am

You have to put the source code inside a file: eg. $ vim o
My source file:

one two three four five 1 <2> 3 <4> 5
six seven eight nine ten 6 <7> 8 <9> 10
eleven twelve thirteen fourteen fifteen 11 <12> 13 <14> 15
sixteen seventeen eighteen nineteen twenty 16 <17> 18 <19> 20


My command:
sed 's/\([[:blank:]]\)\([[:alnum:]]*\)\(\)\([[:space:]]\)\([[:alnum:]]*\)\([[:space:]]\)\([[:alnum:]]*\)/\4\5/' o>lbs


At the end, instead the o> lbs you can add: current_file>new_file (where current_file is your source file, and new file is the file which contains the modifications.)

Grep:
ps aux | grep ' bash$'

grep
avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 25
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum