Script using awk to find and replace a line, how to ignore comment lines
Hello,
I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to find the line in the src file that needs to be replaced and then print the modified file.
This example sets the size of a block of shared memory
There are commented out lines in the src that could match the regex, /\#define SHMEMSIZE \(4096 /, so the first step compares the first two characters of $0 to see if the line is commented out. If so, the line is ignored (printed). If the line is not commented out, there is a check to see if the line contains the regex (the text in red above). Lines that do not match the regex are printed. When the regex is found, the substitute line is printed. Finally, the original file is overwritten by the modified temp file.
This works, but there are some issues.
The first issue is that I believe this will only work if the comment is the first two characters on the line. This doesn't look like it will work if the comment is indented, which is common in c and c++. This was originally written for old FORTRAN code where the comment was always a "C" in the first column of the punch card. Is there a way to ignore leading whitespace and check if "//" is the first two non-whitespace characters?
Second, I was not able to pass in the regex line, meaning the line I was looking for. When I tried something like,
I got an error for unmatched parenthesis and I wasn't able to figure out how to escape it. This means that the awk code has to be hard coded for each find and replace and can't be used as a function, etc.
This also doesn't account for multi-line comments. That isn't an issue in this case, but it would be nice to know how to address that. I guess I would look for the start /* and save lines in an array until the */ and then print the array.
Is there a way to tell awk to ignore the first 11 lines of a file?? example, I have a csv file with all the heading information in the first lines. I want to split the file into 5-6 different files but I want to retain the the first 11 lines of the file.
As it is now I run this command:
... (8 Replies)
I want to replace this line : "test compare visible] true" and make it "#test compare visible] true".
How can I do it ? And it should be checked in many sub folder files also. (6 Replies)
Hello,
I have a file as follow
a
b
c
c
d
d
e
I would like to write a awk command to insert # from the first occurence of
"c" to the end of the files.
OUTPUT should be like this
a
b
#c (5 Replies)
My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block
rem Subset Rows (&&tempName.*)
CREATE VIEW &&tempName.* AS
SELECT *
FROM &&tempName.*
WHERE f is not null
and replace with following code
rem Subset Rows... (9 Replies)
I am parsing a nagios config, searching for a string, and then printing the line 2 lines later (the "members" string). Here's the data:
define hostgroup{
hostgroup_name chat-dev
alias chat-dev
members thisisahostname
}
define hostgroup{
... (1 Reply)
hello everyone
my file contains many records, the following is a sample:
BEGIN
ASX1500000050002010120000000308450201012000177
ASX1100002000000201012000000038450201012000220
ASX1600100005000201012000000038450020101200177
ASX1900100006000201067000000058450020101200177... (2 Replies)
Find in first column and replace the line with Awk, and output new file
File1.txt"2011-11-02","Georgia","Atlanta","x","",""
"2011-11-03","California","Los Angeles","x","",""
"2011-11-04","Georgia","Atlanta","x","x","x"
"2011-11-05","Georgia","Atlanta","x","x","" ... (4 Replies)
My first comment on every file contains the license message. I want to replace with a new license message. I used the below sed script, which replaces all comments. What is the modification or any other method with awk script for the below to edit only the first comment(license message)?
#sed -f... (1 Reply)
I want to find common line in two files and replace the next line of first file with the next line of second file. (sed,awk,perl,bash any solution is welcomed ) Case Ignored. Multiple Occurrence of same line.
File 1:
hgacdavd
sndm,ACNMSDC
msgid "Rome"
msgstr ""
kgcksdcgfkdsb... (4 Replies)
How do you write a script to ignore all lines except the --- dash lines and then remove --- dashes from the data in a text file?
Also how do you separate data in a text file with a tab (for example, column1 (software) and column2 (date) ) ?
Here is my scripts : I am getting errors in... (3 Replies)