lidl
lidl

Reputation: 49

String concating using linux pipe

Script:

env|grep JAVA_HOME|cat >>aa.txt

aa.txt will get values: JAVA_HOME=...
How can I script to get values as: export JAVA_HOME=...
This script is wrong:

env|grep JAVA_HOME|cat 'export'$0>>aa.txt

Upvotes: 1

Views: 159

Answers (3)

Chandru
Chandru

Reputation: 1334

You can use this command to get the required output

 $ env | grep JAVA_HOME | sed 's/.*/export &/' | cat >> aa.txt

.* represent anything from the output
& represent all in the first field of .*.

Upvotes: 2

Michael
Michael

Reputation: 1503

You can use awk utility:

env|grep JAVA_HOME|awk '{print "export "$0;}'|cat >> aa.txt

$0 - means printing all the input columns (default column separator is space)

Upvotes: 1

DevSolar
DevSolar

Reputation: 70273

For the general case, including the possibility of more than one matching line for the grep and that you might want to do complex work on each line, you can feed a pipe to a loop:

env | while read line; do echo "export ${line}"; done

Alternatively, you could use sed:

env | sed "s/^/ export/"

(^ indicating start-of-line.)

Upvotes: 1

Related Questions