sed command to append word at end of line

hello Team,

I am looking for sed command or script which will append word at end of line. for example. I want to validate particular filesystem with mount |<filesystem name> command. if nodev parameter is not there then it should add in the fstab file with receptive to the filesystem.

[root@test CIS]# mount |grep /data
/dev/mapper/datavg-lv_data on /data type ext4 (rw,relatime,seclabel,data=ordered)
[root@test CIS]#

i am looking for below output after adding nodev in fstab file

[root@test CIS]# mount |grep /data
/dev/mapper/datavg-lv_data on /data type ext4 (rw,relatime,seclabel,data=ordered,nodev)
 [root@test CIS]#

Use this:-

[test ~]$ mount |grep /data | awk -F '=|)' '{if(!match($0, /nodev/)){print $(NR)"="$(NR+1)",nodev)"} else {print $0}}'
/dev/sdb on /data type ext4 (rw,relatime,data=ordered,nodev)

Hello Mannu2525,

thanks for providing command. Can we add nodev option in fstab file if its not there.

Help me out - what is the $(NR) and $(NR+1) for?

That is relatively easy: the dollar sign "$" represents the line end in regular expressions, hence:

sed 's/$/ nodev/' /some/file

will append nodev to every line at the end - it substitutes the line end with the text. Since you want only certain lines to be modified you can add a regular expression in front to only affect certain lines:

sed '/nodev/ ! s/$/ nodev/' /some/file

will append nodev only to lines which don't contain the string already. You probably want to refine this a bit more but the principle should be obvious by now.

PS: checking a filesystems current status with the mount/icode]command and then modify the mount table accordingly is a dangerous proposition: it could already be changed alredy but not been remounted so far. Whenever you change such a thing always base the decision to apply the change on the thing you are modifying, not something else - in this case on the content of the file /etc/fstab .

Also notice that you said you want to change the end of line. You haven't told us your OS but if it is Linux the /etc/fstab has two columns after where you should place your change, here iis the one from my laptop, for example:

# cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/mapper/rootvg-rootlv /               ext4    noatime,errors=remount-ro 0       1
# /boot was on /dev/sda1 during installation
UUID=xxxxxxxxxx /boot           ext4    defaults        0       2
/dev/mapper/rootvg-homelv /home           ext4    noatime,defaults        0       2
/dev/mapper/rootvg-optlv /opt            ext4    noatime,defaults        0       2
/dev/mapper/rootvg-swaplv none            swap    sw              0       0

I hope this helps.

bakunin

hello,

basically i want search particular filesystem(eg. /data) is having nodev parameter set or not using mount command.
mount |grep /data

if /data does not have nodev parameter then it should add into /etc/fstab file. if its already there in mount command output then it will ignore it.

In the fstab file, its should be add in fourth column with existing option.

original output:

/dev/mapper/rootvg-homelv /home           ext4    noatime,defaults        0       2

require output:

/dev/mapper/rootvg-homelv /home           ext4    noatime,defaults,nodev        0       2

Why the mount command, then? How about

awk '/\/data/ && $4 !~ /nodev/ {$4 = $4 ",nodev"} 1' OFS="\t" /etc/fstab

Hello,
Because, In mount command output, it shows default option which are set on particular filesystem. which is not showing in /etc/fstab file. it only show defaults word there.

[root@test CIS]# mount |grep /data
/dev/mapper/datavg-lv_data on /data type ext4 (rw,relatime,seclabel,data=ordered)
[root@test CIS]#

I just tried you command. its working fine but i need specific to file system. i have data and data1 mount point. it should apply only /data only.

/dev/mapper/datavg-lv_data      /data   ext4    default2,nodev  0       0
/dev/mapper/datavg-lv_data1     /data1  ext4    default2,nodev  0       0

your command adding nodev on both filesystem.

Any idea on how to cope with that little problem?