Hashing parts of file using sed - unix

I am creating a script to hash some parts of the text. The point is, I would like to do it using sed. Here is how example string looks like:
..HASHSTART.......HASHSTART..HASHEND..HASHEND..HASHSTART.....
.....HASHEND....HASHSTART.........
I have three problems with it:
I would like to match "HASHSTART" and "HASHEND" properly. So for example if i will find first HASHSTART, then i would like to hash everything to first find of HASHEND, even if there is more HASHSTARTs between.
I would like to hash it even if there is a new line between HASHSTART and HASHEND.
If there is HASHSTART at the end and no HASHEND till end of the file, I would like to hash everything after.
By "hash" I mean here to change from . to #.
So with this input the output should be:
..HASHSTART##################HASHEND..HASHEND..HASHSTART#####
#####HASHEND....HASHSTART#########
Thank you very much for any help.

Like this?
awk -F 'HASHSTART' -vw=3 '{i=index($w,"HASHEND");if(i)print substr($w,1,i-1)}' file
-vw=3 postion (2,)

Related

Programmatically modifying a specific line in a text file

Help with what I suspect to be pretty simple would be much appreciated.
I want to be able to programmatically change a certain line in a configuration file. Specifically, the file looks like
...Other lines...
# The name of the database to mount
dbms.active_database=test.db
...Other lines...
I want to be able to replace "test.db" with "production.db", etc. Other programs may add or delete lines, so I can't count on a consistent line number, but there is no other line containing "dbms.active_database="
How might I best do this?
Thank you.
With sed:
sed 's/\(^dbms\.active_database=\)test\.db/\1production.db/' file
awk -v name0fNewDb="HEythere.db" 'BEGIN{FS=OFS="="}/dbms.active_database/{$2=name0fNewDb}1'
...Other lines...
# The name of the database to mount
dbms.active_database=HEythere.db
...Other lines...
If I understood problem statement right, this should help you in doing the changes only on the line which consist of "dbms.active_database". Where it will replace the right part of = . name0fNewDb is a variable to which you can assign any text of your requirement.

Regex to find a particular word

I have an HTML file and I want to find a particular URL inside it.
Starting with http://abcd.com/Admin/CoreFolder/Resources/
Has a JPG file. Ex - http://abcd.com/Admin/CoreFolder/Resources/ff8127aa-bf81-4833-ba12-edf2f366ccbb.jpg
Can you help me out with the regex for the same. I want to find all occurrences and replace the abcd.com with xyz.com.
http.*?(abcd\.com)[^\s]*?\.jpg
DEMO : http://regexr.com?3853l
Then you can replace the first capture ( the part within braces ) with xyz.com
This is a RegEx that will match any .jpg link starting with http://abcd.com/Admin/CoreFolder/Resources/
(http://abcd.com/Admin/CoreFolder/Resources/(.+?).jpg)
Example test on RegExr
Using something like sed to do the search and replace (you didn't actually give enough information about your environment - like "what language do you want to implement your regex in"?):
sed 's/\(http:\/\/\)\(abcd\.com\\)\(/Admin\/CoreFolder\/Resources\/.*\.jpg\)/\1xyz\.com\2/' myFile.html > newFile.html

Improperly formatted CSV, how to repair?

I have a csv, and each line reads as follows:
"http://www.videourl.com/video,video title,video duration,thumbnail,<iframe src=""http://embed.videourl.com/video"" frameborder=0 width=510 height=400 scrolling=no> </iframe>,tag 1,tag 2",,,,,,,,,,,,,,,,,,,,,,,,,,
Is there a program I can use to clean this up? I'm trying to import it to wordpress and map it to current fields, but it isn't functioning properly. Any suggestions?
Just use search and replace in this case. remove the commas at the end and then replace the remaining commas with ",".
Should anyone else have the same issue. Know that this solution will only work with data much like the example giving. If data has a lot of text and there are commas within the text that need kept. Then search replacing comma will not work. Using regex would be the next option and that can be done in Notepad ++
However I think the regex pattern depends on the data so not much point creating an example.
PHP could be used to explode each line also. Remove values that match a regex out of many i.e. URL, money. Then what is left could be (depending on the data again) just a block of text. That approach may not work if there are two or more columns with a lot of text

Copy the highlighted pattern in gvim

Lets say, I highlighted (matched) text present in brackets using
/(.*)
Now, how to copy the highlighted text only (i.e matching pattern, not entire line) into a buffer, so that I paste it some where.
Multiple approaches are presented in this Vim Tips Wiki page. The simplest approach is the following custom command:
function! CopyMatches(reg)
let hits = []
%s//\=len(add(hits, submatch(0))) ? submatch(0) : ''/ge
let reg = empty(a:reg) ? '+' : a:reg
execute 'let #'.reg.' = join(hits, "\n") . "\n"'
endfunction
command! -register CopyMatches call CopyMatches(<q-reg>)
When you search, you can use the e flag to motion to the end of the match. So if I understand your question correctly, if you searched using eg.:
/bar
And you wish to copy it, use:
y//e
This will yank using the previous search pattern until the end of the match.
Do you want to combine every (foo) in the buffer in one register (which would look like (foo)(bar)(baz)…) or do you want to yank a single (foo) that you matched?
The last is done with ya( if you want the parenthesis or yi( if you only want what's between.
Ingo's answer takes care of the former.

How to grep value from a line in Unix

Suppose I have a lines as follows:
<Instance name="cd" id="sa1">
<work id="23" permission="r">
I want to get the id value printed, where the id field is not constant.
It hard to give a hint without doing it for you. But assuming your real needs are more involved than you describe, then perhaps some learning can happen while applying this answer.
Grep isn't really powerful enough to do the job you describe, although it may be useful in a pipline to select data at a larger "grain". If your file has one-tag-per-line like your example shows, you can use grep to filter just the Instance or work tags.
grep Instance | program to extract id val
or
grep work| program to extract id val
To extract the value you need something more powerful than grep. Assuming the value is enclosed in double-quotes and contains no embedded quotes; and that there are no similarly named attributes that could confuse the expression, this sed magic should do the trick.
sed 's/.*id="\([^"]*\)".*/\1/'
If any one the above asumptions are not true, the expression will have to be more complicated.

Resources