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
SUBTLER(1)																SUBTLER(1)

NAME
subtler - a commandline interface for subtle SYNOPSIS
subtler [GENERIC|MODIFIER] GROUP ACTION [ARG1] [ARG2] DESCRIPTION
subtler is a commandline tool, that allows interacting with a EWMH/NetWM compatible window manager and especially with subtle(1) from a shell. The arguments of subtler are quite complex and allow a variety of spellings and different number of arguments. Most of the commands require at least one argument and return output of following format: FORMAT
INPUT
o DISPLAY :<display number> o ID <number> o GEOMETRY <x>x<y>+<width>+<height> o NAME <string|number> o DATA <string|number> o PATTERN Matching works either via plaintext, regex(7), id or window id if applicable. If a pattern matches more than once ALL matches are used. If the PATTERN is - subtler will read from stdin. OUTPUT
o Client listing <window id> <visibility> <view id> <geometry> <gravity> <flags> <instance name> (<class name>) o Gravity listing <gravity id> <geometry> o Screen listing <screen id> <geometry> o Tag listing <tag name> o Tray listing <window id> <instance name> (<class name>) o View listing <window id> <visibility> <view id> <view name> FIELDS
o <window id> Numeric (hex) id (e.g. 0xa00009) o <visibility> - = not visible, * = visible o <view id> Numeric id (e.g. 5) o <geometry> x x y + width + height o <flags> - = not set, + = fullscreen, ^ = float, * = stick, ~ = resize, = = zaphod, ! = fixed o <instance name> Window instance/resource name o <class name> Window class name o <gravity id> Numeric id of gravity (e.g. 2) o <screen id> Numeric id of a screen (e.g. 1) o <tag name> Name of a tag (e.g. terms) OPTIONS
GENERIC
o -d, --display=DISPLAY Connect to DISPLAY (default: :0) o -h, --help Show this help and exit o -V, --version Show version info and exit o -p, --proc This creates a ruby proc from the given argument and yields the result of the group to it as parameter param. Examples: subtler -cl -p "puts param.name" subtler -cC -p "param.gravity = { terms: :left }" MODIFIER
o -r, --reload Reload subtle o -R, --restart Restart subtle o -q, --quit Quit subtle o -C, --current Select current active window/view instead of passing it via argument. Example: subtler -cCf o -X, --select Select a window via pointer instead of passing it via argument. Example: subtler -cXf GROUPS
o -c, --Client Use client group o -g, --Gravity Use gravity group o -e, --Screen Use screen group o -s, --Sublet Use sublet group o -t, --Tag Use tag group o -y, --Tray Use tray group o -v, --View Use views group ACTIONS FOR CLIENTS
o -f, --find => PATTERN Find client Example: subtler -cf urxvt o -o, --focus => PATTERN Set focus to client Example: subtler -co urxvt o -F, --full => PATTERN Toggle full Example: subtler -cF urxvt o -O, --float => PATTERN Toggle float Example: subtler -cO urxvt o -S, --stick => PATTERN Toggle stick Example: subtler -cS urxvt o -N, --urgent => PATTERN Toggle urgent Example: subtler -cN urxvt o -l, --list List all clients o -T, --tag => PATTERN Add tag to client Example: subtler -cT urxvt tag o -U, --untag => PATTERN NAME Remove tag from client Example: subtler -cU urxvt tag o -G, --tags => PATTERN Show client tags Example: subtler -cG urxvt o -Y, --gravity => PATTERN PATTERN Set client gravity Example: subtler -cY urxvt gravity o -E, --raise => PATTERN Raise client window Example: subtler -cE urxvt o -L, --lower => PATTERN Lower client window Example: subtler -cL urxvt o -k, --kill => PATTERN Kill client Example: subtler -ck urxvt ACTIONS FOR GRAVITIES
o -a, --add => NAME GEOMETRY Create new gravity Example: subtler -ga test 0x0+100+100 o -l, --list List all gravities Example: subtler -gl o -f, --find => PATTERN Find a gravity Example: subtler -gf center o -k, --kill => PATTERN Kill gravity Example: subtler -gk center ACTIONS FOR SCREENS
o -l, --list List all screens Example: subtler -el o -f, --find => ID Find a screen Example: subtler -ef 0 ACTIONS FOR SUBLETS
o -l, --list List all sublets Example: subtler -sl o -f, --find => PATTERN Find sublet Example: subtler -sf sublet o -u, --update Updates value of sublet Example: subtler -su o -D, --data => PATTERN DATA Send data to sublet Example: subtler -sD sublet something o -k, --kill => PATTERN Kill sublet Example: subtler -sk PATTERN ACTIONS FOR TAGS
o -a, --add => NAME Create new tag Example: subtler -ta tag o -f, --find => PATTERN Find all clients/views by tag Example: subtler -ta tag o -l, --list List all tags Example: subtler -tl o -I, --clients Show clients with tag Example: subtler -tI o -k, --kill => PATTERN Kill tag Example: subtler -tk PATTERN ACTIONS FOR TRAYS
o -f, --find => PATTERN Find a tray Example: subtler -yf PATTERN o -l, --list List all trays Example: subtler -yl o -k, --kill => PATTERN Kill tray Example: subtler -yk PATTERN ACTIONS FOR VIEWS
o -a, --add => NAME Create new view Example: subtler -va NAME o -f, --find => PATTERN Find a view Example: subtler -vf PATTERN o -l, --list List all views Example: subtler -vl o -T, --tag => PATTERN NAME Add tag to view Example: subtler -vT terms tag o -U, --untag => PATTERN NAME Remove tag from view Example: subtler -vT terms tag o -G, --tags Show view tags Example: subtler -vG terms o -I, --clients Show clients on view Example: subtler -vI terms o -k, --kill => PATTERN Kill view Example: subtler -vk terms BUGS
Report bugs at http://subforge.org/projects/subtle/issues Homepage: http://subtle.subforge.org COPYRIGHT
Copyright (c) Christoph Kappel <unexist@subforge.org> SEE ALSO
subtle(1), subtlext(1), sur(1), surserver(1) November 2011 SUBTLER(1)
All times are GMT -4. The time now is 12:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy