I have a 13 number string, some whitespace, and then /mp3.
I need to join them. Everyline that I need this for begins with "cd" (without the quotes).
What it looks like now:
cd media/Audio/WAVE/9781933976334 /mp3
What I want my output to be:
cd media/Audio/WAVE/9781933976334/mp3
The 13 number string is always different but always begins with 978.
Please use sed unless there is something much better for this task.
How about this:
$ echo 'cd media/Audio/WAVE/9781933976334 /mp3' | sed 's=\([0-9][0-9]*\)\( *\)\(/mp3\)=\1\3='
cd media/Audio/WAVE/9781933976334/mp3
$
I ended up using a while loop
grep -A2 -B2 ^cd '/home/gregg/Desktop/mp3-to-m4b-batch1'|while read line ; do echo ${line}\/mp3;done >>mp3-to-m4b-batch2
---------- Post updated at 10:48 AM ---------- Previous update was at 10:48 AM ----------
Thanks Perderabo, I am going to study your code and learn from it.
---------- Post updated at 11:20 AM ---------- Previous update was at 10:48 AM ----------
Perdabo, can you explain your code please? that is the s=\ ? Is that specifying a delimiter of a backslash?
kshji
4
There is so many solution, here is some if you like to use loops. If input include exactly 3 values then
cat somefile | while read cmd path mp xxx
do
echo "$cmd $path$mp"
done > newfile
And if like to use awk, then
cat somefile | awk '{print $1, $2$3}' > newfile
another sed solution..
echo "cd media/Audio/WAVE/9781933976334 /mp3"|sed 's/ \+//2'
And how about:
echo 'cd media/Audio/WAVE/9781933976334 /mp3' | sed 's! */mp3!/mp3!'
Franklin, please explain your use of !
Scott
8
It serves the same purpose as / would.
i.e.
sed "s/something/something_else/" ...
But if your regular expression, or replacement contain / then it's easier to use something else.
i.e.
sed "s!something!something/else!" ...
sed "s#something#something/else#" ...
etc.