我有一个包含字段的csv文件: '文件路径''行号','说明'。例如:
/home/user/file.txt,121,xxxx
csv文件有很多条目。
如何解析字段文件路径和行号并存储它们?
解析文件路径后,我必须在Linux机器中打开文件,并在grep
中查找特定字符串。
有人能告诉我怎么做吗?
步骤是:
答案 0 :(得分:0)
cat sample.csv
home/user1/file1.txt,2,nice user1
cat home/user1/file1.txt
PATTERN1
PATTERN3
PATTERN2
PATTERN4
#Run till the last line of the file is reached.
while read line
do
#Capture the line number which is in 2nd column
line_number=$(echo $line |awk -F, '{print $2}')
#Capture the file name which is in 1st column
file_name=$(echo $line |awk -F, '{print $1}')
#echo each line and print the line in corresponding file and grep for PATTERN
cat $file_name |head -n $line_number |tail -1 |grep PATTERN
done <sample.csv