Sponsored Content
Top Forums Shell Programming and Scripting Grep and sed (replace string in patterned lines) Post 302830655 by unknown7 on Tuesday 9th of July 2013 09:15:37 AM
Old 07-09-2013
Thanks ++

Sometimes you just can wonder why you didn't find it :-)

I now remember that it was possible to add a pattern in front of sed substitution.
I don't want to substitute again, if i was obviously done in steps before. So it should be like

1.) Find PATTERN
2.) Only if element is "to be changed" but NOT ", YEHA! to be changed"
2.1.) than substitute "to be chaned" to ", YEHA! to bechanged"
2.2.) else (do nothing)

Thanks a lot!

final outcome:
Code:
sed '/PATTERN/s/ to be changed/, YEHA! to be changed/' test > tmp_file && mv tmp_file test

---------- Post updated at 03:15 PM ---------- Previous update was at 02:58 PM ----------

can I add a rule checking if substitution was done?

Example:
Input:
blah PATTERN 987654321 blub to be changed

Output after first run or otherwise manipulated: FINE
blah PATTERN 987654321 blub , YEHA! to be changed

Now if I rerun or if this was changed from other sources: SHOULD NOT BE SUBSTITUTED
blah PATTERN 987654321 blub , YEHA! , YEHA! to be changed

Note:
", YEHA! " can be between PATTERN and "to be changed" and rule should substitute if it's not directly in front of "to be changed"

Quote:
IN:
PATTERN 12345... , YEHA ...678 to be changed
PATTERN 12345... , YEHA ...678 , YEHA to be changed

OUT:
PATTERN 12345... , YEHA ...678 , YEHA to be changed
PATTERN 12345... , YEHA ...678 , YEHA to be changed

Last edited by unknown7; 07-09-2013 at 10:26 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Like grep -v for a string over 2 lines? Sed?

Hi, I have a log file that I need to monitor as it's being written to, and I want to exclude certain strings from the output. At the moment I'm using ... tail -f LogFileName_`date +%d`.log | egrep -v "First String To Exclude | 2nd string | 3rd string" ...which works OK - but now I need to... (1 Reply)
Discussion started by: jake657
1 Replies

2. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

3. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

4. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

5. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

6. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

8. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

9. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

10. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies
LSDIFF(1)							     Man pages								 LSDIFF(1)

NAME
lsdiff - show which files are modified by a patch SYNOPSIS
lsdiff [[-n] | [--line-number]] [[-p n] | [--strip-match=n]] [--strip=n] [--addprefix=PREFIX] [[-s] | [--status]] [[-E] | [--empty-files-as-removed]] [[-i PATTERN] | [--include=PATTERN]] [[-x PATTERN] | [--exclude=PATTERN]] [[-z] | [--decompress]] [[-# RANGE] | [--hunks=RANGE]] [--lines=RANGE] [--files=RANGE] [[-H] | [--with-filename]] [[-h] | [--no-filename]] [[-v] | [--verbose]...] [file...] lsdiff {[--help] | [--version] | [--filter ...] | [--grep ...]} DESCRIPTION
List the files modified by a patch. You can use both unified and context format diffs with this program. OPTIONS
-n, --line-number Display the line number that each patch begins at. If verbose output is requested (using -nv), each hunk of each patch is listed as well. For each file that is modified, a line is generated containing the line number of the beginning of the patch, followed by a Tab character, followed by the name of the file that is modified. If -v is given once, following each of these lines will be one line for each hunk, consisting of a Tab character, the line number that the hunk begins at, another Tab character, the string "Hunk #", and the hunk number (starting at 1). If the -v is given twice in conjunction with -n (i.e. -nvv), the format is slightly different: hunk-level descriptive text is shown after each hunk number, and the --number-files option is enabled. --number-files File numbers are listed, beginning at 1, before each filename. -# RANGE, --hunks=RANGE Only list hunks within the specified RANGE. Hunks are numbered from 1, and the range is a comma-separated list of numbers or "first-last" spans; either the first or the last in the span may be omitted to indicate no limit in that direction. --lines=RANGE Only list hunks that contain lines from the original file that lie within the specified RANGE. Lines are numbered from 1, and the range is a comma-separated list of numbers or "first-last" spans; either the first or the last in the span may be omitted to indicate no limit in that direction. --files=RANGE Only list files indicated by the specified RANGE. Files are numbered from 1 in the order they appear in the patch input, and the range is a comma-separated list of numbers or "first-last" spans; either the first or the last in the span may be omitted to indicate no limit in that direction. -p n, --strip-match=n When matching, ignore the first n components of the pathname. --strip=n Remove the first n components of the pathname before displaying it. --addprefix=PREFIX Prefix the pathname with PREFIX before displaying it. -s, --status Show file additions, modifications and removals. A file addition is indicated by a "+", a removal by a "-", and a modification by a "!". -E, --empty-files-as-removed Treat empty files as absent for the purpose of displaying file additions, modifications and removals. -i PATTERN, --include=PATTERN Include only files matching PATTERN. -x PATTERN, --exclude=PATTERN Exclude files matching PATTERN. -z, --decompress Decompress files with extensions .gz and .bz2. -H, --with-filename Print the name of the patch file containing each patch. -h, --no-filename Suppress the name of the patch file containing each patch. -v, --verbose Verbose output. --help Display a short usage message. --version Display the version number of lsdiff. --filter Behave like filterdiff(1) instead. --grep Behave like grepdiff(1) instead. SEE ALSO
filterdiff(1), grepdiff(1) EXAMPLES
To sort the order of touched files in a patch, you can use: lsdiff patch | sort -u | xargs -rn1 filterdiff patch -i To show only added files in a patch: lsdiff -s patch | grep '^+' | cut -c2- | xargs -rn1 filterdiff patch -i To show the headers of all file hunks: lsdiff -n patch | (while read n file do sed -ne "$n,$(($n+1))p" patch done) AUTHOR
Tim Waugh <twaugh@redhat.com> Package maintainer patchutils 23 Jan 2009 LSDIFF(1)
All times are GMT -4. The time now is 02:21 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy