Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex awk-F最终删除字段I';我想回去_Regex_Linux_Bash_Unix_Awk - Fatal编程技术网

Regex awk-F最终删除字段I';我想回去

Regex awk-F最终删除字段I';我想回去,regex,linux,bash,unix,awk,Regex,Linux,Bash,Unix,Awk,所以我有一个库脚本,我必须为类编写。它有几个功能,如addbook、deletebook、checkout等。问题在于我的checkout功能 colin@Colins-三星:~$bash--版本 GNUBash,版本4.2.45(1)-发行版(x86_64-pc-linux-GNU) 版权所有(C)2011免费软件基金会。 许可证GPLv3+:GNU GPL版本3或更高版本 首先是我的字符串(用逗号分隔的字段) 表格书、作者、图书馆、日期 string= Unix for programmer

所以我有一个库脚本,我必须为类编写。它有几个功能,如addbook、deletebook、checkout等。问题在于我的checkout功能

colin@Colins-三星:~$bash--版本 GNUBash,版本4.2.45(1)-发行版(x86_64-pc-linux-GNU) 版权所有(C)2011免费软件基金会。 许可证GPLv3+:GNU GPL版本3或更高版本

首先是我的字符串(用逗号分隔的字段)

表格书、作者、图书馆、日期

string= Unix for programmers,Graham Glass,mylibrary,11-11-13
我已经在代码的前几行中声明了我的标题、库和日期,但是当我试图声明作者时,我使用了这一行代码

author=`awk -F, '/'$title'/ {print $2}' $library`
我相信这就是我的问题所在

字符串的最终结果是作者变为null 所以在我的函数完成后,字符串现在是

 Unix for programmers,,mylibrary,11-11-13
因此,这条线似乎发生了一些事情: “/”$title“/{print$2}” 我的问题是什么

我试过了

author=`awk -F, "/'$title'/ {print $2}" $library`
我也试过了##

但在这两种情况下,我都会遇到一些错误,要么是失控的正则表达式,要么是无效的命令

这是我试图修复的整个函数

checkout(){

echo please enter your username 
read username
uCount=`egrep "$username" $library | wc -l`
if (( $uCount >=3 ))
then 
    echo "Sorry, you can only have 3 books checked out at a time"
else 
    echo "what is the name of the book you would like to check out?"
    read title
    exists=`egrep "$title" $library | wc -l`
    if (( $exists == 0 ))
    then 
        echo "Sorry, but this book does not exist"
    else 

    author=`awk -F, '/'$title'/ {print $2}' $library`

    ##author=`egrep "$title" $library | awk -F, '{print $2}' $library`
    ##author=`awk -F, "/'$title'/ {print $2}" $library`
    ##String = unix,graham glass,mylib,11-11-13

    updated=$title,$author,$username,`date +%F`
    sed -e "/$title/d" $library > $tmp
    echo $updated >> $tmp
    mv $tmp $library
    echo "$title succesfully checked out"
    fi
fi

请告知。提前感谢您提供的任何帮助

向awk添加变量,请执行以下操作

awk -v var="$variable" '{$0=var}' file

因此:

author=`awk -F, '/'$title'/ {print $2}' $library`
应该是这样的

author=$(awk -F, '$0~var {print $2}' var="$title" $library)

PS最好使用括号
var=$(code)
compare back tics

如果脚本中有很多shell错误,那么对于各种输入和数据值,它可能会以多种方式失败。你也有逻辑问题。想象一下,如果一个名叫
Theo
的家伙想使用你的图书馆,而你的图书馆里有3本书的书名是
Theory
,会发生什么?可怜的老提奥再也借不到书了

shell是一个调用工具的环境。它有编程结构来帮助您对工具的调用排序。仅此而已。您不应该试图使用shell构造来解析文本文件——这就是awk的发明目的,因此它非常擅长于此


如果您希望帮助正确书写此内容,请告诉我们。

哦,谢谢!我不记得上课时讲过这个,我们很快就复习了awk。这修复了我脚本中的几个错误。谢谢你,乔特!
author=`awk -F, '/'$title'/ {print $2}' $library`
author=$(awk -F, '$0~var {print $2}' var="$title" $library)