September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  
104

Categories

Archives

[one-liner]: Joining All Lines Using sed

Recently I was trying to install Python 2.5 on a CentOS 5 OpenVZ VE. I found a blog post over on the blog.bashton.com site that provided some RPMS but they were for the x86_64 architecture and I needed them for i386. Not to worry, they also provided a SRPM so I could just hit it with rpmbuild.

Now I’m a keyboard and shell kind of guy but I’m lazy. So anytime I can take the output from one command and use it as arguments into another command I’m all for it. Here is a cool trick where I can take the output from the rpmbuild of the python 2.5 SRPM and create a list of all the packages that I need to yum install.

The victim

Doing the rpmbuild of a SRPM returns this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
% rpmbuild --rebuild python25-2.5.1-bashton1.src.rpm 2>&1 | grep "is needed" | sed 's/^[ \t]\+//g'
readline-devel is needed by python25-2.5.1-bashton1.i386
openssl-devel is needed by python25-2.5.1-bashton1.i386
gmp-devel is needed by python25-2.5.1-bashton1.i386
ncurses-devel is needed by python25-2.5.1-bashton1.i386
gdbm-devel is needed by python25-2.5.1-bashton1.i386
zlib-devel is needed by python25-2.5.1-bashton1.i386
expat-devel is needed by python25-2.5.1-bashton1.i386
libGL-devel is needed by python25-2.5.1-bashton1.i386
tk is needed by python25-2.5.1-bashton1.i386
tix is needed by python25-2.5.1-bashton1.i386
gcc-c++ is needed by python25-2.5.1-bashton1.i386
libX11-devel is needed by python25-2.5.1-bashton1.i386
glibc-devel is needed by python25-2.5.1-bashton1.i386
pkgconfig is needed by python25-2.5.1-bashton1.i386
tcl-devel is needed by python25-2.5.1-bashton1.i386
tk-devel is needed by python25-2.5.1-bashton1.i386
tix-devel is needed by python25-2.5.1-bashton1.i386
bzip2-devel is needed by python25-2.5.1-bashton1.i386
sqlite-devel is needed by python25-2.5.1-bashton1.i386
autoconf is needed by python25-2.5.1-bashton1.i386
db4-devel >= 4.3 is needed by python25-2.5.1-bashton1.i386
tcl is needed by python25-2.5.1-bashton1.i386

Here we’re picking out all the lines that contain “is needed” with this: grep “is needed”. Next we’re cutting out any spaces or tabs that prefix each line with this command: sed ‘s/^[ \t]\+//g’.

Pre magic

Next we can use a quick awk “{ print $1 }” to truncate everything after the package names to get to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
% rpmbuild --rebuild python25-2.5.1-bashton1.src.rpm 2>&1 | grep "is needed" | sed 's/^[ \t]\+//g' | awk '{print $1}'
readline-devel
openssl-devel
gmp-devel
ncurses-devel
gdbm-devel
zlib-devel
expat-devel
libGL-devel
tk
tix
gcc-c++
libX11-devel
glibc-devel
pkgconfig
tcl-devel
tk-devel
tix-devel
bzip2-devel
sqlite-devel
autoconf
db4-devel
tcl
The magic

The real magic and the reason I’m taking the time to write this post up is this little bit of sed magic here:

1
2
% rpmbuild --rebuild python25-2.5.1-bashton1.src.rpm 2>&1 | grep "is needed" | sed 's/^[ \t]\+//g'| awk '{print $1}' | sed -e :a -e '$!N;s/\n/ /;ta'
readline-devel openssl-devel gmp-devel ncurses-devel gdbm-devel zlib-devel expat-devel libGL-devel tk tix gcc-c++ libX11-devel glibc-devel pkgconfig tcl-devel tk-devel tix-devel bzip2-devel sqlite-devel autoconf db4-devel tcl

This little gem sed -e :a -e ‘$!N;s/\n/ /;ta’ takes all the lines coming in and joins them into a single line.

The final product …

Now you can wrap this command with a yum install `…` like this, and let the power of the shell do all the dirty work for you.

1
yum -y install `rpmbuild --rebuild python25-2.5.1-bashton1.src.rpm 2>&1 | grep "is needed" | sed 's/^[ \t]\+//g' | awk '{print $1}' | sed -e :a -e '$!N;s/\n/ /;ta'`

NOTE: For further details regarding my one-liner blog posts, check out my one-liner style guide primer.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>