0

Observed result

While writing a function that removes a variable nr of n lines from a file, the output file has an additional space at the first position of each line (after the first line).

For example:

some text
another text
third text
fourth text
fifth text

goes to:

third text
 fourth text
 fifth text

after removing the first n=2 lines.

Expected result

I would intend/expect those additional spaces not to be added, in essence:

third text
fourth text
fifth text

Code

The function that does the removal consists of:

#!/bin/bash
apt_update() {
    source src/hardcoded_variables.txt

    # copy target file
    cp $INPUT_PATH $REMAINING_LINES_IN_TARGET_AFTER_STARTING_LINE

    # get starting line number
    starting_line=$(<$STARTING_LINE_QUERY_VAR_PATH)
    
    # remove first starting_line lines from the copied target file
    echo -n $(sed "1,${starting_line}d" $REMAINING_LINES_IN_TARGET_AFTER_STARTING_LINE) > $REMAINING_LINES_IN_TARGET_AFTER_STARTING_LINE
}
apt_update "$@"

Question

How could I prevent the adding of the space at the start of each line after the first line in the output file?

a.t.
  • 2,002
  • 3
  • 26
  • 66
  • 5
    Remove `echo -n $(` and `)`? – Cyrus Apr 02 '21 at 22:55
  • `sed "2,${starting_line}d" $REMAINING_LINES_IN_TARGET_AFTER_STARTING_LINE > "$REMAINING_LINES_IN_TARGET_AFTER_STARTING_LINE"` yields an empty output file. – a.t. Apr 02 '21 at 22:59
  • 1
    What's with that weird echo/sed/redirection? Use `ed` or `sed -i` to modify the file in place. – Shawn Apr 02 '21 at 23:01
  • 3
    Generally in bash `ALLCAPS` variables are reserved for system or environment variables. (and shorter descriptive names would make things easier to read) – David C. Rankin Apr 02 '21 at 23:02
  • 3
    See http://mywiki.wooledge.org/BashPitfalls#cat_file_.7C_sed_s.2Ffoo.2Fbar.2F_.3E_file for examples and details of why you got an empty file. – Shawn Apr 02 '21 at 23:03
  • 1
    Anyways, you're getting extra spaces because you're not quoting the `$(sed ...)` bit. See the next pitfall after the one in my earlier link. – Shawn Apr 02 '21 at 23:08
  • 1
    See: [Text substitution (reading from file and saving to the same file) on linux with sed](https://stackoverflow.com/q/6484340/3776858) and [Find and replace in file and overwrite file doesn't work, it empties the file](https://stackoverflow.com/q/5171901/3776858) – Cyrus Apr 02 '21 at 23:22

1 Answers1

0

The answer was given as a comment by Shawn, I used a convoluted method of sed that led to an error. Furthermore, I first made a copy of a file and that tried to apply sed to that same file. When I first made the copy, but then piped from the original file to the copied file, the target file was filled as expected. Hence, a working solution was found with:

#!/bin/bash
apt_update() {
    source src/hardcoded_variables.txt

    # copy target file
    cp $INPUT_PATH $REMAINING_LINES_IN_TARGET_AFTER_STARTING_LINE

    # get starting line number
    starting_line=$(<$STARTING_LINE_QUERY_VAR_PATH)
    
    # remove first starting_line lines from the copied target file
    sed "1,${starting_line}d" $INPUT_PATH > $REMAINING_LINES_IN_TARGET_AFTER_STARTING_LINE
}
apt_update "$@"

I did need to make the target copy before applying sed in this solution.

a.t.
  • 2,002
  • 3
  • 26
  • 66