简体   繁体   English

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

[英]Bash Using variable names in sed to insert record

I'm trying to use sed in bash shell to insert a new row before row 1 with : as the Field Separator into an existing file but when I do it, it's printing the variable names instead of the values. 我试图在bash shell中使用sed在第1行之前插入新行:作为字段分隔符到现有文件中,但是当我这样做时,它会打印变量名而不是值。 Here's what I have. 这就是我所拥有的。 Sed seems to have quirky issues with variable names. Sed的变量名似乎有古怪的问题。 Any suggestions? 有什么建议么?

#!/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

I assume you're trying to edit listing.txt in place? 我假设您正在尝试在适当位置编辑listing.txt? Instead of the line starting cat listing.txt... , use this: 而不是以cat listing.txt...开头的行,请使用:

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

This assumes that you didn't actually want quotes in the listing.txt file. 假设您实际上并不想在listing.txt文件中使用引号。 It will also leave a listing.txt.bak as a backup to the edited file. 它还将留下一个listing.txt.bak作为已编辑文件的备份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM