Results 1 to 6 of 6

Thread: Script - issue with SED command

  1. #1

    Default Script - issue with SED command

    Hi folks,

    I'm attaching an excerpt from a script I'm working at:

    HTML Code:
    for i in {1..$line_nr_log}; do
        tb=$(sed "$i"'q;d '"$log"); #line i of file log.txt (cotnaining the file/dir to be backed up) is read into variable $tb
        cp -v --parents $tb $backupdir >> $temp_operations_log; #file $tb is copied to the backup directory. $tb contains the path of the file relative to /home. All directories contained in the relative path are copied in the backup directory in order to preserve the original path; output of the command obtained in verbose mode is redirected to the temp_operations_log file
    done
    I'm getting following errors when running bash -xv for this script:

    HTML Code:
    for i in {1..$line_nr_log}; do
        tb=$(sed "$i"'q;d '"$log"); #line i of file log.txt (cotnaining the file/dir to be backed up) is read into variable $tb
        cp -v --parents $tb $backupdir >> $temp_operations_log; #file $tb is copied to the backup directory. $tb contains the path of the file relative to /home. All directories contained in the relative path are copied in the backup directory in order to preserve the original path; output of the command obtained in verbose mode is redirected to the temp_operations_log file
    done
    + for i in '{1..$line_nr_log}'
    sed "$i"'q;d '"$log")
    sed "$i"'q;d '"$log"
    ++ sed '{1..2609}q;d /var/run/media/BACKUP/BACKUP/fbackup_03.05.2014/log'
    sed: -e expression #1, char 3: unknown command: `.'
    + tb=
    + cp -v --parents
    cp: missing file operand
    Try 'cp --help' for more information.
    I guess there is something wrong about the syntax of the SED command, however I can't figure out which the issue is.

    I'm getting the same errors when replacing the $(sed ...) with `sed ...`.

    On the other hand when using sed without variables and only with single quotes I'm not getting any errors. Here's an example:

    HTML Code:
    central_backup_dir=`sed '1q;d' /home/test/bin/Backup_Scripts_Parameters`; #reading the path of the central backup directory, which is contained by the first line of the Backup_Scripts_Parameters file (this file contains certain parameters of the scripts fbackup.sh and dbackup.sh)
    Thanks in advance for your help!

    Cheers,
    Liviu

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,818

    Default Re: Script - issue with SED command

    As you can see, the $i is evaluated as {1..2609}, which I think is not what you want (and which sed (not SED btw) does not understand.

    Thus IMHO you must concentrate on the
    Code:
    for i in {1..$line_nr_log}
    to check what it does and what it should do.
    Henk van Velden

  3. #3

    Default Re: Script - issue with SED command

    brace expansions comes first before variable expansions. In this case it is seen as a literal $line_nr_log
    Code:
    for i in {1..$line_nr_log}
    Use a C-style for loop instead.

    Code:
    for ((i=0;i<${#line_nr_log};i++))
    Also "Don't Read Lines With For", use a while loop instead

    Code:
    while read -r line; do echo "$line"; done < log.txt
    You might not need sed for this imo. You can paste the sample input of your file and a sample output that you desire. So the folks can see what are you really trying to do.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,818

    Default Re: Script - issue with SED command

    And please do not use the HTML tags, but the CODE tags. It is the # button.
    Henk van Velden

  5. #5

    Default Re: Script - issue with SED command

    If i understand correctly you are trying to loop over the files in a directory and run sed on each file using the for loop. If so you can use a glob instead

    Code:
    for i in ./*; do echo "${i#*/}"; done
    If you need to do some test insert your test afer the do

    Code:
    for i in ./*; do [[ -f $i ]] && ....; done
    This sed '1q;d' reads the very first line of the file (if im not mistaken), if so you can test try this.

    Code:
    for i in ./*;do [[ -f "$i" ]] && read -r foo < "$i" && echo "$foo"; done
    A lot of this is just a guess and it would end up in a guessing games unless you state what are you really trying to do aka EndGoal
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  6. #6

    Default Re: Script - issue with SED command

    If you are inside the directory of your files e.g. you have cd into it, try this and hopefully it will give you and idea.

    for i in ./*; do [[ -f "$i" ]] && read -r FirstLine < "$i" && echo "First line of ${i##*/} is $FirstLine"; done
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

Posting Permissions

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