Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    'sed' with Positional Parameters


    I'm new with 'sed' and for sure something still I don't understand yet with it. If you see my output on ">Output..." portion, the new directory still on "source_dir" instead of "dest_dir". You may disregard for the "tar" part, this is just a test script, just for me to understand 'sed' using the Positional Parameters ($1 $2 $3..). Could you shed some light for me please? I really appreciate any help.


    File contents (*.txt) in source folder (source_dir):
    Code:
    /home/test/scripts/sed/source_dir/file1/file1.txt
    /home/test/scripts/sed/source_dir/file2/file2/file2.txt
    /home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt

    Objective:
    I want to compress all those *.txt files on a new directory "dest_dir" but with their own subdirectories (according to souce folder), like the output below:
    Code:
    /home/test/scripts/sed/dest_dir/file1/file1.txt
    /home/test/scripts/sed/dest_dir/file2/file2/file2.txt
    /home/test/scripts/sed/dest_dir/file3/file3/file3/file3.txt
    My script:
    Code:
    #!/bin/sh
    input_file_type="txt"
    
    find $1 -name *.$input_file_type | sort | while read in_file
    do
            echo ">Input "$in_file
    
            out_file=$(echo $in_file | sed 's/$1/$2/g')
    
            echo ">Output "$out_file
    
            tar -zcvf "$in_file" "$out_file"
    done
    My Output with the script:
    Code:
    [test-centoslab sed]$ ./zip_test ~/scripts/sed/source_dir/ ~/scripts/sed/dest_dir/
    >Input /home/test/scripts/sed/source_dir/file1/file1.txt
    >Output /home/test/scripts/sed/source_dir/file1/file1.txt  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    tar: Removing leading `/' from member names
    /home/test/scripts/sed/source_dir/file1/file1.txt
    >Input /home/test/scripts/sed/source_dir/file2/file2/file2.txt
    >Output /home/test/scripts/sed/source_dir/file2/file2/file2.txt  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    tar: Removing leading `/' from member names
    /home/test/scripts/sed/source_dir/file2/file2/file2.txt
    >Input /home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt
    >Output /home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    tar: Removing leading `/' from member names
    /home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt
    [test-centoslab sed]$

  2. #2
    -->
    Seems I made it worked!

    Below is the change I made:
    Code:
    #!/bin/sh
    input_file_type="txt"
    
    find $1 -name *.$input_file_type | sort | while read in_file
    do
            echo ">Input "$in_file
    
            out_file=$(echo $in_file | sed 's,'$1','$2',g')
    
            echo ">Output "$out_file
    
            tar -zcvf "$in_file" "$out_file"
    done
    And my output, the destination folder is now come out correctly at last!:
    Code:
    [test@centoslab sed]$ ./zip_test ~/scripts/sed/source_dir/ ~/scripts/sed/dest_dir/
    >Input /home/test/scripts/sed/source_dir/file1/file1.txt
    >Output /home/test/scripts/sed/dest_dir/file1/file1.txt
    tar: Removing leading `/' from member names
    tar: /home/test/scripts/sed/dest_dir/file1/file1.txt: Cannot stat: No such file or directory
    ------------ snipped ------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •