0

I would like to replace a line in file which start with the content of variable $cont, using awk.

5473  12G Ju 2 06:32 part3
5423  11G Ju 6 07:32 part4
573  11G Ju 2 08:21 part5
5473  11G Ju 6 09:12 part6
5423  1G Jl 6 09:32 part7

I'm using sed now but taking too much time to do the same.

sed code is given below.

Now I'll find and delete the line starts with $cont and write the $cont to the file.

cont="foo"
 newLineCont ="this is new line"
    parallel -a test.txt -k --block 30M --pipe-part "sed -i /^$cont/d" test.txt
      echo $newLineCont >> test.txt

wanted to search and replace lines in the file which starts with the content of the variable $cont by content of $newLineCont using AWK

closed as unclear what you're asking by Kusalananda, Isaac, G-Man, Jesse_b, slm Aug 2 '18 at 16:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • your question is not clear.. you want replace the contents of the file ? $cont with $newLineCont ? – Kamaraj Aug 2 '18 at 7:41
  • 1
    try without parallel it might go faster – Kiwy Aug 2 '18 at 7:42
  • Note that looking for ^54 would also find the lines starting with e.g. 5473. – Kusalananda Aug 2 '18 at 7:48
  • @Kamaraj I wanted to search and replace lines in the file which starts with the content of the variable $cont by content of $newLineCont – Juhan Aug 2 '18 at 8:07
  • @Juhan You want to replace the whole line or just the number at the start? – Kusalananda Aug 2 '18 at 8:11
1

Replacing each whole line with $newLineCont if the first whitespace-delimited field is equal to $cont:

awk -v c="$cont" -v nc="$newLineCont" '$1 == c { $0 = nc } 1' <infile >outfile

This will read from infile and create a new file called outfile. If a line's first field is equal to $cont, the whole line will be replaced by $newLinecont.

The trailing 1 is a short way of writing { print } and will cause all lines to be outputted.


Taking Stéphane Chazelas comment below into account (in cases where one or both of your variables contain backslashes):

env c="$cont" nc="$newLineCont" awk '$1 == ENVIRON["c"] { $0 = ENVIRON["nc"] } 1' <infile >outfile

Using sed:

sed "s/^$cont[^0-9].*/$newLineCont/" <infile >outfile

With sed, we have to be more careful to match the correct thing with out regular expression. If $cont is 12, we do not want to match lines starting with 123. This is why I explicitly match a non-digit after $cont with [^0-9] (I'm assuming $cont is a number).

As with the awk command, this reads from infile and writes to outfile.

If $cont or $newLineCont contains slashes, these would have to be escaped as \/, and if $cont contains any other character that is special in regular expressions, these would also need to be escaped properly.

  • That assumes $cont and $newLineCont don't contain backslash characters (and of course that $cont doesn't contain blanks or newlines but that's obvious). Use ENVIRON if they might contain backslashes. – Stéphane Chazelas Aug 2 '18 at 8:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.