LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2016, 02:35 PM   #1
nscripta
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Rep: Reputation: Disabled
Linux string replacement where input value has forward slash, unable to work out!!


Trying to solve this is using way more time than it should!!

I have an input string: <td>10.11.12.13</td> that I want to convert to \folder1\folder2\folder3\**** REMOTE 10-11-12-13 ****.txt. The output string is then created as a file (touch)

I have tried breaking it down to smaller conversion steps, but the best I can get is \folder1\folder2\folder3\**** 10-11-12-13.txt

I have attempted to use both sed and bash built-in string manipulation with no complete success.

The folder names are static, the ip address will be variable.

Code used so far:

Code:
firststr="**** REMOTE "
secondstr=" ****.txt"
myfolder="/folder1/folder2/folder3/"

ipaddress="<td>10.11.12.13</td>"
ipaddress="${ipaddress//[!0-9.]/}"
echo $ipaddress
ipaddress="${ipaddress//./-}"
ipaddress="$myfolder/$firststr$ipaddress$secondstr"
echo $ipaddress
touch $ipaddress
The problem is obviously associated with the words I need in the final file name and the correct use of escape characters. Originally when using the sed and bash string replacements I couldn't get the </td> to convert with the operating system displaying a directory listing after the </td>. Searching on-line I found the allow only numbers code which I expanded to include the period which allowed me to make some progress.

Hopefully others can see the errors in my code and point me in the right direction!!
 
Old 02-26-2016, 02:45 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
The way it's written, you would need to ensure that the path /folder1/folder2/folder3/ exists, though you may not want your files hanging off of /, a better way may be to have the folders created elsewhere, and set myfolder to folder1/folder2/folder3/ instead.

You'll need to quote $ipaddress on your touch line since there are spaces in the file name (I prefer _ to spaces), also you might want to reconsider using "*" in a file name.

Code:
folder1/folder2/folder3 $ rm \*\*\*\*\ REMOTE\ 10-11-12-13\ \*\*\*\*.txt
 
1 members found this post helpful.
Old 02-26-2016, 02:55 PM   #3
nscripta
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by thesnow View Post
The way it's written, you would need to ensure that the path /folder1/folder2/folder3/ exists, though you may not want your files hanging off of /, a better way may be to have the folders created elsewhere, and set myfolder to folder1/folder2/folder3/ instead.

You'll need to quote $ipaddress on your touch line since there are spaces in the file name (I prefer _ to spaces), also you might want to reconsider using "*" in a file name.

Code:
folder1/folder2/folder3 $ rm \*\*\*\*\ REMOTE\ 10-11-12-13\ \*\*\*\*.txt
The folder structure already exists so there's no issue there.
The reason for **** in the file name is so that it displays at the top of a list alphabetically.
 
Old 02-26-2016, 02:59 PM   #4
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
Other options exist, if that's the hang up

Code:
user@host [-] ~/linuxquestions/folder1/folder2/folder3 $ ls -al
total 32
drwxrwxr-x 2 user user 4096 Feb 26 14:57 .
drwxrwxr-x 3 user user 4096 Feb 26 14:40 ..
-rw-rw-r-- 1 user user    0 Feb 26 14:57 000file
-rw-rw-r-- 1 user user    0 Feb 26 14:56 ___file
-rw-rw-r-- 1 user user    0 Feb 26 14:56 ***file
 
1 members found this post helpful.
Old 02-27-2016, 02:13 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
My question is, why do you use quotes everywhere EXCEPT for the final echo and touch??

On a personal side, I would wonder why you change the dots to dashes? It is more recognisable as an IP if left as dots.

As above, I would use underscore if the file is needed at the top. Using a wildcard is just asking for trouble down the track.

Last edited by grail; 02-27-2016 at 02:15 AM.
 
Old 02-27-2016, 03:41 PM   #6
nscripta
LQ Newbie
 
Registered: Feb 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
I have taken onboard the comments regarding the use of the wildcard characters and have changed the leading characters to underscores. I have corrected the code to add the quotes for the touch statement and everything is working as required. Thanks for everyone's input in resolving my issue.
 
  


Reply



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
How to remove forward slash and everything after with sed or awk? CaptainDerp Linux - General 4 03-25-2013 01:15 AM
Small problem with bash and forward slash Karsh Linux - Newbie 5 08-13-2010 07:55 PM
Forward slash (/) in filenames. Oxagast Linux - Software 10 07-21-2009 09:04 PM
question mark and forward slash osc~ Slackware 2 08-16-2007 12:32 PM
forward slash being treated as escape character in shell? debiant Linux - General 1 07-19-2006 04:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:55 AM.

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
Open Source Consulting | Domain Registration