Sponsored Content
Top Forums Shell Programming and Scripting Portable and efficient way to add text after pattern Post 303019158 by disedorgue on Sunday 24th of June 2018 09:23:17 AM
Old 06-24-2018
Hi,

Maybe something like as :
Code:
awk -vArgz="$ArgZ" -vArgZB="$ArgZB" 'BEGIN{split(Argz,A,"\n")}
FNR == 1 {Line=0}
FNR == 1 && $0 ~ /^#!/ {Line=FNR}
$0 == A[2] || $0 == A[3]{next}
/^import/ {Line=FNR}
{B[FNR]=$0}
END{B[Line]=B[Line] Argz ArgZB;for (ll in B){print B[ll]}}' file

With your file example:
Code:
#!/usr/bin/env python2.7

import time
import re
import os.path
import os
import sys
MySpecialpath = os.path.abspath(sys.argv[0])
#
#REGEN
#REGEN
#REGEN

from bisect import bisect_left

This awk reads the file once and only once.


Regards.
This User Gave Thanks to disedorgue For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: add text if pattern not found

Hello, I would like to add the line TIMEZONE="CET" if the pattern TIMEZONE is not found between the range <JOB and JOB> : Example: Src file: <!DOCTYPE DEFTABLE SYSTEM "deftable.dtd"> <DEFTABLE > <JOB TASKTYPE="Job" TIMEFROM="0030" TIMEZONE="CET" </JOB> <JOB... (5 Replies)
Discussion started by: mutunzi
5 Replies

2. Shell Programming and Scripting

process text between pattern and print other text

Hi All, The file has the following. =========start of file=== This is a file containing employee info START name john id 123 date 12/1/09 END START name sam id 4234 date 12/1/08 resigned END (9 Replies)
Discussion started by: vlinet
9 Replies

3. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

4. Homework & Coursework Questions

Efficient Text File Writing

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a template main.c file via shell script to make it easier for yourself later. The issue here isn't writing... (2 Replies)
Discussion started by: george3isme
2 Replies

5. Shell Programming and Scripting

Efficient population of array from text file

Hi, I am trying to populate an array with data from a text file. I have a working method using awk but it is too slow and inefficent. See below. The text file has 70,000 lines. As awk is a line editor it reads each line of the file until it gets to the required line and then processes it.... (3 Replies)
Discussion started by: carlr
3 Replies

6. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

7. Shell Programming and Scripting

Most efficient method to extract values from text files

I have a list of files defined in a single file , one on each line.(No.of files may wary each time) eg. content of ETL_LOOKUP.dat /data/project/randomname /data/project/ramname /data/project/raname /data/project/radomname /data/project/raame /data/project/andomname size of these... (5 Replies)
Discussion started by: h0x0r21
5 Replies

8. Shell Programming and Scripting

Efficient way to search array in text file by awk

I have one array SPLNO with approx 10k numbers.Now i want to search the subscriber number from MDN.TXT file (containing approx 1.5 lac record)from the array.if subscriber number found in array it will perform below operation.my issue is that it's taking more time because for one number it's search... (6 Replies)
Discussion started by: siramitsharma
6 Replies

9. Shell Programming and Scripting

Efficient awk way to add numbers in line fields

data.now: blah1,dah,blaha,sweet,games.log,5297484456,nagios-toin,529748456,on__host=93 SERVICE__ALERT_=51 Warning___The__results__of__service=16 Warning___on__host=92 Auto_save__of__retention__data__completed=1 Warning___Return=68 PASSIVE__SERVICE__CHECK_=53 ,1026--1313,1... (12 Replies)
Discussion started by: SkySmart
12 Replies

10. Shell Programming and Scripting

awk to add text to matching pattern in field

In the awk I am trying to add :p.=? to the end of each $9 that matches the pattern NM_. The below executes andis close but I can not seem to figure out why the :p.=? repeats in the split as in the green in the current output. I have added comments as well. Thank you :). file ... (4 Replies)
Discussion started by: cmccabe
4 Replies
HG-FAST-EXPORT(1)						   User Commands						 HG-FAST-EXPORT(1)

NAME
hg-fast-export - import hg repository into git SYNOPSIS
hg-fast-export [--quiet] [-r <repo>] [--force] [-m <max>] [-s] [-A <file>] [-M <name>] [-o <name>] DESCRIPTION
Import hg repository <repo> up to either tip or <max> If <repo> is omitted, use last hg repository as obtained from state file, GIT_DIR/hg2git-state by default. Note: The argument order matters. OPTIONS
-m Maximum revision to import --quiet Passed to git-fast-import(1) -s Enable parsing Signed-off-by lines -A Read author map from file (Same as in git-svnimport(1) and git-cvsimport(1)) -r Mercurial repository to import -M Set the default branch name (default to 'master') -o Use <name> as branch namespace to track upstream (eg 'origin') --force Ignore validation errors when converting, and pass --force to git-fast-import(1) hg-fast-export February 2011 HG-FAST-EXPORT(1)
All times are GMT -4. The time now is 06:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy