Using cat, head, tail, and tr with pipelines

Trying to do this little exercise. It’s not for homework or anything, but I want to make sure I understand it.

Create 2 files. file1 will have 4 lines of text. file2 will have 6 lines of text. Create a pipeline that will print the last 2 lines of file1 and the first 3 lines of file2. Transliterate all lowercase characters to uppercase in file1 and all uppercase characters to lowercase in file2.

Use no temporary files.

Use command grouping at the start of the pipeline with two tr commands.

And my script:

#!/bin/bash

i=0;
while [ $i -lt 4 ]
do
    echo "line $i of file1";
    i=`expr $i + 1`;
done > file1

i=0;
while [ $i -lt 6 ]
do
    echo "LINE $i OF FILE2";
    i=`expr $i + 1`;
done > file2

{
    tr [:lower:] [:upper:] < `cat file2 file1 | tail -2`;
    tr [:upper:] [:lower:] < `cat file2 file1 | head -3`;
}

I feel like this is close, but I keep getting the ambiguous redirect error. Any help would be greatly appreciated.


Source: unix

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.