-1

I have a file called tmp.mount with the following contents

[Mount]
Options=mode=1777,strictatime,noexec,nosuid
  1. I'd like to search the file via the word Options= to get the line number.
  2. I will search the word nodev via the given line number.
  3. If it does not exist, I will insert the word ,nodev to the end of this line via given line number.

With the results

[Mount]
Options=mode=1777,strictatime,noexec,nodev,nosuid,nodev

All without line break. Most of the solution was to use sed but i'm clueless on how I could incorporate the line search with sed.

  • Why do you want to operate by line number, rather than filtering based on whether the contents match? And what prior approach were you trying to use that would have inserted a line break? – Charles Duffy Mar 28 '18 at 03:32

1 Answers1

1
awk '/Options=/ && ! /nodev/ {print $0 ",nodev"; next};1' file

no need to get the line number, just append the ",nodev" to the corresponding line

Redundant
  • 36
  • 4
  • @CharlesDuffy Thanks! – Redundant Mar 28 '18 at 03:34
  • @CharlesDuffy The content in the file is dynamic and Options= may end up on a different line. Is there any approach to handle this scenario? – Dickson Chu Mar 28 '18 at 05:40
  • @DicksonChu what do you mean by dynamic, can you raise more examples in the question? – ZNZNZ Mar 28 '18 at 06:46
  • @CharlesDuffy Lets say Options= is on another line in the file, will the command still work? And does awk update this newly appended string back into the given file? Or do I have to store the output from awk and make sure of sed to update the file. – Dickson Chu Mar 28 '18 at 08:24
  • @DicksonChu I need more info about your pattern, if `Options=` only happens at start of line you can make it `/^Options=/` is this okay? and awk will not change your original file you can either 1. save to tmp file and change name 2. use gawk 4.1.0+ `gawk -i inplace` btw, you are keep @ the wrong person – Redundant Mar 28 '18 at 08:41
  • @Redundant sorry about tagging the wrong name. So, I basically just want to look for Options= anywhere in the file, append if the nodev to the line if it is not there. After which I should save this changes and everything else in the file remains the same except the new added nodev. – Dickson Chu Mar 28 '18 at 08:51
  • @Dickson if `Options=` is meant to be everywhere, my code will work fine. awk will go through line by line, it will continue even if it already encounter one match. for changing the original file, you can refer to my previous comment – Redundant Mar 28 '18 at 08:58
  • @Redundant I managed to got it working but by using sed instead of gawk. Is there any reason that I should use gawk over sed? Or it is just a matter of preference. – Dickson Chu Mar 28 '18 at 09:46
  • @DicksonChu `sed -i` works the same way as `gawk -i inplace` the difference is internal, `sed` is using the build in option, but `gawk` is just including the library called `inplace`. all of them are nothing much different from `awk/sed > tmp && mv tmp original`. other differences are the functional difference of `sed` and `awk`, they outperformed each other on their own fields, and `gawk` is just extended implementation of `awk`. the comment here is limited, I can only answer briefly, for all their differences you can search more on `sed awk and gawk`. btw, thanks for accepting the answer – Redundant Mar 28 '18 at 09:56
  • in my use case, sed is mainly for pattern matching of the whole text, it's more like a vim with a bunch of commands in one run; awk is for records or table like data. I think awk can have more functionalities, thus I prefer awk, but sed may be much easier than awk in some use cases. – Redundant Mar 28 '18 at 10:07