Need to modify contents of file with complex patterns.


 
Thread Tools Search this Thread
Top Forums Programming Need to modify contents of file with complex patterns.
# 1  
Old 07-05-2010
Need to modify contents of file with complex patterns.

hi,
my fstab file content is like this along with some other lines:

Code:
/dev/vg0/var1  /var1  ext3  defaults  0  2
/dev/vg0/flx1  /flx1  ext3  defaults  0  2
/dev/vg0/var  /var  ext3  defaults  0  1
/dev/vg0/flx  /flx  ext3  defaults  0  2

I want to remove lines with /dev/vg0/var and /dev/vg0/flx ,

and modify the lines with
Code:
 "/dev/vg0/var1  /var1  ext3  defaults  0  2"

to
Code:
"/dev/vg0/var1  /var  ext3  defaults  0  2"

and

Code:
"/dev/vg0/flx1  /flx1  ext3  defaults  0  2"

to
Code:
"/dev/vg0/flx1  /flx  ext3  defaults  0  2"

I tried some options with sed but didn't work. Please help me in solving this in PERL.

Last edited by Scott; 07-05-2010 at 08:52 AM.. Reason: Code tags, please...
# 2  
Old 07-05-2010
Code:
awk 'NR<3 {$2=substr($2,1,4); print}' fstab > fstab.new

# 3  
Old 07-05-2010
Code:
# sed 's: /var1: /var:;s: /flx1: /flx:'  infile
/dev/vg0/var1 /var ext3 defaults 0 2
/dev/vg0/flx1 /flx ext3 defaults 0 2
/dev/vg0/var /var ext3 defaults 0 1
/dev/vg0/flx /flx ext3 defaults 0 2

This User Gave Thanks to ygemici For This Post:
# 4  
Old 07-05-2010
Need to modify contents of file with complex patterns

Quote:
Originally Posted by ygemici
Code:
# sed 's: /var1: /var:;s: /flx1: /flx:'  infile
/dev/vg0/var1 /var ext3 defaults 0 2
/dev/vg0/flx1 /flx ext3 defaults 0 2
/dev/vg0/var /var ext3 defaults 0 1
/dev/vg0/flx /flx ext3 defaults 0 2

Hi ,
Thanks for the solution, it modifies the lines with /var1 to /var and /flx1 to /flx respectively, But how to delete the lines with /dev/vg0/var and /dev/vg0/flx. when i use "grep -v /dev/vg0/var " for deletion bothe the lines with /dev/vg0/var1 and /dev/vg0/var are getting deleted. even sed for deletion is causing the same problem.

Please suggest a way for deleting only the lines with /dev/vg0/var and /dev/vg0/flx.
# 5  
Old 07-05-2010
Code:
# sed '/vg0\/var /d;/vg0\/flx /d' myfstab
/dev/vg0/var1  /var1  ext3  defaults  0  2
/dev/vg0/flx1  /flx1  ext3  defaults  0  2

you can use grep to add -w (list only this pattern matched)

Code:
# grep -Ewv "vg0/flx|vg0/var" myfstab
/dev/vg0/var1  /var1  ext3  defaults  0  2
/dev/vg0/flx1  /flx1  ext3  defaults  0  2



regards
ygemici
# 6  
Old 07-05-2010
success,
may I ask did you try the awk thing Smilie
Let me know what's wrong with it.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Grab contents between two patterns

Hi, What is the best approach to grab contents between Changes Dependencies from the following example snippy Changes in packages about to be updated: bash-3.2-32.el5_9.1.x86_64 * Thu Jun 27 22:00:00 2013 Roman Rakus <rrakus@redhat.com> - 3.2-32.1 - Fixed a bug that caused... (2 Replies)
Discussion started by: ashokvpp
2 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Grab contents between two matched patterns

I am wanting to fetch the content of the table within a file the table begins with data label like N Batch Mn(I) RMSdev I/rms Rmerge Number Nrej Cm%poss AnoCmp MaxRes CMlplc SmRmerge SmMaxRes $$ $$ . #columns of data . . . . . $$ I tried the command awk... (18 Replies)
Discussion started by: piynik
18 Replies

5. UNIX for Dummies Questions & Answers

How to modify lines without certain patterns?

I have file like this: rs111 A T 0.9 0.8 ... rs112 AT T 0.8 0.2 ... rs113 G CT 0.9 0.1... I wish to replace all the NONE "A" or "T" or "G" or "C" values in column 3 and column 4 to " ", how can I do this? thanks! (8 Replies)
Discussion started by: luoruicd
8 Replies

6. UNIX for Advanced & Expert Users

How do I modify file contents?

I have a table output in a text file (abc.txt)as follows col1 , col2 , col3 , col4 , Here I want to modify this file as follows col1 '~!#', col2 '~!#', col3 '~!#', col3 '^@\n' Could you help on this??? Thanks Kris (1 Reply)
Discussion started by: mkris
1 Replies

7. Shell Programming and Scripting

sed: How to modify files in a complex way

Hello, I am new to sed and hope that someone can help me with the following task. I need to modify a txt file which has format like this: xy=CreateDB|head.queue|head.source|head.definition|rtf.edit|rtf.task|rft.cut abc|source|divine|line4|5|true into something like: head.queue=abc... (19 Replies)
Discussion started by: pinkypunky
19 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

How to modify the contents of file using script

Hi, Can anyone pls let me know how can i modify the file contents thru script. Eg. I have file abc.dat that contains below lines Merge.resync.cycleFlag Merge.resync.logFlag Merge.resync.maxByteRate Merge.resync.maxSearch Merge.resync.rate Merge.resync.tickLog ... (2 Replies)
Discussion started by: sdosanjh
2 Replies
Login or Register to Ask a Question