Re: Get and substitute filenames of filepathes that contain umlauts/special chars

"Thierry Blanc [email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
yes, your interpretation is correct. I would suggest to export the
commands to a sed script, one command per line. One command per line is
more clear when you have a long list of replacements.

There was a discussion on the y command recently.  Not sure if there are
drawbacks so I would stick to s command

On 18/11/15 12:54, [email protected] [sed-users] wrote:
> Thierry,
> thank you much. This code works.
> sed --text -r "h ;s/.*\\// ;s/ü/ue/g ;s/\.-\./-/ ;x ;G ;s/\n/ /" 
> "InFile" > "OutFile"
>
> Just for my understanding (although being at risk of needling you), is 
> this explaination right:
> 1. Line 1: copy line to hold space (unchanged-remember)
> 2. Line 1: delete all up to the last backslash
> 3. Line 1: do some more replacements
> 4. exchange the pattern space with the hold space
> 5: append the pattern space to the hold space (adding a newline \n)
> 6: replace the newline with something (a space) and print
> 7: repeat this for the next line(s)
> ?
>
> Hold buffer: Is "H" and "h" the same?
>
> [email protected],
> your input is also much appreciated.
> I simply removed the "y" command but must confess that your code 
> sometimes gave some unexpected results.
> But anyway, thanks.
>
> Zharif
>
> Am 17.11.2015 um 11:01 schrieb Thierry Blanc [email protected] 
> [sed-users]:
>> sed -r 'h;s|.*\\||;s|ü|ue|g;s|\.-\.|-|;x;G;s|\n||'
>>
>>
>>
>>
>>
>>
>> On 17/11/15 00:05,[email protected]  [sed-users] wrote:
>>> Thanks to Thierry and gnfalex@gmail for your replies.
>>>
>>> Thierry,
>>> sed --text -r "p;s|.*\\||;s|ü|ue|g;s|\.-\.|-|" "infile" > "outfile"
>>>
>>> Result:
>>> Line1: D:\dir1\subdir1\2006_A programme.-.a randomized controlled trial.pdf
>>> Line2: 2006_A programme-a randomized controlled trial.pdf
>>> Line3: D:\dir1\subdir1\2011_Schürer-Low Back Pain.pdf2011_Schuerer-Low
>>> Back Pain.pdf
>>>
>>> Only the third line gives the desired result.
>>> The first filepath is splitted into two lines.
>>>
>>> [email protected]:
>>> Your approach fits to the original request of the first mail I sent -
>>> thanks much.
>>>
>>> "Main fragment":
>>> ...does exactly what I want. It appends the filename only into the same
>>> line (without any substitution of the filename).
>>> Result:
>>> Line1: ren "D:\dir1\subdir1\2006_A programme.-.a randomized controlled
>>> trial.pdf" "2006_A programme.-.a randomized controlled trial.pdf"
>>> Line2: ren "D:\dir1\subdir1\2011_Schürer-Low Back Pain.pdf"
>>> "2011_Schürer-Low Back Pain.pdf"
>>>
>>> Full command:
>>> sed --text -r -e
>>> "h;s/.*\\//;s/[ÄÜÖ]/\0e/g;s/ß/ss/g;y/ÄÜÖäüö&\d183/AOUaou+-/;s/([-+_.\x20]){2,}/\1/g;s/\x20*.?([-+_.]).?\x20*/\1/g;H;x;s/^/ren
>>> \x22/;s/$/\x22/;s/\n/\x22\x20\x22/" "infile" > "outfile"
>>> ...results in an error message:
>>> sed: -e expression #1, char 58: strings for y command are different lengths
>>>
>>> Removing the "\d183" on the LHS and the "-" on the RHS of the s-command
>>> results in the following output file:
>>> Line1: ren "D:\dir1\subdir1\2006_A programme.-.a randomized controlled
>>> trial.pdf" "200_programm.randomized controlled tria.df"
>>> Line2: ren "D:\dir1\subdir1\2011_Schürer-Low Back Pain.pdf"
>>> "201_chore-ow Back Pai.df"
>>>
>>> Any further help is much appreciated
>>> Zharif
>>>
>>>
>>> Am 16.11.2015 um 12:01 [email protected]  [sed-users]:
>>>> Because no one replied I assume that my request was not clear.
>>>> Maybe I used wrong phrasing or yahoo ate some of my code?
>>>>
>>>> So let me try to re-compose my request with a simple example.
>>>> I do have an inputfile containing file pathes of files inside a directory.
>>>> inputfile example:
>>>> D:\dir1\subdir1\2006_A programme.-.a randomized controlled trial.pdf
>>>> D:\dir1\subdir1\2011_Schürer-Low Back Pain.pdf
>>>> ...
>>>> ...
>>>> I want sed to print the full filepath+filename+extension,
>>>> then to do some substitutions to the FILENAMES ONLY and
>>>> append these substituted filename into the same line (separated by a
>>>> space).
>>>> Substitutions in this example:
>>>> - replace ".-." with "-"
>>>> - replace umlaut "ü" or "Ü" with "ue" or "Ue"
>>>>
>>>> Desired output should be like this:
>>>> D:\dir1\subdir1\2006_A programme.-.a randomized controlled trial.pdf
>>>> 2006_A programme-a randomized controlled trial.pdf
>>>> D:\dir1\subdir1\2011_Schürer-Low Back Pain.pdf 2011_Schuerer-Low Back
>>>> Pain.pdf"
>>>> ...
>>>> ...
>>>>
>>>> Is this somehow possible via oneliner or maybe by a sed script?
>>>>
>>>> Again, thanks for any reply
>>>> Zharif
>>>>
>>>>
>>>>
>>>> Am 08.11.2015 um 16:46 [email protected]  [sed-users]:
>>>>> Env: German Win8.1x64 with sed v407 from UnxUtils.
>>>>>
>>>>> I want to queries full filepathes and finds any filename (+extension)
>>>>> containing umlauts and some special characters. Goal is to do some
>>>>> substitutions for the filenames only and to redirect results into a new
>>>>> file.
>>>>> This is what I have so far (working):
>>>>>
>>>>> Command to get a list of filenames that contain umlauts and some special
>>>>> chars (piping the DIR command):
>>>>> DIR /B /S /A:-D "E:\"|sed --text -n
>>>>> "s/^\(.*\\\)\(.*[äüößÄÜÖß&\d183]\+[^\\]\+\)$/\1\2/p" >> OutFile1
>>>>>
>>>>> The resulting file should:
>>>>> 1. contain a renaming command (REN+space) at start of each line in OutFile1.
>>>>> 2. contain the filename of the filepath only at end of each line.
>>>>> Example: REN "e:\XYZ\XY\2011   - Schürer _Low Back Pain.pdf"
>>>>> "2011-Schuerer_Low Back Pain.pdf"
>>>>>
>>>>> This is my loop command in which substitutions are made to the original
>>>>> file. Output is redirected to a new file:
>>>>> FOR /F "tokens=1* delims=    " %%a IN (OutFile1) DO (
>>>>>         FOR /F "tokens=1* delims=    " %%A IN ('ECHO "%%a"^|sed --text
>>>>> "s/Ä/Ae/g;s/ä/ae/g;s/Ü/Ue/g;s/ü/ue/g;s/Ö/Oe/g;s/ö/oe/g;s/ß/ss/g;s/&/+/g;s/\d183/-/g;s/[[:space:]]*\([-+_.]\)\{1,\}[[:space:]]*/\1/g;s/\([-+_.]\)\{2,\}/\1/g;s/[[:space:]]\{2,\}/
>>>>> /g"') DO (ECHO REN "%%a" "%%~nxA" >> %2)
>>>>> )
>>>>> %%~nxA prints only the filename(n)+extension(x) of the full path(A).
>>>>>
>>>>> This code is increddible slow for long files (as always for loops).
>>>>> Questions:
>>>>> 1. I'm sure it must be possible to solve this task with sed only without
>>>>> using a FOR loop. I'm runnig out of ideas here.
>>>>> 2. What about the used sed commands? In need of being improved? Or are
>>>>> they even faulty?
>>>>>
>>>>> 3. Another one;  is sed able to count and print characters for each line
>>>>> of a file (similar to wc)?
>>>>>
>>>>> Thanks much in advance for any reply
>>>>> Zharif
>>>>>
>>>>>
>>>>>
>>>>> ------------------------------------
>>>>> Posted by:[email protected]
>>>>> ------------------------------------
>>>>>
>>>> ------------------------------------
>>>> Posted by:[email protected]
>>>> ------------------------------------
>>>>
>>> ------------------------------------
>>> Posted by:[email protected]
>>> ------------------------------------
>>>
>> ------------------------------------
>> Posted by: Thierry Blanc<[email protected]>
>> ------------------------------------
>>
>
>
> ------------------------------------
> Posted by: [email protected]
> ------------------------------------
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.