Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/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
在bashshell中使用grep_Bash_Grep - Fatal编程技术网

在bashshell中使用grep

在bashshell中使用grep,bash,grep,Bash,Grep,在while循环中使用grep命令时遇到问题。下面是我的代码: #!/bin/bash #FILE: grep_track #will read in a list of track IDs and grep the track data from the original track files set=-x track_list=Top95_HSI_forGrep.txt track_path="/mnt/gpfs/backup/jpty_surge/kimberly/Launch_mu

在while循环中使用grep命令时遇到问题。下面是我的代码:

#!/bin/bash
#FILE:  grep_track
#will read in a list of track IDs and grep the track data from the original track files
set=-x

track_list=Top95_HSI_forGrep.txt
track_path="/mnt/gpfs/backup/jpty_surge/kimberly/Launch_multiple_storms/input/$track_list"
outname=$track_list

#echo track_list $track_list
#echo track_path $track_path
#echo outname $outname

IFS=$","
while read trackid fileid 
do
   file="input/track_param_$fileid"
   outfile="output/$outname"
   echo fileid $fileid
   echo trackid $trackid
   echo file $file
   echo outfile $outfile
   grep $trackid $file > $outfile 
done < $track_path

有人能帮我弄清楚发生了什么事吗?谢谢

将您的参数引用到
grep

grep "${trackid}" "${file}" >> "${outfile}"
这样,如果
$trackid
$file
包含空格,它将被视为
grep
的一个参数,而不是多个参数。请注意,对于文件“
${outfile}
”(而不是)


要从输入文件中删除错误的
\r
s,请使用:

因此,您完成的脚本可能如下所示:

#!/bin/bash
#set -x

track_list=Top95_HSI_forGrep.txt
track_path="${1-/mnt/gpfs/backup/jpty_surge/kimberly/Launch_multiple_storms/input}/${track_list}"
outname="${track_list}"
output_directory="output"
outfile="${output_directory}/${outname}"

if [[ ! -f "${track_path}" ]]; then
    echo "Could not find track_path input [${track_path}]. Exiting"
    exit
fi

if [[ ! -d "${output_directory}" ]] ; then
    echo "Creating output directory [${output_directory}]..."
    mkdir -p "${output_directory}"
fi

IFS=$","
tr -d '\r' < "${track_path}" | while read trackid fileid
do
    file="input/track_param_${fileid}"
    if [[ -f "${file}" ]]; then
        grep "${trackid}" "${file}" >> "${outfile}"
    else
        echo "Could not find file [${file}]. Skipping."
    fi
done
#/bin/bash
#集合x
track\u list=Top95\u HSI\u forGrep.txt
track_path=“${1-/mnt/gpfs/backup/jpty_surge/kimberly/Launch_multiple_storms/input}/${track_list}”
outname=“${track\u list}”
output\u directory=“output”
outfile=“${output\u directory}/${outname}”
如果[!-f“${track_path}”];然后
echo“找不到跟踪路径输入[${track\u path}]。正在退出”
出口
fi
如果[!-d“${output_directory}”];然后
echo“正在创建输出目录[${output\u directory}]…”
mkdir-p“${output_directory}”
fi
如果S=$”,”
tr-d'\r'<“${track_path}”|同时读取trackid fileid
做
file=“input/track_param_${fileid}”
如果[[-f“${file}”]];然后
grep“${trackid}”“${file}”>>“${outfile}”
其他的
echo“找不到文件[${file}]。正在跳过。”
fi
完成

我试过这个,它说:
第23行:{output/Top95\u HSI\u forGrep.txt}:没有这样的文件或目录存在并且是可写的。你能想到这里还有什么可能出错的地方吗?谢谢grep“${trackid}”“${fileid}”>>“${outname}”实际上是这样编码的错误返回到我最初引用的内容::没有这样的文件或目录1.txt它在查找输入文件$fileid时遇到问题,这是track_param_0001.txt它看起来像
/mnt/gpfs/backup/jpty_surge/kimberly/Launch_multiple_storms/input/Top95_HSI_forGrep.txt中的一行,包含两个或多个逗号。不幸的是,没有。当我在命令行中输入这一切时,它工作正常。OK。将
set=-x
更改为
set-x
以启用调试。好的,它告诉我file=$'input/track\u param\u 0001.txt,\r'肯定是输入文件的格式有问题。谢谢你的帮助,指引我走到这一步!
tr -d '\r' < "${track_path}" | while read trackid fileid 
# [...]
done
if [[ -f "${file}" ]] ; then
    grep "${trackid}" "${file}" > "${outfile}"
else
    echo "Could not find file [${file}]. Skipping."
fi
#!/bin/bash
#set -x

track_list=Top95_HSI_forGrep.txt
track_path="${1-/mnt/gpfs/backup/jpty_surge/kimberly/Launch_multiple_storms/input}/${track_list}"
outname="${track_list}"
output_directory="output"
outfile="${output_directory}/${outname}"

if [[ ! -f "${track_path}" ]]; then
    echo "Could not find track_path input [${track_path}]. Exiting"
    exit
fi

if [[ ! -d "${output_directory}" ]] ; then
    echo "Creating output directory [${output_directory}]..."
    mkdir -p "${output_directory}"
fi

IFS=$","
tr -d '\r' < "${track_path}" | while read trackid fileid
do
    file="input/track_param_${fileid}"
    if [[ -f "${file}" ]]; then
        grep "${trackid}" "${file}" >> "${outfile}"
    else
        echo "Could not find file [${file}]. Skipping."
    fi
done