1

I have a file which looks like below:

memory=500G
brand=HP
color=black
battery=5 hours

For every line, I want to remove everything after = and also the =.

Eventually, I want to get something like:

memory:brand:color:battery: 

(All on one line with colons after every word)

Is there a one-line sed command that I can use?

user1691717
  • 213
  • 1
  • 7
  • 15

5 Answers5

2
sed -e ':a;N;$!ba;s/=.\+\n\?/:/mg' /my/file

Adapted from this fine answer.

To be frank, however, I'd find something like this more readable:

cut -d = -f 1 /my/file | tr \\n :
Community
  • 1
  • 1
pilcrow
  • 56,591
  • 13
  • 94
  • 135
1

Here's one way using GNU awk:

awk -F= '{ printf "%s:", $1 } END { printf "\n" }' file.txt

Result:

memory:brand:color:battery:

If you don't want a colon after the last word, you can use GNU sed like this:

sed -n 's/=.*//; H; $ { g; s/\n//; s/\n/:/g; p }' file.txt

Result:

memory:brand:color:battery
Steve
  • 51,466
  • 13
  • 89
  • 103
1

This might work for you (GNU sed):

sed -i ':a;$!N;s/=[^\n]*\n\?/:/;ta' file
potong
  • 55,640
  • 6
  • 51
  • 83
1
perl -F= -ane '{print $F[0].":"}' your_file

tested below:

> cat temp
abc=def,100,200,dasdas
dasd=dsfsf,2312,123,
adasa=sdffs,1312,1231212,adsdasdasd
qeweqw=das,13123,13,asdadasds
dsadsaa=asdd,12312,123
> perl -F= -ane '{print $F[0].":"}' temp
abc:dasd:adasa:qeweqw:dsadsaa:
Vijay
  • 65,327
  • 90
  • 227
  • 319
0

My command is

First step:

sed 's/([a-z]+)(\=.*)/\1:/g' Filename |cat >a

cat a

memory:

brand:

color:

battery:

Second step:

sed -e 'N;s/\n//' a | sed -e 'N;s/\n//'

My output is

memory:brand:color:battery:

loganaayahee
  • 809
  • 2
  • 8
  • 13