简体   繁体   English

awk -F最终删除我想要返回的字段

[英]awk -F ends up deleting the field I'm trying to return

So I have a library script I have to write for class. 所以我有一个我必须为课程编写的库脚本。 It has several functions, such as addbook, deletebook, checkout, etc. The problem is in my checkout function. 它有几个功能,如addbook,deletebook,checkout等。问题出在我的结账功能。

colin@Colins-Samsung:~$ bash --version GNU bash, version 4.2.45(1)-release (x86_64-pc-linux-gnu) Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html colin @Colin-Samsung:〜$ bash --version GNU bash,版本4.2.45(1)-release(x86_64-pc-linux-gnu)版权所有(C)2011 Free Software Foundation,Inc。许可证GPLv3 +:GNU GPL版本3或更高版本http://gnu.org/licenses/gpl.html

so firstly here is my string (fields seperated by commas) 所以首先是我的字符串(以逗号分隔的字段)

form book,author,library,date 表格书,作者,图书馆,日期

string= Unix for programmers,Graham Glass,mylibrary,11-11-13

I've already declared my title, library, and the date in previous lines of the code, but when I try to declare author I use this line of code 我已经在代码的前几行中声明了我的标题,库和日期,但是当我尝试声明作者时我使用了这行代码

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

I believe this is where my problem lies 我相信这就是我的问题所在

What ends up happening to the string is that the author becomes null so after my function completes, the string is now 最终发生在字符串上的是作者变为null所以在我的函数完成之后,现在是字符串

 Unix for programmers,,mylibrary,11-11-13

So it would seem that something happens in the piece of the line: '/'$title'/ {print $2}' My question is what? 所以看起来行中出现了一些事情:'/'$ title'/ {print $ 2}'我的问题是什么?

I've tried 我试过了

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

I've also tried ## 我也试过##

author=`egrep "$title" $library | awk -F, '{print $2}' $library`

But on both accounts, I get some error, either a runaway regular expression, or invalid command. 但是在这两个帐户上,我都会收到一些错误,无论是失控的正则表达式还是无效的命令。

Here is the entire function I'm trying to fix 这是我正在尝试解决的整个功能

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

Please advise. 请指教。 Thanks in advance for any help 在此先感谢您的帮助

To add variable in to awk do like this. 要将变量添加到awk,请执行此操作。

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

or 要么

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

So this: 所以这:

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

should be like this 应该是这样的

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

PS its better to use parentheses var=$(code) compare back tics PS最好使用括号var=$(code)比较回tics

You have a LOT of shell errors in your script, it could fail in many ways for various inputs and data values. 脚本中有很多shell错误,它可能在很多方面对各种输入和数据值失败。 You also have logic problems. 你也有逻辑问题。 Imagine what would happen if a guy named Theo wanted to use your library and you had 3 books in your library whose title contained the word Theory ? 想象一下如果一个名叫Theo想要使用你的图书馆并且你的图书馆里有3本书的标题包含了Theory这个词会怎么样? Poor old Theo would never be able to borrow a book! 可怜的老西奥永远无法借书!

The shell is an environment from which to call tools. shell是一个可以从中调用工具的环境。 It has programming constructs to help you sequence the calls to tools. 它具有编程结构,可帮助您对工具的调用进行排序。 That is all. 就这些。 You shouldn't be trying to us shell constructs to parse text files - that's what awk was invented to do and so it is very good at it. 您不应该尝试使用shell构造来解析文本文件 - 这就是awk发明要做的事情,所以它非常擅长。

If you'd like help writing this correctly, let us know. 如果你想帮助写这个,请告诉我们。

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

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