Search This Blog

Tuesday, September 13, 2011

HowTo: Append to the end of line in a file.

Sed - An Introduction and Tutorial
http://www.linuxquestions.org/questions/linux-newbie-8/sed-append-text-to-end-of-line-if-line-contains-specific-text-how-can-this-be-done-684650/

The original answer needed a little more to be general, and allow a variable to be passed to sed to create append to the end of a particular line. In this case, a file when sourced defined an environment variable. This variable could then be used in a for loop to process a list of things to do.

cat <<EOF >${RESULTS}/suite_id.prop
SUITE_ID=""
TEST_ID=""
EOF
suite_id=1104;test_id=3007
# tried gawk...sed wins this time.
#{ rm ${RESULTS}/suite_id.prop && gawk '/SUITE_ID/{print $0 "$suite_id :";}' > ${RESULTS}/suite_id.prop; } < ${RESULTS}/suite_id.prop
sed -i '/SUITE_ID/s|"$| '"$suite_id"'"|' ${RESULTS}/suite_id.prop
sed -i '/TEST_ID/s|"$| '"$test_id"'"|' ${RESULTS}/suite_id.prop
source ${RESULTS}/suite_id.prop
for i in $SUITE_ID; do
    echo "SUITE_ID: $i"
done
for i in $TEST_ID; do
    echo "TEST_ID: $i"
done

No comments:

Post a Comment