Programming :: Sed One-liner: Search Text In File And Replace It - If Not Present Insert It?

Oct 16, 2010

As indicated in the subject, I want to search a text. If the text is present I want to replace it. But if the text is not present, I want to insert it after first line and before last line.

Searched text is:CleanCache "*";

Where * can be anything.

Example: CleanCache "false"; -> CleanCache "true";

If CleanCache "false"; is not present, only insert CleanCache "true"; after first line and before last line.

View 8 Replies


ADVERTISEMENT

Programming :: Write A Utility Which Will Scan In A Text File And Search And Replace Strings?

Jul 16, 2010

Something very handy to do in a Linux shell, is manipulating files and strings - essentially parsing data. Write a utility which will scan in a text file and search and replace strings. We also want to keep track of how many strings we've replaced.

I know that my command would look like this: <utility name> <filename> <stringToSearchFor> <stringToReplaceWith>
Code: #!/bin/bash

[code]....

View 2 Replies View Related

Ubuntu :: Text Editor Column Editor And Regex Search/replace, Incrementing Number Insert?

Jul 25, 2011

On windows I really only used Notepad++ as my text editor, it had two features that I loved.What I need to accomplish is what I would do with Notepad++ column editor.I could have like 100 lines, and place the cursor at a column, and goto edit>column editor, and I could insert an incrementing number. (I could also pad the incrementing number with 0s, this was GREAT for making batch files among other things.)So each line at that column had a number higher than the previous line.The other feature that I used sometimes was a search/replace with regex patterns.Does anyone know of an editor that has those features for linux? I am mostly after the column editor insert feature but if you know of one with both features that would rock.

View 1 Replies View Related

Programming :: Search A File For A Particular Pattern And If Pattern Found Replace The Line With New Text?

Feb 24, 2010

I want to search a file for a particular pattern and if pattern found replace the line with new text. i am using awk 'match($0,"pattern") != 0 {print $0} ' filename to check if the pattern exists.how do i get the line number of the pattern and delete that line and replace the line with my new text?

View 1 Replies View Related

General :: Shell Script To Search One File For Contents Of Another And Replace Text?

Feb 3, 2011

Suppose I have a pair of files containing lists. The first file is called contigs.txt and it contains a list that looks like this:

Code:
Contig822
Contig826

[code]...

View 5 Replies View Related

Programming :: Sed To Read The File And Replace And Insert The Pattern?

Jun 11, 2009

I want to read from the file and check for the pattern, if the line has some word like <string>: then string should be copied into buffer. Afterwards, I want to insert the same <string> with some word in the next line of the file. use sed command to perform the above mentioned operations?

View 2 Replies View Related

Programming :: Search And Replace In A Binary File?

Aug 11, 2010

i need to change a binary file, let's say to find and replace username:

find string: "/home/name/bla-bla-bla/ "
new string: "/home/anewname/bla-bla-bla/ "

i can do it, for example, in emacs (hexl-mode), but interesting in writing a script instead. it will be much more better for me if i could do it automatically. is there an analog of: sed 's/string1/string2/g' ? P.S. the best way is to recompile the binary files i have, but there are no sources available.

View 5 Replies View Related

Programming :: Multi File Recursive Folder Search And Replace?

Aug 3, 2010

I'm wanting to mod some PHP files across a hierarchy and thought I'd drive it with find + grep + xargs

I built up a command line which I was confident would do the job, but now can't save the results.

First I tried this:

Code:
find . -name *.php | xargs grep serialize | cut -d: -f1| sort -u | xargs sed -i s/serialize/serialise/g
but that didn't work:
Code:
sed: illegal option -- i
so I thought I'd try using
Code:

[Code].....

View 10 Replies View Related

Programming :: C++ Character Search In Text File?

Jun 23, 2011

Im trying to read a file in c++ and search for particular character for example if this is a list that I have:

Alice
Bob
David

[code]....

if the input is D, it should give David, if its B, gives bob. so in this case, meaning it reads the first character of every line. but if possible I want to make this dynamic so the user can specify which character position he is looking for, so in case he is looking for R as character index 3 in all lines, it should give Charlie. but the problem is, it does now recognize , besides, I do not know how to specify the character position in each line.

here is my code

Code:

#include <iostream>
#include <fstream>
#include <cstring>

[code]....

View 1 Replies View Related

Programming :: String Search In Text File Using Visual C++

Jan 25, 2010

im tryin to make a tool in visual C++ which will take an input string through a text box,then it will compare tht string with a text file containing data and display the matched results in list box.

View 2 Replies View Related

Programming :: Sed Script - Searching For Text Within File / Replace With Variable

Feb 25, 2011

Code:
#!/bin/bash
VARR=`cat /proc/asound/cards | grep HDMI | cut -c 1-2`
VARX="defaults.ctl.card $VARR"
VARY="defaults.pcm.card $VARR"
FILE1="alsa"
FILE2="alsa.new"
echo $VARX
echo $VARY
sed "s/defaults.ctl.card*/'$VARX'/g" $FILE1 > $FILE2

This is what I have right now. Well, I thought I knew sed, and apparently I don't... I tried writing this for someone else, and this has given me trouble, so since the user pretty much figured it out on his own, here it goes.
Say VARR=1, so VARX and VARY contain the above text, appended by 1.
What I am trying to do is replace the text "defaults.ctl.card 0" by VARX and "defaults.pcm.card 0" by VARY. The contents of FILE1 is the file being used to search for both text fields, and FILE2 is the output file. I tried using single quotes, double quotes, and a mixture of both, and no go whatsoever. So my question... What is the proper way of searching for text within a file and replacing with a variable?

View 5 Replies View Related

Debian Programming :: Replace Certain Text In A File With Person Username Automatically?

Mar 16, 2014

Is there any way to use sed to replace certain text in a file with the persons username automatically? Right now i'm using
Code: Select allsed -i.bak s/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g file.foo

I would like it to automatically inject the persons usrname in the replacement string. Is this possible? I've been looking on line at various sed tutorials and I cant quite find what i'm looking for. I also didn't really see anything in the forums search function.Essentially i'm trying to take this file URL...Android.rules and replace all instances of username with the persons actual username automatically.

Code: Select allsed -i.bak s/username/$USER/g 51_Android.rules

View 0 Replies View Related

Programming :: Text File Replace Occurrences Of Three Character String ZZZ With Quotation Mark

Mar 30, 2010

I want to use SED to do the following: In a text file replace any occurrences of the three character string ZZZ with a quotation mark "and. replace all occurrences of a comma with a semi-colon. It is the S/ / / command which is stumping me on the first issue...inparticular how to get the replace string to be quote.

View 9 Replies View Related

Programming :: Sed To Search And Insert Column?

Feb 24, 2011

I have the following file.

B0 49.2230869 -102.5743868
C 1LPRK 654.90
C 1PREU 3693.90
C 1JRSU 3693.90

[code]....

I need to insert text ABCDEF starting at position 26 if a line starts with C. I need it to look like:

C 1VKNG ABCDEF 3097.00
C 1JLFU ABCDEF 3157.20
C 1MNVL ABCDEF 3346.10

I would prefer to use sed to do this and not perl.

View 14 Replies View Related

Programming :: Using Sed To Search And Replace Backwards?

Oct 25, 2010

I'm trying to use sed to search and replace backwards. The problem is that I have a shell script that is required to put commas into big numbers. For example

9999999 as 9,999,999

I've tried a few things, but none seem to work:

Code:

$ echo 9999999 | sed -e 's/([0-9]{3})/,1/g'
,999,9999
$ echo 9999999 | sed -e 's/([0-9]{3})$/1,/g' -e 's/([0-9]{3})/1,/g'
999,999,9,
$ echo 9999999 | sed -e 's/([0-9]{3})$/1,/g' -e 's/([0-9]{3})/,1/g'

[ode]....

It would be much easier if I could search backwards! For example Bash parameter substitution style:

Code:
$ echo 9999999 | sed -e 's%([0-9]{3})%,1%g'

View 14 Replies View Related

Programming :: Awk Multiple Line Search And Replace?

May 1, 2011

I am trying to search and replace a multi line pattern in a php file using awk.The pattern starts with

<div id="navbar">
and ends with
</div>

[code]...

View 3 Replies View Related

Programming :: Insert Text Before The First Pattern Found?

Apr 8, 2011

i need to add this line nameserver 208.67.122.221 from ISP before the first nameserver already exists in resolv.conf in all workstations i know

Code:

sed '/nameserver/ i
ameserver 208.67.122.221'

but that insert it on every line after nameserver not only before the first one

Code:

# blah blah
# blah blah
domain mydom.com

[code]....

View 3 Replies View Related

Programming :: Insert Text Message In Textarea Using Php?

Dec 13, 2010

i trying to add text messge in textarea and i got error in connection and i don't know if it correct syntax. and i wonder when i was install xamp server it no password required. but i dont know what password that i input in my php connection.?

PHP Code:

<?php
$con = mysql_connection("localhost","root","123");
if(!$con)
{

[code]....

PHP Code:

<textarea rows="3" cols="30" name="txtcomment" wrap="physical">Leave a Comment

View 4 Replies View Related

Programming :: Bash Search Line And Replace Hex To Binary?

Jul 7, 2011

I need a command to search a string in a file and then to convert the next string in the same line from hexadecimal to binary. I was able to put everything in capitals. The original file can be as such:

E 2
C 1 794
T ffff
E 2
C 1 787

It is not always FFFF! I am trying to do this in a file at once, not reading line by line (using while).

View 5 Replies View Related

Programming :: Insert Line Of Text Prior To Pattern Only Once?

Jun 29, 2010

I have a file like so:

Code:

one
two
three
four

I would like to insert prior to the word "three" all items from this second file with the following contents:

Code:

four
three
two
one

Now my issue is, and I have been using both sed and awk currently, that after the second line of the new file is read there will of course now be 2 copies of the word "three" but I would like to only insert the final 2 words, ie "two" and "one" prior to only the first occurrence of the word "three" so final file will look like:

Code:

one
two
four

[code]....

So here there is now only one of each word from the second file joined to make the new file. For simple code I have tried something like the following:

Code:

while read line
do
awk -v n=$line '!f && /three/{print n;f++}1' file1 > tmp_file
mv tmp_file file1
done < file2

Now this works but seems very clumsy to me. There is obviously a better sed and / or awk out there.

View 5 Replies View Related

General :: Insert Text In The Last Line Of A File With Sed Command?

Oct 4, 2010

I want to insert the text "DB dept" in the last line of passwd file which looks like this right now

Quote:

newuser:x:717:717::/home/sacharje:/bin/bash

And I want it to be like this:

Quote:

newuser:x:717:717: DB dept:/home/sacharje:/bin/bash

I tried to do that with sed but failed, I'd like to know the proper syntex with sed to achieve this.

View 7 Replies View Related

General :: Insert Text To A File Using Command Line?

Jan 28, 2010

I need to be able to edit a file from the commend line as I intend to script this operation, the file is called menu.lst The original is as below

Code:

titleUbuntu 8.04.3 LTS, kernel 2.6.24-24-generic
root(hd0,0)
kernel/boot/vmlinuz-2.6.24-24-generic root=UUID=b09feb23-5524-4ec4-862f-94700b968f64 ro quiet splash

[code]....

View 4 Replies View Related

General :: Replace Text In One File With Text From Another File Using Sed?

Apr 28, 2010

How could I replace text in one file with text from another file using sed?

The text in each of the files would be surrounded by a starting and ending delimiter or a starting tag, say like /* */ so to easily find them.

View 8 Replies View Related

Programming :: Using .sh Script To Replace Text?

Feb 24, 2011

i'm trying to script a lil piece to execute in terminal so when i type something like:

scriptname textfile

the script searches the text file, replaces charecters of my choosing with charecters of my choosing

"sed" im told is the way to go, and i got summat like this going

Code:

#!/bin.sh
bash -c "sed -i.backup -e /s/char/newchar/"

(if need be, i think i can just add another line of "-e /s/char/newchar/" if i need to target more charecters or words as needed.) the issue with the above code. . . how do i get it to target whatever text file follows the command? without having to manually designate sed to it each time? e.g. in commanding:

scriptname textfile

how do i get the script to target textfile^^^?

View 4 Replies View Related

Programming :: Exact Text Find And Replace?

Aug 13, 2010

I have a sysctl.conf that has the following in it:

kernel.exec-shield-randomize = 1
kernel.exec-shield = 1

When I grep kernel.exec-shield I get both line, hence I keep over writing the kernel.exec-shield-randomize in my script because it finds them both for my sed commend.

How can I get an exact match with either sed/awk/grep in shell so I can do a find and replace?

Example: sed 's/^kernel.exec-shield =.*/kernel.exec-shield = 1/g' /etc/sysctl.conf will replace BOTH lines

Example: grep "^kernel.exec-shield" find both line and I want it to find only the exact line.

View 3 Replies View Related

Programming :: One-liner With && And || Didn't Work

Feb 9, 2011

I recently noticed some of my one-liners with && and || didn't work as I expected so I googled for "bash && || pitfalls" and found this page [URL] It's probably my fault, but I still don't understand why it wouldn't work as I want it..... I don't even have the code anymore that wasn't working properly because I rewrote it using if ; then ; command ; fi

View 4 Replies View Related

Programming :: Text Editor With Search And Capture

Dec 6, 2010

i was wondering if in all the editors/tools in linux, if there was a prog that can extract a portion of a text file, giving it a starting line of say o as the first character on a line of say o35565 oxxxxx then when it finds the next line down the file with the same thing oxxxxwhatever

it could take those lines and all the lines between them and save it to another file? i dont know if you call this parsing? or what.

View 14 Replies View Related

Programming :: Construct A Quick Regex That Will Search For Six Lines Of Text

Apr 25, 2011

I am trying to construct a quick regex that will search for six lines of text without a clear line break between them. It only needs to search, not replace, as I will be using in gEdit (with regex plugin) anyway.

It's for editing subtitle files. The video player I will be using them on can only cope with 3-line subtitles, so I just need to edit any in the srt file that contain four or more. There won't be many so I can do it manually. For example:

26
00:01:47,357 --> 00:01:49,359
a motivated business
professional with
clearly defined goals.

[Code].....

but .* seems to mean "any character, or none", so that doesn't work. My experience of regular expressions is limited, but I do know they are very powerful when used correctly!

View 10 Replies View Related

Ubuntu :: Replace All Tab Stops In A Text File With 4 Spaces?

May 18, 2011

How can I make vi and nano such that when I press the tab key, it automatically replaces the tab with 4 spaces?

View 5 Replies View Related

Ubuntu :: Script To Replace Characters In Text File?

Jun 30, 2011

I have an odd request, lets say I have a text file like this:

Quote:

Code:

__##___##___IIIIII
__##___##_____II__
__##___##_____II__
__#######_____II__
__##___##_____II__
__##___##_____II__
__##___##___IIIIII

I want a script that will replace all of the hash tabs with random characters.I found this command will generate a random character

Quote:

apg -a1 -n1 -m1 -x1 -d

how to script a search and replace using that random character upon each successive find until the end of file occurs.

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved