Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Quote your arguments to <code>grep</code>:</p> <pre><code>grep "${trackid}" "${file}" &gt;&gt; "${outfile}" </code></pre> <p>This way if either <code>$trackid</code> or <code>$file</code> contains whitespace, it will be treated as one argument to <code>grep</code> rather than multiple arguments. Note that <a href="http://www.gnu.org/software/bash/manual/bashref.html#Appending-Redirected-Output" rel="nofollow"><code>&gt;&gt;</code> <em>appends</em></a> to the file "<code>${outfile}</code>" (rather than <a href="http://www.gnu.org/software/bash/manual/bashref.html#Redirecting-Output" rel="nofollow"><code>&gt;</code>, which would <em>truncate</em></a>).</p> <hr> <p>To remove the errant <code>\r</code>s from your input file, use <a href="http://www.manpagez.com/man/1/tr/" rel="nofollow"><strong>tr(1)</strong></a>:</p> <pre><code>tr -d '\r' &lt; "${track_path}" | while read trackid fileid # [...] done </code></pre> <p>I also recommend some sanity checking, such as:</p> <pre><code>if [[ -f "${file}" ]] ; then grep "${trackid}" "${file}" &gt; "${outfile}" else echo "Could not find file [${file}]. Skipping." fi </code></pre> <p>So your finished script <em>may</em> look like this:</p> <pre><code>#!/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' &lt; "${track_path}" | while read trackid fileid do file="input/track_param_${fileid}" if [[ -f "${file}" ]]; then grep "${trackid}" "${file}" &gt;&gt; "${outfile}" else echo "Could not find file [${file}]. Skipping." fi done </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload