-2

Is it possible to use sed in bash for? eg.:

list(){
    cat file.yml | awk '{print $1}' | cut -d ':' -f 1 | tail -n +2
}

var2=$(cat file.yml | awk '{print $2}')

for key in $(list)
    do
        new=$(git log -1 --pretty="format:%ct" $key)
        sed 's/$key/$new/' file.yml
done

I think it's wrong because every for cycle the sed want to change every line.I mean is it not possible to use sed in for.

file.yml:

catalog/view/css/animate.css/3.1.1/animate.min.css: 1494509091
catalog/view/fonts/cinzel/fonts.css: 1494509091
catalog/view/fonts/garamondcondot-book/fonts.css: 1494509092
catalog/view/fonts/gotham-narrow/fonts.css: 1494509092
    catalog/view/fonts/gotham/fonts.css: 1494509092
    catalog/view/fonts/greatvibes/fonts.css: 1494509092
    catalog/view/fonts/indie-flower/fonts.css: 1494509092
Inian
  • 80,270
  • 14
  • 142
  • 161
TDex
  • 61
  • 7
  • What do you mean `sed wants to change every line`? Is it that everything is printed to console? That's normal behaviour if you have not told it to edit the file (which you haven't) – fredrik Jun 03 '20 at 09:17
  • Posted. Yes, the file was printed the same number the lines. – TDex Jun 03 '20 at 09:19
  • @TDex : Just being curious: What's the purpose of `var2`? You never use it. – user1934428 Jun 03 '20 at 11:02
  • There's enough wrong with your code that you should throw it away and start over. Copy/paste it into http://shellcheck.net to get a report on some of the fundamental issues. If you want help to do whatever it is you're trying to do the right way then post a new question about that and include a [mcve] with concise, testable sample input and expected output. See [ask]. – Ed Morton Jun 03 '20 at 13:15
  • 1
    As an example of one of the issues shellcheck won't tell you about - the contents of your `list()` function, `cat file.yml | awk '{print $1}' | cut -d ':' -f 1 | tail -n +2`, can be written as just `awk 'NR>1{sub(/:.*/,"");print $1}' file.yml`. As another - your sed command will have false matches for any file names containing regexp metachars like `.`. Also your `for` loop is not how to read the output of a command, see https://mywiki.wooledge.org/BashFAQ/001. I could go on... – Ed Morton Jun 03 '20 at 13:20

1 Answers1

0

Every iteration of the loop will read the file.yaml, and send to stdout an updated version of file.yaml, where all occurances of the literal string $key is replaced by the literal string $new in the output (because you wrapped the argument to sed between single quotes). file.yaml itself will never change. You end up with a bunch of "copies" of file.yaml written to stdout.

So to answer your question literally: Is it possible to use sed inside a loop? Yes, of course. There is no law which would forbid it. Do you actually use it in a sensible way? I don't know, because I don't know what you wanted (and this is also not part of the question).

UPDATE: Fixed the answer by incorporating the comment given by @Socowi.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • 1
    "*`where all occurances of the respective $key is replaced by $new`*" you may want to point out, that `$key` is the literal string `'$key'` here and **not** the value `"$key"` of a variable name `key`. – Socowi Jun 03 '20 at 10:57
  • Right. Same for `$new`. – user1934428 Jun 03 '20 at 10:59