≡ Menu

sed command

Linux / Unix: sed Command Print Only Matching Lines

I see many examples and man pages on how to do operations like search-and-replace using sed. But, I want to match a given string or a regular pattern expression and display on the screen. How do I print lines with matching pattern using sed command only under Unix like operating systems?
[click to continue…]

Linux / Unix sed: Delete Word From File / Input

I've a file as follows:

This is a test.
One bang two three
Foo dang Bar
001 0xfg 0xA
002 0xA foo bar 0xfG
I'm done

Tutorial details
DifficultyIntermediate (rss)
Root privilegesNo
Requirementssed and bash/ksh
Estimated completion timeN/A

How do I delete all "words" from the above file which ends with a particular letter (say 'g') in each line? The output should be as follows:

This is a test.
One two three
Foo Bar
001 0xA
002 0xA foo bar
I'm done

How do I delete regex-based word using sed or awk under Linux / Unix like operating systems?
[click to continue…]

I got over 100s of HTML files in the following format:

 <title>Statistics for ABC LTD - January 2007 - Rang IDXYZZAZZZZ</title>
<body BGCOLOR="#E8E8E8" TEXT="#000000" LINK="#0000FF" VLINK="#FF0000">
<h2>Statistics for ABC LTF</h2>
Summary Period: January 2007<br />
Generated 01-Feb-2007 06:40 CET<br />
<hr />
<font SIZE="-1"></font></p></center><pre>
my data 1
my data 2
my data 3
my data 10000
my data N times

Generated by MyAppDbStatsWriter (UNIX) version 1.9b2

How do I extract text between two words (<PRE> and </PRE>) in unix or linux using grep command?
[click to continue…]

I'm using the date +'%D_%T' to store Unix system date and time in a shell variable called $_now:

_now="$(date +'%D_%T')"
echo $_now


I'd like to replace / and : with _. I'm aware of the following sed command:

sed 's/\//_/g
> s/:/_/g' < <<"$_now"


How do I specify two pattern within the same sed command to replace | and : with _ so that I can get output as 01_20_12_16_10_42?
[click to continue…]

Linux / Unix: Sed Delete Matching Words In A File

I have a file called rsnapshot.conf. It has the words "ssh_args=-p 1222". How do I match and remove (delete) the words "ssh_args=-p 1222" from config file using sed command under Linux or Unix like operating systems?
[click to continue…]

My script depends upon $1 to take certain actions:

echo "|${file}|"

However, sometime there will leading while space which will result into:

|     output.txt|

How do I trim leading white space from one of my input variables?
[click to continue…]

sed Delete / Remove ^M Carriage Return [ Line Feed ]

How can I remove the ^M or ^M (carriage Return / line feed ) from text file using sed under UNIX or Linux operating systems?
[click to continue…]