Linux with Operating System Concepts



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

first_name middle_initial. last_name
to the format
last_name, first_name
while omitting the middle initial entirely. This would change 
Homer J. Simpson
to be 
Simpson, Homer
. We need to remember all three parts of the name.
For a first name, we would expect 
[A-Z][a-z]
+
, for the middle initial, we would expect 
[A-Z]\.
, and the last name would be the same as the first. We would embed each of these 
in 
\(\)
marks giving us
\([A-Z][a-z] 
+
\) \([A-Z]\.\) \([A-Z][a-z] 
+
\)


230

Linux with Operating System Concepts
Our replacement string would be 
\3, \1
. That is, we would put the third string first 
followed by a comma followed by a space followed by the first string, and no second string. 
The sed command is
sed ‘s/\([A-Z][a-z] 
+
\) \([A-Z]\.\) \([A-Z][a-z] 
+
\)/
\3, \1/’ names.txt
Unfortunately, the above sed command will not work if the first name is just an initial 
or if the person’s name has no middle initial. To solve these problems, we can use multiple 
pattern/substitution pairs using the –e option. In the first case, we replace 
[A-Z][a-z]
+
with 
[A-Z]\.
giving us 
–e ‘s/\([A-Z]\.\) \([A-Z]\.\) \([A-Z][a-z]
+
\)/\3, 
\1/’
. For the second case, we remove the middle portion of the expression and renumber 
\3
to 
\2
since there are only two patterns we are remembering now, giving us 
–e ‘s/\
([A-Z][a-z]
+
\) \([A-Z][a-z]
+
\)/\2, \1/’
As you can see, sed commands can become both complicated and cryptic very quickly. 
We end this section with a brief listing of examples. Assume names.txt is a file of names 
as described above. The first example below capitalizes every vowel found. The second 
example fully capitalizes every first name found. Notice the use of 
*
for the lower-case let-
ters. This allows us to specify that the first name could be an initial (in which there are no 
lower-case letters). The third example removes all initials from the file. The fourth example 
replaces every blank space (which we assume is being used to separate first, middle, and 
last names) with a tab. The fifth example matches every line and repeats the line onto two 
lines. In this case, the replacement is & (the original line) followed by a new line (\n) fol-
lowed by & (the original line again).
• sed ‘s/[aeiou]/\u&/g’ names.txt
• sed ‘s/[A-Z][a-z]*/\U&/’ names.txt
• sed ‘s/[A-Z]\.//g’ names.txt
• sed ‘s//\t/g’ names.txt
• sed ‘s/[A-Za-z.]
+
/&\n&/’ names.txt
6.6 awk
The awk program, like sed and grep, will match a regular expression against a file of strings. 
However, grep returns the matching lines and sed replaces matching strings, the awk pro-
gram allows you to specify actions to perform on matching lines. Actions can operate 
on the substring(s) that matched, other portions of the line, or perform other operations 
entirely. In essence, awk provides the user with a programming language so that the user 
can specify condition and action pairs to do whatever is desired. We also see in this section 
that awk does not need to use regular expressions for matches but instead can use different 
forms of conditions.


Regular Expressions

231
Overall, awk (named after its authors Aho, Kernighan, and Weinberger) is a more pow-
erful tool than either grep or sed, giving you many of the same features as a programming 
language. You are able to search files for literal strings, regexes, and conditions such as if 
a particular value is less than or greater than another. Based on a matching line, awk then 
allows you to specify particular fields of a matching line to manipulate and/or output, use 
variables to store values temporarily, and perform calculations on those variables, output-
ting results of the calculations.
Unlike grep and sed, awk expects that the text file is not just a sequence of lines but 
that each line is separated into fields (or columns). In this way, awk is intended for use on 
tabular information rather than ordinary text files. While you could potentially use awk 
on a textfile, say a text document or an email message, its real power comes into existence 
when used on a file that might look like a spreadsheet or database, with distinct rows and 
columns. Figure 6.4 illustrates this idea where we see two matching lines with awk operat-
ing on the values of other fields from those lines.
6.6.1 Simple awk Pattern-Action Pairs
The structure of a simple awk command will look something like this:
awk ‘/

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   87   88   89   90   91   92   93   94   ...   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