简体   繁体   English

是否可以使用 linux 比较同一行 in.txt 文件的 2 个内容?

[英]Is it possible to compare 2 contents of same line in .txt file using linux?

Let's say I have the following content in text file假设我在文本文件中有以下内容

input.txt
aaa bbb
ccc ddd

I would like to compare user input with the content in text file.我想将用户输入与文本文件中的内容进行比较。

Let's say user is prompted to enter 2 strings, if first string matches the content in text file (let's say aaa, which is the first content of line 1) , how should I modify my code to make the program to compare the second string that user entered with the second content (bbb) on the same line?假设提示用户输入 2 个字符串,如果第一个字符串与文本文件中的内容匹配(比如说 aaa,这是第 1 行的第一个内容) ,我应该如何修改我的代码以使程序比较第二个字符串用户在同一行输入了第二个内容(bbb)

echo "First input: "; read input1
echo "Second input: "; read input2
if [ "$input1" = $(< input.txt) ]; then
 # content matches
else
 # content doesn't match
fi

Read the first line from input.txt :input.txt中读取第一行:

read -r shouldbe_input1 shouldbe_input2 < <(head -n1 input.txt)

Then just compare:然后只是比较:

if [ "$input1" = "$shouldbe_input1" ] && [ "$input2" = "$shouldbe_input2" ]; then
       echo yay
else
       echo not yay

fi

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

相关问题 如何在 Linux 命令行中使用 wget 将网页内容下载到 .txt 文件? - How to download the contents of a web page on to a .txt file using wget in Linux command line? Linux 命令将文件内容每行一个字复制到从同一命令创建的新文件中 - Linux command to copy file contents one word per line into a new file created from the same command 如何在Linux bash中将相邻行放在.txt文件中的同一行上? - How do I put adjacent lines in a .txt file on the same line in linux bash? 比较两个目录并在txt文件中将输出与Linux中的文件名不同 - Compare two directory and output in txt file with with differ file name in linux 循环遍历 txt 文件的内容以在第二个 txt 文件中查找匹配项(Mac OS 或 Linux) - Loop through contents of txt file to find match in 2nd txt file (Mac OS or Linux) 在Linux中使用php创建txt文件 - Creating a txt file using php in linux 将文件的内容插入另一个文件(在发送的文件的特定行中)-BASH / LINUX - insert the contents of a file to another (in a specific line of the file that is sent)-BASH/LINUX 在txt文件Linux命令行上显示php输出 - Display php output on a txt file Linux command line 如何将输入保存到txt文件。 在linux命令行输入 - how to save inputs to a txt file. In linux command line inputing 跳过mysql中的第一行导出到Linux中的.txt文件 - skip first line in mysql export to .txt file in linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM