Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet88/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   84   85   86   87   88   89   90   91   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

file1 file2 file3 file4 

where the files are all files found in 
the current directory. Then, the wc command is applied to the list of files.
Now consider the instruction 
egrep abc* foo.txt
. The 
*
seems to indicate a 
regular expression wildcard. You might interpret this instruction as saying “search the file 
foo.txt for lines that contain an a followed by a b followed by 0 or more c’s.” Unfortunately, 
the Bash interpreter does not treat it this way. Instead, globbing takes place prior to the 
execution of the egrep command. Let us assume that the current directory has a file named 
abcd.txt (and no other entries whose name starts with ab). When you enter the above egrep 
instruction, globbing takes place before egrep executes. Globbing will produce one match, 
the file abcd.txt. The result of globbing is that anything that matched replaces the string 
with the wildcard. So the instruction becomes 
egrep abcd.txt foo.txt
. That is, the 
regex 
abc*
is replaced by the regex 
abcd.txt
. Now, egrep executes. In this case, egrep 
will look for strings in foo.txt that match the regex 
abcd.txt
where the period is the 
metacharacter to match any single character. If foo.txt happened to have the string 
abc-
detxt
, it would match. However, if foo.txt had the string 
abcccc
, it would not match!
To force the Bash interpreter to not perform globbing mistakenly on the regular expres-
sion, you place the regular expression inside of quote marks. Aside from the 
*
, recall that 
globbing operates on 
?
and 
[]
. In addition, the Linux interpreter applies a different inter-
pretation to 
$
(variable name), 
{}
, and several other characters. So, between the possibility 
that globbing will interfere with the command and the possibility that the regular expres-
sion contains a blank space, you should always use the single quote marks. What about 
using the double quote marks
“”
? Recall that the Bash interpreter will perform operations 


Regular Expressions

225
on an item placed in double quote marks, such as “$NAME,” that it would not perform 
when the item is placed in single quote marks. For this reason, use the single quote marks 
and not the double quote marks.
6.5 SED
Sed is a stream editor. A stream editor is a program that takes a stream of text and modi-
fies it. This can be useful if you want to perform mass substitutions on a text file without 
having to open the file to perform a search and replace operation. In this section, we take 
an introductory look at how to use sed for fairly simple editing tasks. A full description of 
sed is well beyond the scope of this text.
6.5.1 Basic sed Syntax
With sed, you specify a regular expression which represents a pattern of what you want to 
replace. You then specify a replacement string to take the place of any strings that match 
the pattern. The generic form of a sed command is
sed ‘s/
pattern
/
replacement
/’ 
filename
where 
pattern
is the regular expression that describes the strings you wish to replace and 
replacement
is the replacement string.
By default, sed outputs the resulting file with the replacements to the terminal window. 
You might wish to then redirect the output to a new file. The format of the sed command 
then becomes
sed ‘s/
pattern
/
replacement
/’ 
filename
>
filename2
You would use a second file with redirection because the first file, filename, is currently 
open and so cannot be written to. You could later use 
mv 

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   84   85   86   87   88   89   90   91   ...   254




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish