Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a version that <em>might</em> work (haven't tested it). Here is what I used to test it.</p> <p>Creating the test environment:</p> <pre><code>mkdir out mkdir archive echo -e "one\ttwo\tthree\tfour\none\t---\tthree\tfour" &gt; test.ext echo -e "newone\tnewtwo\tnewthree\tfour" &gt; archive/test.arc rm out/test.ext </code></pre> <p>The following script works.</p> <pre><code>#!/bin/bash mylist="test.ext" path_archive="./archive/" path_out="./out/" process_line () { line=$1 name=$2 set -- $line if [ "$2" == "---" ] ; then ID=$(grep -F -w "$4" ${path_archive}/${name}.arc | awk '{print $2}') echo -e "$1\t$ID\t$3\t$4" else echo "$line" fi } # process the files in the list for fname in `cat $mylist` ; do echo processing $fname name=`basename $fname .ext` cat $fname | while read line ; do process_line "$line" "$name" &gt;&gt; $path_out/$name.ext done done </code></pre> <p>The <code>set -- $line</code> is a bash thing. This sets the positional parameters (<code>$1</code>, <code>$2</code>, ...) to whatever arguments were provided to <code>set --</code>. With no arguments, <code>set --</code> unsets the positional parameters. Consider the following example:</p> <pre><code>:~$ echo $1 :~$ set -- foo :-$ echo $1 foo :-$ set -- bar :-$ echo $1 bar </code></pre> <p>The above <code>for fname in `cat $mylist`; do</code> will not work if file names in <code>$mylist</code> contain spaces. If that is the case and there is exactly one file name in each line, you should do <code>cat $mylist | while read fname ; do</code> instead and make sure that <code>$fname</code> is always in double quotes (<code>"$fname"</code>).</p> <p>Output:</p> <pre><code>$ cat test.ext one two three four one --- three four $ cat out/test.ext one two three four one newtwo three four </code></pre> <p>All in all, I would rather use Perl or Python for a task like that. It will be much easier to write and debug.</p>
    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.
 

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