0

I'm trying to edit 3 columns in a file if the value in column 1 equals a specific string. This is my current attempt:

cp file file.copy
awk -F':' 'OFS=":" { if ($1 == "root1") $2="test"; print}' file.copy>file
rm file.copy

I've only been able to get the awk command working with one column being changed, I want to be able to edit $3 and $8 as well. Is this possible in the same command? Or is it only possible with separate awk commands or with a different command all together?

Edit note: The real command i'll be passing variables to the columns, i.e. $2=$var

It'll be used to edit the /etc/passwd file, sample input/output:

root:$6$fR7Vrjyp$irnF38R/htMSuk0efLSnAten/epf.5v7gfs0q.NcjKcFPeJmB/4TnnmgaAoTUE9.n4p4UyWOgFwB1guJau8AL.:17976::::::
6
  • 1
    edit your question to show us the code where you tried to edit $3 and it failed and tell us in what way it failed. Also include concise, testable sample input and expected output. Commented Mar 29, 2019 at 13:06
  • 1
    After the edit, try awk -v var1="$v1" -v var2="$v2" -F':' '$1 == "root1" { $2 = var1; $3 = var2}1' FS=":" file > tmp && mv tmp file. Commented Mar 29, 2019 at 13:11
  • 2
    Dear AceVenturos, you seem to have a perfectly running command and your request is rather a straightforward adjustment of the existing command. With a bit of reading about awk you would be able to make the adjustment yourself in minutes. While I strongly support your quest for knowledge, please be advised that Stack Overflow is not a code writing service. If you want to know more about awk, which I strongly recommend as it is a great tool to know, have a look at catonmat.net/awk-one-liners-explained-part-one Commented Mar 29, 2019 at 13:11
  • @WiktorStribiżew thank you, I appreciate it! Commented Mar 29, 2019 at 13:15
  • @kvantour Thank you, that's on me, my patience has been thin the last few days but I'll do more research in the future. Appreciate the link! Commented Mar 29, 2019 at 13:16

2 Answers 2

2

You can create multiple statements for the if condition with a block {}.

awk -F':' 'OFS=":" { if ($1 == "root1") {$2="test"; $3="test2";} print}' file.copy>file

You can also improve your command by using awk's default "workflow": condition{commands}. For this you need to bring the OFS to the input variables (-v flag)

awk -F':' -v OFS=":" '$1=="root1"{$2="test"; $3="test2"; print}' file.copy>file
Sign up to request clarification or add additional context in comments.

Comments

2

You may use

# Fake sample values
v1=pass1
v2=pass2
awk -v var1="$v1" -v var2="$v2" 'BEGIN{FS=OFS=":"} $1 == "root1" { $2 = var1; $3 = var2}1' file > tmp && mv tmp file

See the online awk demo:

s="root1:xxxx:yyyy
root11:xxxx:yyyy
root1:zzzz:cccc"
v1=pass1
v2=pass2
awk -v var1="$v1" -v var2="$v2" 'BEGIN{FS=OFS=":"} $1 == "root1" { $2 = var1; $3 = var2}1' <<< "$s"

Output:

root1:pass1:pass2
root11:xxxx:yyyy
root1:pass1:pass2

Note:

  • -v var1="$v1" -v var2="$v2" pass the variables you need to use in the awk command
  • BEGIN{FS=OFS=":"} set the field separator
  • $1 == "root1" check if Field 1 is equal to some value
  • { $2 = var1; $3 = var2 } set Field 2 and 3 values
  • 1 calls the default print command
  • file > tmp && mv tmp file helps you "shrink" the "replace-inplace-like" code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.