Sponsored Content
Top Forums Shell Programming and Scripting Script using awk to find and replace a line, how to ignore comment lines Post 303039483 by MadeInGermany on Monday 7th of October 2019 05:05:42 PM
Old 10-07-2019
The RE searches require some quoting effort.
The index() function searches for plain strings and returns the position.
Also only the comments left from the search string matter.
Two reasons to go for the index() function rather than the RE.
Code:
# replace multiplier of SHMEMSIZE in sizedefs.h with $num_pages*pagesize
# generated value to substitute for current line
numpages_find_line="#define SHMEMSIZE (4096 "
numpages_replacement_line="#define SHMEMSIZE (4096 * $num_pages)"
# value of comment character for current src file, C code in this case
comment_ch1='//'
comment_ch2='/*'
# path to file we are working on
filename='./src/sizedefs.h'
# process file to switch out line to replace
awk -v find="$numpages_find_line" \
    -v replace="$numpages_replacement_line" \
    -v comment1="$comment_ch1" \
    -v comment2="$comment_ch2" \
    '
    {
      comm1pos=index($0,comment1)
      comm2pos=index($0,comment2)
      commpos=((comm1pos > 0 && comm1pos < comm2pos || comm2pos == 0 ) ? comm1pos : comm2pos)
# commpos := the leftmost comment
      findpos=index($0,find)
# unchanged if commpos is left from findpos or not found
      if (commpos > 0 && commpos < findpos || findpos == 0)
        print $0;
      else
        print replace;
    }
    ' $filename > tmp
mv tmp $filename

These 2 Users Gave Thanks to MadeInGermany For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk, ignore first x number of lines.

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)
Discussion started by: trey85stang
8 Replies

2. UNIX for Dummies Questions & Answers

how to replace a text of line with a comment line

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)
Discussion started by: manoj.b
6 Replies

3. Shell Programming and Scripting

using awk to comment out lines to the end of file

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)
Discussion started by: phamp008
5 Replies

4. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

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)
Discussion started by: Zaheer.mic
9 Replies

5. Shell Programming and Scripting

awk find a string, print the line 2 lines below it

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)
Discussion started by: mglenney
1 Replies

6. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

7. Shell Programming and Scripting

Find in first column and replace the line with Awk, and output new file

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)
Discussion started by: charles33
4 Replies

8. Shell Programming and Scripting

sed/awk script to replace only FIRST comment in the file

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)
Discussion started by: vpshastry
1 Replies

9. Shell Programming and Scripting

Shell Script to find common lines and replace next line

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)
Discussion started by: madira
4 Replies

10. UNIX for Dummies Questions & Answers

Ignore all lines except the --- dash line in a text file.

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)
Discussion started by: dellanicholson
3 Replies
LOOKBIB(1)						      General Commands Manual							LOOKBIB(1)

NAME
lookbib - search bibliographic databases SYNOPSIS
lookbib [ -v ] [ -istring ] [ -tn ] filename... It is possible to have whitespace between a command line option and its parameter. DESCRIPTION
lookbib prints a prompt on the standard error (unless the standard input is not a terminal), reads from the standard input a line contain- ing a set of keywords, searches the bibliographic databases filename... for references containing those keywords, prints any references found on the standard output, and repeats this process until the end of input. For each database filename to be searched, if an index filename.i created by indxbib(1) exists, then it will be searched instead; each index can cover multiple databases. OPTIONS
-v Print the version number. -istring When searching files for which no index exists, ignore the contents of fields whose names are in string. -tn Only require the first n characters of keys to be given. Initially n is 6. FILES
filename.i Index files. SEE ALSO
refer(1), lkbib(1), indxbib(1) Groff Version 1.18.1 27 June 2001 LOOKBIB(1)
All times are GMT -4. The time now is 03:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy