Showing posts with label LPI Guides. Show all posts
Showing posts with label LPI Guides. Show all posts

Thursday, 19 August 2021

Open, Simple, Generative: Why the Web is the Dominant Internet Application

LPI Exam Prep, LPI Tutorial and Materials, LPI Career, LPI Learning, LPI Preparation, LPI Study Materials

Everything in the 2021 Open Anniversary celebration comes together in the Open Web, the subject of this month's article. In fact, I have taken the words Open Web as almost a prompt for free association. While everyone appreciates the wealth of free information on the Web, readers of this article may be surprised at where the idea of the Open Web takes us.

Hypertext, the Internet Way

Let's start at the dawn of the World Wide Web. The two standards on which it originally rested were HTML and HTTP.

The “HT” that those abbreviations share stands for hypertext, a term famously invented by visionary philosopher Ted Nelson around 1965. As a somewhat heady and grandiose term that matches Nelson's expansive ambition and personality, hypertext recognizes that thoughts are never confined to individual documents. Each document inevitably refers to and depends on an understanding of other sources of information. This has always been true, but the web makes the relationships explicit through links.

A zeal for representing relationships was and remains the ultimate goal of Tim Berners-Lee, inventor of the web. Just glance through his original proposal to his management, the European Organization for Nuclear Research (CERN in its French abbreviation). The proposal is bound together throughout by a concern for exposing and organizing the relationships between thoughts and between things. The same obsession has remained through the decades in Berners-Lee's proposals for a Semantic Web (2000) and Linked Data (2006).

Grandiosity on the scale of Nelson and Berners-Lee is not entirely abjured by CERN, either, which presents the first-time visitor to its web site with the question, "What is the nature of our universe?"

So the idea of hypertext has been around for a while, but neither Nelson's grand vision (the stillborn Xanadu) nor later experiments such as Apple Computer's HyperCard caught on. I will say a bit about each of these projects in order to contrast them with the traits that took the World Wide Web on such a different path.

First, both Xanadu and HyperCard were proprietary. This limited chances for people outside the organizations developing each technology to add to it and build their own visions on it. The web, in contrast, because it was open, shared the amazing ability of certain computer technologies to spawn enormous innovation. In the term used by law professor Jonathan Zittrain, the web is generative.

Apples' Hypercard was starved for resources, admittedly, but I found little value to it in the first place. The design offered limited capabilities, probably because of the tiny computer memories and slow processors of the 1980s. Each piece of content had to fit on a small card. The biggest limitation was fundamental: each set of cards was self-contained and couldn't link to outside resources. It was left to Berners-Lee to make this major leap in hypertext power through one of his greatest inventions: the URL. These unassuming strings take advantage of the internet and Domain Name System—and Berners-Lee cooked into the URL ways to connect to content outside the web, a valuable gambit to gain a foothold in a sea of other protocols.

Xanadu was complex. This complexity stemmed from the best of intentions: Nelson insisted on creating bidirectional links, which could lead to all kinds of benefits. With bidirectional links, you could follow a site that is linking to you. Payment systems could be enabled—something many internet users dearly wish for now. There would be ways to preserve sites whose hosting computers go down—another serious problem always hovering over the internet, as I point out in my article “Open knowledge, the Internet Archive, and the history of everything.”

As Nelson said in a talk I attended in 2008, "Berners-Lee took the easy way out!" The web has one-directional links, and therefore suffers from all the aforementioned lapses that Nelson claimed would not have plagued Xanadu. To drive home how conscious this choice was, let's go back to Berners-Lee's proposal, mentioned earlier:

"Discussions on Hypertext have sometimes tackled the problem of copyright enforcement and data security. These are of secondary importance at CERN, where information exchange is still more important than secrecy. Authorisation and accounting systems for hypertext could conceivably be designed which are very sophisticated, but they are not proposed here."

Reams have been written, virtual and real, about the ramifications of Berners-Lee's prioritization. But history's verdict is pretty definitive: the easy way out is the right way. Like the larger internet, the web does not try to track down lost resources or assign value to traffic. Its job is just to get information from source to destination.

Open, simple, generative: these traits allowed the web to succeed where other systems had tried and failed. Web standards are debated, endorsed, and maintained by the nonprofit World Wide Web Consortium (W3C).

Berners-Lee also happened to come along at a good moment in computer history. He invented the web in 1989 and it picked up steam a couple of years later. This was the very time that the general public was discovering the internet, that odd research project used only by defense sites, scientific researchers, and a few related companies. (Most of the internet access points were run by the U.S. government until 1994.)

People had long been using non-internet discussion forums and clunky ways of transferring files. Much of the traffic now moved onto the web. And this leads to the next stage of the web's generativity.

Port 80 Finds New Uses

We need a bit of technical background to understand what happened next on the web.

Most computers run multiple programs. When a computer receives network traffic, it figures out which program to send it to, thanks to an arbitrary number called a port that is attached to each packet. A person may be talking to friends using internet relay chat (TCP port 6667), while receiving mail (TCP port 25), and so on. The web was awarded TCP port 80.

Meanwhile, to prevent malicious attacks, network and system administrators usually place firm restrictions on the ports where they accept traffic. In the mid-1990s, with the growth of the internet and the creation of high-speed, always-on connections, restrictions on ports hampered the introduction of new services. People would hear of some wonderful service that could enhance their productivity, but the network administrator either didn't trust the service or was too busy to reconfigure the network so it could send and receive traffic on the new port. The internet was experiencing one of the biggest booms known to any industry in history, and innovators were stymied by this odd technological straitjacket.

More and more, innovators gazed yearningly at the one port that was always guaranteed to be open: port 80, thanks to the universal adoration for the web. And so the developers made a momentous decision: they would violate the tradition of providing a unique port number for their application, and send everything over the Web. The user's web browser would receive and process the traffic. (I put this move into a broader context in a memoir, near the end of the section "Peer-to-peer (early 2000s) and the emergence of user-generated content.")

Although slapped with the censorious term "port 80 pollution" by network administrators, the movement toward web services succeeded beyond its wildest dreams and brought along Software as a Service. Many people spend all day on their browser, moving between Facebook, Google, Salesforce, etc.—with all the traffic moving through port 80.

Berners-Lee's HTTP protocol has now gone far beyond the web. It's the communications protocol of choice for the loosely coupled architecture known as microservices. This story is covered in my article "How the RESTful API Became Gateway to the World." That's the generative web in motion.

Web Hosting and the Democratization of the Internet

The simplicity of the web drove early adoption. Berners-Lee based HTTP on other standard protocols, making it recognizable to administrators. Meanwhile, he based the language for creating web pages, HTML, on an older standard called SGML, but made it rock-bottom easy to learn.

Furthermore, new HTML users could learn how to do cool new things with it just by viewing the source code to the web page in their browser. (I am indebted to Tim O'Reilly, who was my manager for many years, for pointing this out.) This transparency also applied to the languages for rich formatting (CSS) and dynamic web pages (JavaScript). Eventually, CSS and JavaScript were moved into separate files, and developers started shrinking or "minifying" code to save download time. Still, users could look into the files to study how to make web pages. People quit jotting their ideas into journals they shoved in desk drawers, and put their ideas up on the web.

As long as the internet ran on corporate servers, the professional administrators who managed the hardware and software could set up a web server like any other internet service. They could host a few web pages as well as the database that undergirded dynamic content. (See again my article "How the RESTful API Became Gateway to the World" for a history of dynamic content.) Everybody went through the administrator to put up a new web page.

The requirements changed in the early 2000s when millions of individuals started blogging, posting comments, and eventually uploading photos and videos. Tim O'Reilly coined the term "Web 2.0" for this explosion of individual contributions. Content generation was splitting off from web server management. The need was filled by content management systems (CMSes) and web hosting. Thousands of services now help people create their own web pages, providing CMS tools, databases, backups, and guaranteed up-time. Two of the most popular CMSes, WordPress and Drupal, are open source.

The open web depends on hosting. But you do give up some control when using a hosting service. A lot of sophisticated web operations use parts of the HTTP protocol that require control over the web server. A hosting service can also take down sites that it finds objectionable. (On the other hand, a take-down is less painful than hosting the site yourself and being sued or prosecuted.) The software that makes it so easy to build an attractive web site can also be limited or buggy.

The irony of Web 2.0 is that people can easily generate and disseminate content (sometimes racking up earnings in the hundreds of thousands of dollars) because of the technologies' simplicity—but at the same time cede control to social media platforms and other sites.

Many visionaries are trying to decentralize internet services, to make them more like the early days when most internet sites hosted their own servers. Various alternatives to centralized services exist, such as Jabber for chat (standardized now as the Extensible Messaging and Presence Protocol or XMPP) and Diaspora for social media. Proposals for decentralized services based on blockchains and cryptocurrencies revive Ted Nelson's goal of an internet where individuals can charge micropayments.

Accessibility Remains a Problem for Many

LPI Exam Prep, LPI Tutorial and Materials, LPI Career, LPI Learning, LPI Preparation, LPI Study Materials
The resources of the web should be available to everyone, but many factors hold access back: lack of internet connections, censorship, and non-inclusive web design. The article ends by discussing these issues.

Lack of Internet Connections

For years, the computer industry and the mainstream media have taken always-on, high-speed internet access for granted. The people working in those fields have internet access, and all their friends and neighbors do too. (Ironically, I am writing right now during a rainstorm that has cut my internet access, helping me to remember how privileged I usually am.)

The people who usually lacked access had far greater worries—lack of food, jobs, health care, or physical safety—and did not make universal access to the internet a major rallying cry. After the COVID-19 lockdowns revealed that children were being denied an education and adults were cut off from critical information because of limited internet access, some governments—although reeling from the pandemic—did start to look at solutions.

Earlier, some governments and NGOs had found ways to provide information through other media. The previous article in this Open Anniversary series mentioned Endless OS, which distributes computers loaded up with resources such as Wikipedia pages. Although internet access is richer, print-outs and computers can still provide desperately needed educational resources.

Censorship

Censorship is a more selective denial of internet access. There is no doubt that dangers lurk on the internet. Child pornography, terrorist recruitment, trade in illegal substances and stolen information—it all goes on. Censors target these problems, but also crack down on content that they consider politically or socially unacceptable. Because censorship requires central control over the gateways through which all internet content flows, censorship is usually found in highly centralized societies with strong central governments.

Because all of us know of some internet content we wish wasn't there, I will not argue the moral or political issues behind censorship. The topic of this section is what people do to get around it. The main remedy that has emerged is called an onion routing network. TOR, which was originally partly funded by the U.S. Navy, is the best-known of those networks today.

In an onion routing network, people who oppose censorship volunteer to host access points. If I want to reach a human rights researcher or (less sympathetically) want to buy ammunition online, I download a list of access points. I then send my message to one of the access points.

The access point has a list of other nodes in the onion routing network, and forwards my request to one chosen at random. The second node then routes my request to a third node, and so on. Like an onion, the anti-censorship network has many layers in order to make it hard to trace who sent a message to whom. The final nodes in the network routes my request to my recipient.

Because the lists of access points are public, censors know them too. It would be possible to block them all, and censors sometimes try. But the access points are numerous, change regularly, and often serve other purposes besides routing through the network. Sophisticated nodes introduce random delays between receiving a message and passing it on, to make it harder for a snooping observer to realize that the two messages are related.

Back to my successfully delivered request. Some information must be stored somewhere to allow the response to come back to me, and that's the feature of onion routing networks that is most vulnerable to attack. Like other types of cybersecurity, designers of onion routing networks are in constant competition with attackers.

Non-Inclusive Web Design

The final barrier I'll discuss in this article is web page designs that require a visitor to have good eyesight, good hearing, a steady hand, or some other trait that parts of the population lack. When advocates for the differently abled talk about "accessibility," they refer to designs that present no difficulties to anyone, or (because that's hard to achieve) offer workarounds for people with difficulties. Examples of accessibility features include:

◉ Supplementing different colors with other visual or textual cues to the differences in a web page

◉ Allowing text to be enlarged by the viewer

◉ Offering a textual description for each image, so that a person using a screen reader gets the most important information

◉ Adding closed-caption text to videos

◉ Allowing visitors to select elements from a screen without having to point and click

◉ Using familiar or standard design elements, so that visitors can apply knowledge they have learned from other sites

Many online tools exist to help designers check accessibility. In the United States, web sites should do whatever they need to conform to the American with Disabilities Act. Many companies also try to require accessibility on all their web sites. But most designers don't understand where their designs can exclude visitors, and guidelines often go unheeded.

Source: lpi.org

Saturday, 28 September 2019

Echo Command in Linux with Examples

Echo Command, Linux Certifications, Linux Tutorials and Materials, Linux Guides, LPI Online Exam

Echo is a Unix/Linux command tool used for displaying lines of text or string which are passed as arguments on the command line. This is one of the basic command in linux and most commonly used in shell scripts. In this tutorial, we will look at the different  options of echo command.

Basic Syntax


echo [option] [string]

1) Displaying a string on the terminal

To print text or a string on the terminal, use the syntax

echo [string]

$ echo "Welcome to Linux"

Sample Output

Welcome to Linux

2) Declare a variable and echo it value

Assume you have a variable x which is assigned the value 45  i.e.

$ x=10

You can print the value of variable x by executing the command

$ echo The value of x is $x

Sample Output

The value of x is 10

3) Remove  Spaces in between text using -e and \b options

You can choose to remove spaces using the \b option in conjunction with the -e parameter.

Note:

The -e parameter is used for the interpretation of backslash interpreters

Let's assume you have the text string

Linux is an opensource operating system

To remove spaces between the words, run

$ echo -e "Linux \bis \ban \bopensource \boperating \bsystem"

Sample Output

Linuxisanopensourceoperatingsystem

4) Create new lines in between text  using \n option
To create a new line after each word in a string use the -e operator  with the \n option  as shown

$ echo -e "Linux \nis \nan \nopensource \noperating \nsystem"

Sample Output

Linux
is
an
opensource
operating
system

5) Create horizontal tab space in between text  using \t option

If you want to create tab spaces in between words in  a string use the  -e operator  with the \t option  as shown

$ echo -e "Linux \tis \tan \topensource \toperating \tsystem"

Sample Output

Linux is an opensource operating system

5) Create vertical tab spaces using the \v option

You can decide to get a bit fancy and create vertical tab spaces using the -e operator  with the \v option  as shown

$ echo -e "Linux \vis \van \vopensource \voperating \vsystem"

Sample Output

Linux
       is
           an
               opensource
                          operating
                                     system

6) Double vertical tab using \n and \v option simultaneously

You can double the vertical tab spacing as shown below using the \n and \v options as shown

$ echo -e "Linux \n\vis \n\van \n\vopensource \n\voperating \n\vsystem"

Sample Output

Linux

is

an

opensource

operating

system

7) Print all files and folders using the * option

You can print all files and folders in your current working directory using the command

$ echo *

This has the same output as the ls command

Sample Output

Echo Command, Linux Certifications, Linux Tutorials and Materials, Linux Guides, LPI Online Exam

To print files of  a specific type run

$ echo *.file_extension

For example

$ echo *.pdf

Sample Output

Echo Command, Linux Certifications, Linux Tutorials and Materials, Linux Guides, LPI Online Exam

8) Using the carriage return '\r' option

The 'r' option gives you the carriage return i.e. any word(s) before the \r are omitted in the output

$ echo -e "Linux \r is an opensource operating system"

Sample Output

is an opensource operating system

9) Omit echoing trailing newline

The -n option is used for omitting trailing newline. This is shown in the example below

$ echo -n "Linux is an opensource operating system"

Sample Output

Linux is an opensource operating systemjames@buster:/$

Saturday, 6 July 2019

Studying for the LPI Certification Exam

One of the question I often answer when taking part in a Free Software or Education event is, "How can I study for the LPI Certification Exam?"

LPI Certification Exam, LPI Guides, LPI Learning, LPI Tutorials and Materials, LPI Study Materials

The answer for this question depends a lot on the student profile. Is she a self-taught person? Do they like to study in groups? Does he need a coach or teacher?

In one way or another, this follow up question is usually "where do I get materials to study for this or that exam?"

I always tell people that LPI is an independent certification body, providing the topics people need to know to take an exam and get certified. We do not produce educational material, as our official partners produce their own (sometimes sharing with each other) and also independent authors also write books, produce videos, podcasts and other materials. That said, I have a set of hints for people who want to get certified.

For those who feel more comfortable having a coach or teacher, the LPI partners page makes it easy to search for a partner near the place you live or work.

Others prefer to study alone or form a study team, which is my own preferred method. If that sounds like you, it's best to start by reading the official LPI topics in the site wiki, then decide which ones have the most appeal, as well as any areas where you feel the need to learn more.

Consider the following examples . . .

Our first student has been using Linux for quite a while and knows enough about using the shell and terminal. Reading the Linux Essentials exam topics though, he feels he needs to know more about Searching and Extracting Data from Files (Topic 3.2), using the cut command.

We have excellent free material on all the topics covered by the Linux Essentials certification, so I would first refer that student to the link where we collect these materials: https://wiki.lpi.org/wiki/Free_Training_Materials. Here in Brazil, where I live, our partner, 4Linux, has a free online training that also covers all of our LE topics.

The next student is a DevOps professional who, with minor exceptions, feels confident about all of our topics. For instance, she is not familiar with CORS headers and CSRF tokens, and the topic, "701.1 Modern Software Development", specifically asks for this.

Typically, DevOps professionals are quite knowledgeable in finding information, so I suggest some Google searches. Try them yourself:

"CORS headers"
"CORS headers" site:.edu
"CORS headers" filetype:pdf
"CORS headers" book
"CORS headers" tutorial

And as a plus:

fabian devops blog site:lpi.org

Since I mentioned DevOps, I'm proud to report that here, in Brazil, 4Linux has complete DevOps training covering all of our DevOps certifications. This is the first LPI partner in the world to have produced such courseware in a practical training environment.

Thursday, 18 April 2019

SED command in Linux - Set 2

We have discussed some of the SED command options in Sed Command in Linux/Unix with examples

SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search etc. It’s a one of the powerful utility offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you have the basic knowledge about Linux regular expressions.

SED Command, LPI Tutorial and Material, LPI Certifications, LPI Guides, LPI Learning

It provides Non-interactive editing of text files thats why it’s used to automate editing and has two buffers – pattern buffer and hold buffer. Sed use Patter buffer when it read files, line by line and that currently read line is inserted into pattern buffer whereas hold buffer is a long-term storage, it catch the information, store it and reuse it when it is needed. Initially, both are empty. SED command is used for performing different operation without even opening the file.

sed general syntax –


sed OPTIONS… [SCRIPT] [INPUTFILE…]

First create a.txt file on which I am going to perform operation for SED commands. In this blog, I used “a.txt” file to explain all the examples. Blog will become too long if i write the output of each sed command. So, you may refer the same file to practice all the commands initially.

[root@lpicentral ~]# cat a.txt
life isn't meant to be easy, life is meant to be lived.
Try to learn & understand something new everyday in life.
Respect everyone & most important love everyone.
Don’t hesitate to ask for love & don’t hesitate to show love too.
Life is too short to be shy.
In life experience will help you differentiating right from wrong.

# Let’s start with File Spacing


1 – Insert one blank line after each line –

[root@lpicentral ~]# sed G a.txt

2 – To insert two blank lines –

[root@lpicentral ~]# sed 'G;G' a.txt

3 – Delete blank lines and insert one blank line after each line –

[root@lpicentral ~]# sed '/^$/d;G' a.txt

4 – Insert a black line above every line which matches “love” –

[root@lpicentral ~]# sed '/love/{x;p;x;}' a.txt

5 – Insert a blank line below every line which matches “love” –

[root@lpicentral ~]# sed '/love/G' a.txt

6 – Insert 5 spaces to the left of every lines –

[root@lpicentral ~]# sed 's/^/     /' a.txt

# Numbering lines


1 – Number each line of a file (left alignment). **=** is used to number the line. \t is used for tab between number and sentence –

[root@lpicentral ~]# sed =  a.txt | sed 'N;s/\n/\t/'

2 – Number each line of a file (number on left, right-aligned). This command is similar to `cat -n filename`.

[root@lpicentral ~]# sed = a.txt | sed 'N; s/^/     /; s/ *\(.\{4,\}\)\n/\1  /'

3 – Number each line of file, only if line is not blank –

[root@lpicentral ~]#  sed '/./=' a.txt | sed '/./N; s/\n/ /'

# Deleting lines


1 – Delete a particular line –
Syntax: sed ‘nd’ filename
Example :

[root@lpicentral ~]# sed '5d' a.txt

2 – Delete the last line
Syntax: sed ‘$d’ filename

3 – Delete line from range x to y
Syntax: sed ‘x,yd’ filename
Example :

[root@lpicentral ~]# sed '3,5d' a.txt

4 – Delete from nth to last line
Syntax: sed ‘nth,$d’ filename
Example :

[root@lpicentral ~]# sed '2,$d' a.txt

5 – Delete the patter matching line –
Syntax: sed ‘/pattern/d’ filename
Example :

[root@lpicentral ~]# sed '/life/d' a.txt

6 – Delete lines starting from nth line and every 2nd line from there –
Syntax: sed ‘n~2d’ filename
Example :

[root@lpicentral ~]# sed '3~2d' a.txt

7 – Delete the lines which matches the pattern and 2 lines after to that –
Syntax: sed ‘/pattern/,+2d’ filename
Example :

[root@lpicentral~]# sed '/easy/,+2d' a.txt

8 – Delete blank Lines

[root@lpicentral ~]# sed '/^$/d' a.txt

9 – Delete empty lines or those begins with “#” –

[root@lpicentral ~]# sed -i '/^#/d;/^$/d' a.txt

# View/Print the files


If we want to view content of file, then we use cat command and if we want to view the bottom and the top content of any file, we use tools such as head and tail. But what if we need to view a particular section in the middle of any file? Here we’ll discuss, how to use SED command to view a section of any file.

1 – Viewing a file from x to y range –
Syntax: sed -n ‘x,yp’ filename
Example :

[root@lpicentral ~]# sed -n '2,5p' a.txt

2 – View the entire file except the given range –
Syntax: sed ‘x,yd’ filename
Example :

[root@lpicentral ~]# sed '2,4d' a.txt

3 – Print nth line of the file –
Syntax: sed -n ‘address’p filename
Example :

[root@lpicentral ~]# sed -n '4'p a.txt

4 – Print lines from xth line to yth line.
Syntax: sed -n ‘x,y’p filename
Example :

[root@lpicentral ~]# sed -n '4,6'p a.txt

5 – Print only the last line –
Syntax: sed -n ‘$’p filename

6 – Print from nth line to end of file –
Syntax: sed -n ‘n,$p’ filename
Example :

[root@lpicentral ~]# sed -n '3,$'p a.txt

Pattern Printing


7 – Print the line only which matches the pattern –
Syntax: sed -n /pattern/p filename
Example :

[root@lpicentral ~]# sed -n /every/p a.txt

8 – Print lines which matches the pattern i.e from input to xth line.
Syntax: sed -n ‘/pattern/,xp’ filename
Example :

[root@lpicentral ~]# sed -n '/everyone/,5p' a.txt
Following prints lines which matches the pattern, 3rd line matches the pattern “everyone”, so it prints from 3rd line to 5th line. Use $ in place of 5, if want to print the file till end.

9 – Prints lines from the xth line of the input, up-to the line which matches the pattern. If the pattern doesn’t found then it prints up-to end of the file.
Syntax: sed -n ‘x,/pattern/p’ filename
Example :

sed -n '1,/everyone/p' a.txt

10 – Print the lines which matches the pattern up-to the next xth lines –
Syntax: sed -n ‘/pattern/,+xp’ filename
Example :

sed -n '/learn/,+2p' a.txt

# Replacement with the sed command


1 – Change the first occurrence of the pattern –

[root@lpicentral ~]# sed 's/life/leaves/' a.txt

2 – Replacing the nth occurrence of a pattern in a line –
Syntax: sed ‘s/old_pattern/new_pattern/n’ filename
Example :

[root@lpicentral ~]# sed 's/to/two/2' a.txt
We wrote “2” because we replaces the second occurrence. Likewise you can use 3, 4 etc according to need.

3 – Replacing all the occurrence of the pattern in a line.

[root@lpicentral ~]# sed 's/life/learn/g' a.txt

4 – Replace pattern from nth occurrence to all occurrences in a line.
Syntax: sed ‘s/old_pattern/new_pattern/ng’ filename
Example :

[root@lpicentral ~]# sed 's/to/TWO/2g' a.txt
Note – This sed command replaces the second, third, etc occurrences of pattern “to” with “TWO” in a line.

If you wish to print only the replaced lines, then use “-n” option along with “/p” print flag to display only the replaced lines –

[root@lpicentral ~]# sed -n 's/to/TWO/p' a.txt
And if you wish to print the replaced lines twice, then only use “/p” print flag without “-n” option-

[root@lpicentral ~]# sed 's/to/TWO/p' a.txt

5 – Replacing pattern on a specific line number. Here, “m” is the line number.
Syntax: sed ‘m s/old_pattern/new_pattern/’ filename
Example :

[root@lpicentral ~]# sed '3 s/every/each/' a.txt
If you wish to print only the replaced lines –

[root@lpicentral ~]# sed -n '3 s/every/each/p' a.txt

6 – Replace string on a defined range of lines –
Syntax: sed ‘x,y s/old_pattern/new_pattern/’ filename
where,
x = starting line number
and y = ending line number

Example :

[root@lpicentral ~]# sed '2,5 s/to/TWO/' a.txt
Note – $ can be used in place of “y” if we wish to change the pattern up-to last line in the file.
Example :

[root@lpicentral ~]# sed '2,$ s/to/TWO/' a.txt

7 – If you wish to replace pattern in order to ignore character case (beginning with uppercase or lowercase), then there are two ways to replace such patterns –
Frist, By using “/i” print flag –
Syntax: sed ‘s/old_pattern/new_pattern/i’ filename
Example :

[root@lpicentral ~]# sed 's/life/Love/i' a.txt
Second, By using regular expressions –

[root@lpicentral ~]# sed 's/[Ll]ife/Love/g' a.txt

8 – To replace multiple spaces with a single space –
[root@lpicentral clang]# sed 's/  */ /g' filename

9 – Replace one pattern followed by the another pattern –
Syntax: sed ‘/followed_pattern/ s/old_pattern/new_pattern/’ filename
Example :

[root@lpicentral ~]# sed '/is/ s/live/love/' a.txt

10 – Replace a pattern with other except in the nth line.
Syntax: sed ‘n!s/old_pattern/new_pattern/’ filename
Example :

[root@lpicentral ~]# sed -i '5!s/life/love/' a.txt