sed command to find a particular word - linux

//ask.census.gov/
//ask.census.gov/
//business.usa.gov/
//directorsblog.blogs.census.gov/
//public.govdelivery.com/accounts/USCENSUS/subscriber/new
//twitter.com/uscensusbureau
//www.calendarwiz.com/calendars/calendar.php?crd=cens1sample&cid[]=31793
//www.census.gov/2017censustests
//www.census.gov/2017censustests/
//www.census.gov/2020census
//www.census.gov/2020census
//www.census.gov/2020census/
//www.census.gov/about.html
//www.census.gov/about.html
I want the lines which have www in them by using sed command

Use sed:
sed -ne '/\/www\./p' file
Or grep:
grep '/www\.' file
Or awk
awk '/\/www\./' file

Related

i want add 3 line with sed or awk command in file config.xml

I have a linux centos 7 server and i want Below lines to file with name config.xml using sed command
<vhostMap>
<vhost>google</vhost>
<domain>google.com, www.google.com</domain>
</vhostMap>
i want add this lines after line 10 at config.xml file
i want add this with a command at centos7, its possible?
i have searched and i saw this is possible with sed or awk command
how i can do this with sed or awk command?
GNU sed:
sed '10a <vhostMap>\n <vhost>google</vhost>\n <domain>google.com,www.google.com</domain>\n</vhostMap>' config.xml
Almost the same with awk:
awk '{ print $0 ; if(NR == 10) printf "<vhostMap>\n <vhost>google</vhost>\n <domain>google.com, www.google.com</domain>\n</vhostMap>\n" }' config.xml
This might work for you (GNU sed and shell):
cat <<\! | sed '10r /dev/stdin' file
<vhostMap>
<vhost>google</vhost>
<domain>google.com, www.google.com</domain>
</vhostMap>
!
Place the lines to be appended in a here-doc and append them after line 10 of the file by reading them in as stdin via a pipe.

UNIX sed command not removing spaces in cat output

I have a file as below.
Hi this is first line
this is second line
this is third line
Expected is:
Hi this is first line
this is second line
this is third line
What i used is
cat file.txt | sed 's/ //g'
returns,
Hi this is first line
this is second line
this is third line
For a portable sed command use this:
sed 's/^[[:blank:]]*//' file
[[:blank:]] matches space or tab.
EDIT: To remove all spaces using awk:
awk '{$1=$1}1' OFS= file
OR sed:
sed 's/[[:blank:]]*//g' file
sed in your example will replace all the spaces
sed 's/^[ \t]*//' file.txt
cat file.txt | sed -e 's/^[ \t]*//'
OR
sed 's/^[ \t]*//' file.txt
OR if you want to modify file.txt and remove the white spaces in the beginning of the line :
sed -i 's/^[ \t]*//' file.txt
try this line
sed -r 's/^\s*//' file
to remove all spaces(tabs): sed -r 's/\s//g' file
kent$ echo "Hi this is first line
this is second line
this is third line"|sed -r 's/\s//g'
Hithisisfirstline
thisissecondline
thisisthirdline
The most efficient way to remove all blanks from your input is probably not using sed at all, but
tr -d '[:blank:]' < file.txt
That's different from what you have originally asked for, though (removing initial whitespace only).

sed extract text between two patterns where second pattern may be either of one

I am trying to extract text between pattern1 (fixed) and pattern2 (this can be p2-1/p2-2).
can you please tell me how to achieve this in a single command?
A file starts with start and ends with either end or close
File1:
======
junktest
data
start
stackoverflow
sed
close
File2:
======
data2
start
stackoverflow
end
I can extract text from File1 with
sed -n "/start/,/close/p"
And from File2 with
sed -n "/start/,/end/p"
I need a single sed command to achieve both..
something like:
sed -n "/start/, /close or end /p"
Both GNU sed and BSD sed:
sed -nE '/start/,/close|end/p' file
This awk looks better
awk '/start/,/end|close/' file
sed -n -E "/Word1/,/Word2-1/p" | sed -n -E "/Word1/,/Word2-2/p"
Easy with awk:
$ awk '/start/{p=1}p{print}/end|close/{p=0}' file

How to delete lines from file with sed\awk?

I have file, with lines, contains ip with netmask
a.b.c.d/24
w.x.y.z/32
etc
How to delete delete specific row?
i'm using
sed -ie "s#a.b.c.d/24##g" %filname%
but after the removal is an empty string in file.
It should run inside a script, with ip as parameter and also work in freebsd under sh.
Sed solution
sed -i '/<pattern-to-match-with-proper-escape>/d' data.txt
-i option will change the original file.
Awk solution
awk '!/<pattern-to-match-with-proper-escape>/' data.txt
Using sed:
sed -i '\|a.b.c.d/24|d' file
Command line arg:
For the input being command line argument, say 1st argument($1):
sed -i "\|$1|d" file
Replace $1 with appropriate argument number as is your case.
You should use d (delete) not g. Also do not use s (replacement).
sed -ie '/a.b.c.d\/24/d' %filename%
In a script you should using it in this way
IP=$1
IPA=${IP////\\/}
sed -i /"${IPA}"/d %filename%
And the script parameter should be called in this way:
./script.sh a.b.c.d/24
perl -i -lne 'print unless(/a.b.c.d\/24/)' your_file
or in awk if you donot want to do inplace editing:
awk '$0!~/a.b.c.d\/24/' your_file

I want to use "awk" or sed to print all the lines that start with "comm=" in a file

I want to use "awk" or "sed" to print all the lines that start with comm= from the file filex, Note that each line contains "comm=somthing"
for example : comm=rm , comm=ll, comm=ls ....
How can i achieve that ?
For lines that start with comm=
sed -n '/^comm=/p' filex
awk '/^comm=/' filex
If comm= is anywhere in the line then
sed -n '/comm=/p' filex
awk '/comm=/' filex
You could use grep also :
grep comm= filex
this will display all the lines containing comm=.
Here's an approach using grep:
grep -o '\<comm=[[:alnum:]]*\>'
This treats a word as consisting of alphanumeric characters; extend the character class as needed.
If grep is ok to use, you could give a try to:
grep -E "^comm=" file

Resources