Sed: Remove whitespace between two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed: Remove whitespace between two strings
# 1  
Old 01-26-2011
Sed: Remove whitespace between two strings

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:
Code:
cd media/Audio/WAVE/9781933976334    /mp3

What I want my output to be:
Code:
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.
# 2  
Old 01-26-2011
How about this:
Code:
$ echo 'cd media/Audio/WAVE/9781933976334    /mp3' | sed 's=\([0-9][0-9]*\)\( *\)\(/mp3\)=\1\3='
cd media/Audio/WAVE/9781933976334/mp3
$

# 3  
Old 01-26-2011
I ended up using a while loop

Code:
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?
# 4  
Old 01-26-2011
There is so many solution, here is some if you like to use loops. If input include exactly 3 values then
Code:
cat somefile | while read cmd path mp xxx
do
        echo "$cmd $path$mp"
done > newfile

And if like to use awk, then
Code:
cat somefile | awk '{print $1, $2$3}'  > newfile

# 5  
Old 01-27-2011
another sed solution..
Code:
echo "cd media/Audio/WAVE/9781933976334    /mp3"|sed 's/ \+//2'

# 6  
Old 01-27-2011
And how about:
Code:
echo 'cd media/Audio/WAVE/9781933976334    /mp3' | sed 's! */mp3!/mp3!'

# 7  
Old 01-27-2011
Franklin, please explain your use of !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to append strings with whitespace?

Hi, Need help. This seems simple but I tried many things but failed to get what I wanted. Below is the input file: ... (7 Replies)
Discussion started by: bunny_merah19
7 Replies

2. Shell Programming and Scripting

USING sed to remove multiple strings/words from a line

Hi I use sed comnand to remove occurance of one workd from a line. However I need to removed occurance of dufferent words in ne line. Original-1 Hi this is the END of my begining Comand sed s/"END"/"start"/g Output-1 Hi this is the start of my beginig But I have more... (9 Replies)
Discussion started by: mnassiri
9 Replies

3. UNIX for Dummies Questions & Answers

[Solved] How remove leading whitespace from xml (sed /awk?)

Hi again I have an xml file and want to remove the leading white space as it causes me issues later in my script I see sed is possible but cant seem to get it to work I tried sed 's/^ *//' file.xml output <xn:VsDataContainer id="1U104799" modifier="update"> ... (10 Replies)
Discussion started by: aniquebmx
10 Replies

4. Shell Programming and Scripting

awk or sed script to remove strings

Below am trying to separate FA-7A:1, In output file it should display 7A 1 Command am using Gives same output as below format: 22B7 10000000c9720873 0 22B7 10000000c95d5d8b 0 22BB 10000000c97843a2 0 22BB 10000000c975adbd 0 Not showing FA ports as required format... (5 Replies)
Discussion started by: aix_admin_007
5 Replies

5. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

6. Shell Programming and Scripting

Remove strings within range using sed

Hey folks I have a big file that contains junk data between the tags <point> and </point> and I need to delete it (including `<point>' and `</point>'). i.e. a = 1 <point> 123123 2342352 234231 234256 </point> print a needs to become a = 1 print a I'm certain that this is a... (10 Replies)
Discussion started by: ksk
10 Replies

7. Shell Programming and Scripting

sed: remove characters between and including 2 strings

I have the following line: 4/23/2010 0:00:38.000: Copying $$3MSYDDC02$I would like to use sed (or similiar) to remove everthing between and including $ that appears in the line so it ends up like this. 4/23/2010 0:00:38.000: Copying 3MSYDDC02I have been trying these but i'm really just... (5 Replies)
Discussion started by: jelloir
5 Replies

8. UNIX for Dummies Questions & Answers

remove whitespace

I combined 2 files using the paste command. It gave me something like this: 123445 ,AABBNN 22344 ,BBVVMM I want to remove the whitespace between the end of string 1 and the comma (there is more blank space than my post is showing). Would I... (2 Replies)
Discussion started by: nickg
2 Replies

9. Shell Programming and Scripting

sed : remove whitespace

I'm trying to remove the whitespace at the end of each line of a text file in ksh. Im using sed s/ $//g' file1.txt > file2.txt It's not working. Any clues? (3 Replies)
Discussion started by: b.hamilton
3 Replies

10. Shell Programming and Scripting

remove whitespace and test

Hi, I know removing whitespaces I can found so many threads to read how it works and i did it, but my problem isn't solved... I have in my script a variable $1 which can contains a text like " Channel ". No I want to check if $1 contains the word Channel, but I don't know how many... (4 Replies)
Discussion started by: bensky
4 Replies
Login or Register to Ask a Question