Add a # on particular pattern in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add a # on particular pattern in file
# 1  
Old 10-16-2018
Add a # on particular pattern in file

Hi all

I am looking a way to add a # symbol from a defined pattern to next blank line or carriage return.

for example I have a file in this format:


Code:
[G-MI01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456456
thr_act=70
thr_act_w=80
thr_load=60
thr_load_sgw=60
mforce=OK


[P-VE01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456896
thr_act=30
thr_act_w=180
thr_load=60
thr_load_sgw=90
mforce=OK

[P-BA01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456896
thr_act=30
thr_act_w=180
thr_load=60
thr_load_sgw=90
mforce=OK


I whould comment from a specific pattern as [G-MI01SSR] to next carriage return:



Code:
[G-MI01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456456
thr_act=70
thr_act_w=80
thr_load=60
thr_load_sgw=60
mforce=OK


#[P-VE01SSR]
#ping_ip=xxx.xxx.xxx.xxx
#counter_ip=xxx.xxx.xxx.xxx
#thr_up=456896
#thr_act=30
#thr_act_w=180
#thr_load=60
#thr_load_sgw=90
#mforce=OK

[P-BA01SSR]
ping_ip=xxx.xxx.xxx.xxx
counter_ip=xxx.xxx.xxx.xxx
thr_up=456896
thr_act=30
thr_act_w=180
thr_load=60
thr_load_sgw=90
mforce=OK

is there a way to do this job?

thanks!




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

Last edited by RudiC; 10-16-2018 at 06:19 AM.. Reason: Added CODE tags.
# 2  
Old 10-16-2018
Quote:
Originally Posted by rattoeur
. . . is there a way to do this job? . . .
Almost certainly . . . if you could unambiguously specify the job to be done. What you say verbatim doesn't match your sample data, and there's usually noy carriage return in *nix text files.


Would this paraphrase of your request express your needs:


"in a file of empty line separated records, comment out one record's lines AFTER the record identified by [G-MI01SSR] patter"?
# 3  
Old 10-16-2018
Code:
awk '/\[G-MI01SSR\]/ {a++}; a && ! NF {c=1} ; c && NF {b="#"} ; !NF && b {b=_; c=0} {print b $0}; ' infile

# 4  
Old 10-16-2018
This looks like a config file (or ini file) that can be read by python's configparser module. Here's an attempt to comment out a section using python 3 interpreter.


Code:
import configparser

file = 'config.txt'

config = configparser.ConfigParser()
config.read(file)

section = 'P-VE01SSR'

for (k, v) in config[section].items():
    config.remove_option(section, k)
    config.set(section, '#' + k, v)

with open(file, 'w') as configfile:
    config.write(configfile)

# 5  
Old 10-16-2018
Code:
sed '/^.P-VE01SSR.$/,/^$/ s/./#&/' file

sed -i ... writes back to the file (if your sed supports it).
# 6  
Old 10-16-2018
How about

Code:
awk '
L                       {sub  (/^/, "#")
                         gsub (/\n/, "&#")
                         L = 0
                        }
/^\[G-MI01SSR\]/        {L = 1}
1
' RS= ORS="\n\n" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Big pattern file matching within another pattern file in awk or shell

Hi I need to do a patten match between files . I am new to shell scripting and have come up with this so far. It take 50 seconds to process files of 2mb size . I need to tune this code as file size will be around 50mb and need to save time. Main issue is that I need to search the pattern from... (2 Replies)
Discussion started by: nitin_daharwal
2 Replies

2. Shell Programming and Scripting

Identify file pattern, take count of pattern, then act

Guys - Need your ideas on a section of code to finish something up. To make a long story short, I'm parsing a print output file that goes to pre-printed forms. I'm intercepting it, parsing it, formatting it, cutting it up into individual pages, grabbing the text I want in zones, building an... (3 Replies)
Discussion started by: ampsys
3 Replies

3. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

4. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

5. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

How to search pattern and add that pattern in next line

Hi All, I am new to shell scripting and need help in scripting using CSH. Here is what I am trying to so, 1. Search a specific string e.g. "task" from "task (input1, out1)". 2. Extract the arguements "input1" and "out1" 3. Add them in separate lines below. eg. "int input1" , " integer out1" ... (7 Replies)
Discussion started by: deshiashish
7 Replies

8. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

9. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

10. Shell Programming and Scripting

Search file for pattern and grab some lines before pattern

I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in) This is on solaris Can you help? (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question