Domanda

This question expands upon my last question (Search for specific line of text file, replace up to a certain character). With help from user @anubhava I was able to search for the line starting with dataspecs, and transform it from

dataspecs v1 v2 v3 v4 v5 ! blah to

dataspecs $new_val ! blah

However, I now need to be able to keep the variables v1 v2 v3 (I don't know their actual values), and replace v4 and v5 with bash variables $new_v4 and $new_v5. So my new line will now look like this:

dataspecs v1 v2 v3 new_v4 new_v5 ! blah

È stato utile?

Soluzione

awk -v n4="$new4" -v n5="$new5" '/^dataspecs/{$5=n4;$6=n5}7' file

Altri suggerimenti

Kent's solution is great. Here's another option that involves directly accessing shell variables from within awk

awk '/^dataspecs/{$5=ENVIRON["new_v4"];$6=ENVIRON["new_v5"];}{print}' file.txt > newfile.txt
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top