LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-10-2018, 04:35 PM   #1
KimAke
LQ Newbie
 
Registered: Sep 2018
Posts: 2

Rep: Reputation: Disabled
I need a script to replace several strings in a text file with filenames


Hi, I really need some help with this since I'm not that familiar with scripting yet. I'm on Ubuntu 18.

I have been making multisamples of an analog synth with Synthclone, but occasionally something happens and it messes up the XML files where it stores it's internal data.

The XML looks like this:
Code:
<zone aftertouch="128" channel="1" channel-pressure="128" dry-sample-stale="false" note="0" release-time="0.1" sample-time="1.5" velocity="8" wet-sample-stale="true" dry-sample="eW3815">
			<controls/>
		</zone>
		<zone aftertouch="128" channel="1" channel-pressure="128" dry-sample-stale="false" note="0" release-time="0.1" sample-time="1.5" velocity="16" wet-sample-stale="true" dry-sample="CI3815">
			<controls/>
		</zone>
The dry sample filenames are wrong.
I have the correct files, and they are in correct order by time (seconds accuracy needed here).
So I need to run probably a find command and then follow that with a conditional for that looks for the right string and replaces that with the oldest file, then proceeds to the next filename, replaces that with the next filename etc.

So I always would need to target those 6 characters in the string dry-sample="******" and overwrite that with the next filename in the queue. The filenames are all the same length.

There are no other files in the directory except the samples and the XML.

This is just too complex for me right now, altough I kind of can see how it should be done theoretically. I'm not quite sure why Synthclone uses internally scrambled filenames and only writes proper filenames upon export, but that's what I have here...

Last edited by KimAke; 09-10-2018 at 04:37 PM.
 
Old 09-10-2018, 06:25 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,236
Blog Entries: 12

Rep: Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715Reputation: 2715
sed is one command line tool which can edit files, using search and replace.
Code:
sed s/<pattern>/<replace pattern>/g
Please see the bash blog in my signature for information about how to get started scripting.
 
1 members found this post helpful.
Old 09-10-2018, 09:15 PM   #3
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,052

Rep: Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773Reputation: 1773
Welcome to LQ!

The bash shell has some features (=~ binary operator and parameter expansion) that can help. Running the code below will change dry-sample="******" to dry-sample="replaced" on your example XML. You would need to extend this by reading the replacement string from an array of filenames to achieve what is wanted.
Code:
#!/bin/bash

while read aline; do
  [[ "$aline" =~ dry-sample=\"(.*)\" ]] && echo "${aline/${BASH_REMATCH[1]}/replaced}" || echo "$aline"
done < test.xml
 
2 members found this post helpful.
Old 09-10-2018, 09:19 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 17,261

Rep: Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640Reputation: 2640
Things are never as easy as they first seem. "find" doesn't respect chronological order - although you can fudge it by exec'ing ls. If you can guarantee no blanks (or newlines) in filenames you can simply run a loop over "ls -rt" but that will eventually come back and bite you when you least expect it, and is generally frowned upon.

That blog looks like a good start, although there is also "Advanced Bash Scripting Guide" at tldp.org; a few years old now, but still really useful. And of course the bash doco itself - "info bash".
 
1 members found this post helpful.
Old 09-18-2018, 12:44 PM   #5
KimAke
LQ Newbie
 
Registered: Sep 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi, thanks for the answers! I've been really busy and didn't have time to try this out yet, but I will see soon if I can coax something that works together based on the ideas here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Find/Replace shell script that replaces word with other word in text, filenames yanom Programming 8 09-12-2012 01:29 AM
Shell command/script to find duplicate strings in a text file damgar General 4 09-04-2012 02:17 PM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-11-2008 12:13 AM
Using tr to replace strings in a group of filenames Lemmings Linux - Newbie 7 05-21-2008 12:00 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 11:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration