从unix命令行上的文件中的行创建一个路径 [英] create a path from lines in a file on the unix command line

查看:145
本文介绍了从unix命令行上的文件中的行创建一个路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bash脚本中构建一个基本的Java类路径。

I want to build a basic Java class path in a bash script.

这个命令可以处理我的路径中的所有jar文件:

This command works out all the jar files to go in my path:

find ~/jars | grep \.jar$

这会列出我想要的路径上的所有jar文件。

This lists all the jar files I want on my path, one per line.

如何将这些行连接在一起,使它们在一行上,由分隔,我可以在我调用Java时在我的脚本中使用一个变量?

How can I join together these lines so they are on one line, separated by : and put that into a variable I can use in my script when I invoke Java?

例如:

JARS=`find ~/jars | grep \.jar$`
#### somehow make JARS into one line with : between entries
java -cp $JARS ...


推荐答案

您可以管道它,并且sed改变新行

You can pipe it and with sed change new line for ,:

sed ':a;N;$!ba;s/\n/:/g'

一起,

find ~/jars | grep \.jar$ | sed ':a;N;$!ba;s/\n/:/g'

你也可以用 tr 来做,虽然它留下一个结尾冒号( glenn jackman comment ):

You can also do it with tr, although it leaves a trailing colon (glenn jackman comment):

find ~/jars | grep \.jar$ | tr '\n' ':'

基于 SED:如何替换换行符(\\\
)?

这篇关于从unix命令行上的文件中的行创建一个路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆