sed command to append word at end of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed command to append word at end of line
# 1  
Old 09-05-2018
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.

Code:
[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

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



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-05-2018 at 01:04 PM.. Reason: Added and changed CODE tags.
# 2  
Old 09-05-2018
Use this:-

Code:
[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)


Last edited by RudiC; 09-05-2018 at 01:05 PM..
# 3  
Old 09-05-2018
Hello Mannu2525,

thanks for providing command. Can we add nodev option in fstab file if its not there.
# 4  
Old 09-05-2018
Help me out - what is the $(NR) and $(NR+1) for?
# 5  
Old 09-05-2018
Quote:
Originally Posted by ghpradeep
I am looking for sed command or script which will append word at end of line.
That is relatively easy: the dollar sign "$" represents the line end in regular expressions, hence:

Code:
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:

Code:
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 [icode]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:

Code:
# 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

Last edited by bakunin; 09-05-2018 at 04:59 PM..
# 6  
Old 09-06-2018
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:
Code:
/dev/mapper/rootvg-homelv /home           ext4    noatime,defaults        0       2

require output:
Code:
/dev/mapper/rootvg-homelv /home           ext4    noatime,defaults,nodev        0       2

# 7  
Old 09-06-2018
Why the mount command, then? How about
Code:
awk '/\/data/ && $4 !~ /nodev/ {$4 = $4 ",nodev"} 1' OFS="\t" /etc/fstab

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to append word count at end of file?

Hi guys, I have to append the word count of a file at the end of the file name like this - > "filename_2345" where 2345 is the word count of "filename". How do i achieve this using one command ? I am not allowed to store the word count in a variable and then concatenate. Request your... (1 Reply)
Discussion started by: peter2312
1 Replies

2. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

3. Shell Programming and Scripting

SED and Solaris Append line to the end of File does not work

Hello, I have to add a new line at the end of a File on Solaris-System: I think my script should be right, because I evaluated it to other threads. However the script does not what I am expected it should do. My file might look like this: Line1 Line2 Line3 And my script could... (7 Replies)
Discussion started by: Timo_HR
7 Replies

4. Shell Programming and Scripting

How to append something to a word using sed command

Hi, How to append something to already existing word. Suppose, I have the following line as a part of a file. VVV= jdbc:... (6 Replies)
Discussion started by: Dpu
6 Replies

5. Shell Programming and Scripting

Append the text file with comma at the end of every word

Hi folks, Using shell, I am trying the append comma to every line of text. the requirement is like, I have to open the txt file in unix and read line by line and should add comma at the end of every word to make it single line txt file ------- abc@gmail.com bcd@gmail.com... (7 Replies)
Discussion started by: giridhar276
7 Replies

6. Shell Programming and Scripting

append "awk command" to the end of each line

hi; this is my qqq.mos: l ./gcsw 86.0.0.1 'lt all;l+;lset SectorPort=860 Tilt 861;l-' l ./gcsw 86.0.0.2 'lt all;l+;lset SectorPort=862 Tilt 863;l-' l ./gcsw 86.0.0.3 'lt all;l+;lset SectorPort=864 Tilt 865;l-' ... i want to append;l nawk 'NR==14 && $NF!="Set."{print "l ./gcsw "r"... (4 Replies)
Discussion started by: gc_sw
4 Replies

7. Shell Programming and Scripting

Using sed command to change end of line

I am looking to change a data file into a javascript string and this is the code that I am using: sed -i '' -e 's/^/str += "/' -e 's/$/";/' file.xml The first part -e 's/^/str += "/' works as intended, but the second part -e 's/$/";/' adds an additional newline to my file, so that instead of... (3 Replies)
Discussion started by: figaro
3 Replies

8. Shell Programming and Scripting

Sed : identify a pattern and append a word at the end of a line

Hello to all, On aix, I want to identify a term on a line in a file and then add a word at the end of the line identified. I do not want the word to be added when the line contains the symbol "#". I use the following command, but it deletes the term identified then adds the word. #sed... (4 Replies)
Discussion started by: dantares
4 Replies

9. Shell Programming and Scripting

sed append "\n" to end of every line in file

I know it sounds simple, but I want to e-mail the last 6 lines of a log file, which I have tailed into logresults.txt. I'm using echo -e "Subject:server results\nFrom:server log <user@domain.com>\n"`cat logresults.txt` | sendmail -t user@domain.com which works, but the body of the e-mail has... (4 Replies)
Discussion started by: unclecameron
4 Replies

10. UNIX for Dummies Questions & Answers

using sed to append text to the end of each line

Anyone know how to use SED to append a comma to the end of each line example: field1,field2,field3,field4 If i Cat /textfile ---- How can i append the end of /textfile with a comman? (8 Replies)
Discussion started by: Redg
8 Replies
Login or Register to Ask a Question