Bash在sed中使用变量名来插入记录

时间:2014-11-30 03:45:20

标签: bash sed

我正在尝试在bash shell中使用sed在第1行之前插入一个新行:作为Field Separator到现有文件但是当我这样做时,它会打印变量名而不是值。这就是我所拥有的。 Sed似乎有变量名称的古怪问题。有什么建议吗?

#!/bin/bash
select CHOICE in add remove list find exit
do
echo "Pick a directory option: "
 case $CHOICE in
  add)
   printf "What is the first name? "
    read first
   printf "What is the last name? "
    read last
   printf "What is the street address? "
    read address
   printf "What is the city? "
    read city 
   printf "What is the State abreviation? "
    read state
   printf "What is the zip code? "
    read zip
   printf "What is the phone number? "
    read phone
   cat listing.txt | sed '1 i\ > "$first" ":" "$last" ":" "$address" ":" "$city" ":" "$state" ":" "$zip" ":" "$phone"' listing.txt;;
 esac
done

1 个答案:

答案 0 :(得分:0)

我假设您正在尝试编辑listing.txt吗?而不是以cat listing.txt...开头的行,请使用:

sed -i.bak "
1 i\\
$first : $last : $address : $city : $state : $zip : $phone
" listing.txt

这假设您实际上并不想在listing.txt文件中使用引号。它还会将listing.txt.bak留作编辑文件的备份。