Operating System - HP-UX
1777169 Members
3463 Online
109065 Solutions
New Discussion юеВ

Search and replace in unix(Replace colon with space in /etc/passwd)

 
SOLVED
Go to solution
Sammy_2
Super Advisor

Search and replace in unix(Replace colon with space in /etc/passwd)

How do you replace :(colon) with a space in the example below , using cut or awk, etc?
Should look like:
sam 111
dave 232

==========unix command below=======
# cut -d: -f1,3 /etc/passwd > output
sam:111
dave:232

==============
Not modifying /etc/passwd . Just sending the output to another file
good judgement comes from experience and experience comes from bad judgement.
12 REPLIES 12
Geoff Wild
Honored Contributor
Solution

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

cut -d: -f1,3 /etc/passwd |sed 's/:/ /' > output



Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Chris Watkins_1
Respected Contributor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

And the awk way:

# cut -d: -f1,3 /etc/passwd |awk -F: '{print $1 " " $2}' > output


Not without 2 backups and an Ignite image!
Rajeev  Shukla
Honored Contributor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

Hi,
Use
sed 's/:/ /g' /etc/passwd > output
Chris Watkins_1
Respected Contributor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

Shouldn't have said "the" awk way...
there are always lots of ways to do things :-)

awk -F: '{print $1 " " $3}' < /etc/passwd > output
Not without 2 backups and an Ignite image!
Dave Johnson_1
Super Advisor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

how about
awk -F: '{print $1 " " $2}' output

-Dave
Dave Johnson_1
Super Advisor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

Chris,
I guess greate minds think alike.
You just type faster than I.

-Dave
Bill Hassell
Honored Contributor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

And yet another way...

cut -d: -f1,3 /etc/passwd | tr -s ":" " "


Bill Hassell, sysadmin
Chris Hulihan
Advisor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

if you have perl, you could do
perl -pi -e 's|\:| |g' output

to update the "output" file.
Shahul
Esteemed Contributor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)


Hi,

Please find below command, this helps. But don't do it on original passwd file.

#sed 's/:/ /g' passwd > passwd.new

Best of luck
Shahul
H.Merijn Brand (procura
Honored Contributor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

# perl -aF: -nle'print"@F[0,2]"' /etc/passwd

or shorter, but more cryptic

# perl -aF: -ple'$_="@F[0,2]"' /etc/passwd

Enjoy, Have FUN! H.Merijn [ Who thinks -i is extremely dangerous on /etc/passwd ]
Enjoy, Have FUN! H.Merijn
Jordan Bean
Honored Contributor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

Even the posix shell can do it:

#!/usr/bin/sh
while IFS=: read login pw uid gid etc
do
echo $login $uid
done < /etc/passwd > output

Sammy_2
Super Advisor

Re: Search and replace in unix(Replace colon with space in /etc/passwd)

Thanks everyone. All of them work. AWK , SED , Bill's Tr was different and Procur and Chris added perl to it.
good judgement comes from experience and experience comes from bad judgement.