Using sed to remove spaces from middle of the line


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Using sed to remove spaces from middle of the line
# 1  
Old 06-03-2014
Using sed to remove spaces from middle of the line

Hi,
I need to correct href portion of the lines to edit out spaces from the line starting with position "<a href=" and ending at "target="

Below are 2 examples of extra space added by the server:
Code:
<td width=251 colspan=9 rowspan=22> <font size=2 face="courier"><tt><style>{font:7pt Courier  New;color:#000000}</style><a  href="/webaccess/AIntRez.cgi?ser ver=rsrv&report=%2Fdev%2Fypa%2Fhome%2Fentk ppa%2FTSTREP%2Freports%2FGET_TRANS ACTION_2000.rep&desformat=pdf&bat ch=yes&destype=file&p_date_time=200 807091331278990&p_batch_seqno=1&p_t ransaction_seqno=2&P_SESSION_ID=999999&P_ START_LINE=&P_END_LINE="  target="main">07/09/2008 13:31</a></tt></font><br></td>

<td width=183 colspan=10 rowspan=32> <font size=2 face="courier"><b><tt><style>{font:9pt Courier  New;color:#000000}</style ><a  href="/webaccess/AIntR ez.cgi?server=rsrv&rep ort=%2Fdev%2Fypa%2Fhome%2 Fentkppa%2FTSTREP%2Frepo rts%2FORDER_DETAIL_2000 .rep&desformat=pdf&batch= yes&destype=file&p_direct ory_number=000001&p_client _number =7500&p_dir_ver_stat=CURR ENT&p_req_mailbox_name=TS TUSER" target="main">  Current Order </a></tt></b></font><br></td>

I have tried several sed commands but all not success:
Code:
sed '/a  href/,/target=/ s/\d032//2g'

Thank you for any help.
Moderator's Comments:
Mod Comment Especially when trying to fix spacing problem, CODE tags are crucial.

Last edited by Don Cragun; 06-03-2014 at 05:47 PM.. Reason: Add CODE tags.
# 2  
Old 06-03-2014
What you are trying to accomplish is not clear to me.

If you're trying to change all occurrences of two or more adjacent spaces to a single space, try:
Code:
sed 's/  *//g'

If that isn't what you're trying to do, show us the desired output (using CODE tags) so we can see the difference between your input and what you're trying to get out of it.
# 3  
Old 06-03-2014
Thank you, Don for prompt reply.
These are my examples.

Original line:
Code:
<td width=251 colspan=9 rowspan=22> <font size=2 face="courier"><tt><style>{font:7pt Courier  New;color:#000000}</style><a  href="/webaccess/AIntRez.cgi?ser ver=rsrv&report=%2Fdev%2Fypa%2Fhome%2Fentk ppa%2FTSTREP%2Freports%2FGET_TRANS ACTION_2000.rep&desformat=pdf&bat ch=yes&destype=file&p_date_time=200 807091331278990&p_batch_seqno=1&p_t ransaction_seqno=2&P_SESSION_ID=999999&P_ START_LINE=&P_END_LINE="  target="main">07/09/2008 13:31</a></tt></font><br></td>

Expected result:
Code:
<td width=251 colspan=9 rowspan=22> <font size=2 face="courier"><tt><style>{font:7pt Courier  New;color:#000000}</style><a href="/webaccess/AIntRez.cgi?server=rsrv&report=%2Fdev%2Fypa%2Fhome%2Fentkppa%2FTSTREP%2Freports%2FGET_TRANSACTION_2000.rep&desformat=pdf&batch=yes&destype=file&p_date_time=200807091331278990&p_batch_seqno=1&p_transaction_seqno=2&P_SESSION_ID=999999&P_START_LINE=&P_END_LINE=" target="main">07/09/2008 13:31</a></tt></font><br></td>

Original line:
Code:
<td width=183 colspan=10 rowspan=32> <font size=2 face="courier"><b><tt><style>{font:9pt Courier  New;color:#000000}</style ><a  href="/webaccess/AIntR ez.cgi?server=rsrv&rep ort=%2Fdev%2Fypa%2Fhome%2 Fentkppa%2FTSTREP%2Frepo rts%2FORDER_DETAIL_2000 .rep&desformat=pdf&batch= yes&destype=file&p_direct ory_number=000001&p_client _number =7500&p_dir_ver_stat=CURR ENT&p_req_mailbox_name=TS TUSER" target="main">  Current Order </a></tt></b></font><br></td>

Expected result:
Code:
<td width=183 colspan=10 rowspan=32> <font size=2 face="courier"><b><tt><style>{font:9pt Courier  New;color:#000000}</style ><a href="/webaccess/AIntRez.cgi?server=rsrv&report=%2Fdev%2Fypa%2Fhome%2Fentkppa%2FTSTREP%2Freports%2FORDER_DETAIL_2000.rep&desformat=pdf&batch=yes&destype=file&p_directory_number=000001&p_client_number=7500&p_dir_ver_stat=CURRENT&p_req_mailbox_name=TSTUSER" target="main">  Current Order </a></tt></b></font><br></td>

# 4  
Old 06-04-2014
This could be done with sed by splitting the lines with extra spaces into three lines, deleting the spaces in the 2nd line of each split set, and rejoining the split lines into single lines again. I find awk to be easier to use for operations like this:
Code:
awk '
/<a +href=.* target=/ {
#printf("%din:%s\n", NR, $0)
	l1 = index($0, "<a ") + 2
#printf("Part1:XXX%sXXX\n", substr($0, 1, l1))
	e2 = index($0, " target=")
#printf("Part3:XXX%sXXX\n", substr($0, e2))
	nospc = substr($0, l1 + 1, e2 - l1)
#printf("Part2(before):\n\tXXX%sXXX\n", nospc)
#printf("Part2(after %d spaces removed:\n\tXXX%sXXX\n", gsub(/ /, "", nospc), nospc)
	gsub(/ /, "", nospc)
	printf("%s%s%s\n", substr($0, 1, l1), nospc, substr($0, e2))
	next
}
1' sample.xml

If you want to see how this script splits lines, removes spaces from the middle part, and rejoins lines; remove the octothorp (#) characters in front of the debugging printf() calls. (Once you see how it works, you can remove those lines entirely.)

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 06-04-2014
Great Suggestion. Works as expected. Thank you very much, Don !
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can't remove spaces with sed when calling it from sh -c

The following command works echo "some text with spaces" | sh -c 'sed -e 's/t//g''But this doesn't and should echo "some text with spaces" | sh -c 'sed -e 's/ //g''Any ideas? (3 Replies)
Discussion started by: Tribe
3 Replies

2. Shell Programming and Scripting

How to remove spaces on a line?

Hi, suppose I have the following data: albert music=top40 age=20 bob music=punk rock age=25 candy music=r n b age=22 dave music=mozart or bach only age=30 I want to extract and manipulate the music column but it's got spaces in it. How can I substitute the space with an underscore... (2 Replies)
Discussion started by: almonds
2 Replies

3. Shell Programming and Scripting

sed remove newlines and spaces

Hi all, i am getting count from oracle 11g by spooling it to a file. Now there are some newline characters and blank spaces i need to remove these. pl provide me a awk/sed solution. the spooled file is attached. i tried this.. but not getting req o/p (6 Replies)
Discussion started by: rishav
6 Replies

4. Shell Programming and Scripting

tr and sed remove spaces. how to stop this?

if the answer is obvious, sorry, I'm new here. anyway, I'm using tr to encrypt with rot-13: echo `cat $script | tr 'a-zA-Z' 'n-za-mN-ZA-M'` > $script it works, but it removes any consecutive spaces so that there is just one space between words. I've had this problem before while using sed to... (5 Replies)
Discussion started by: Trichopterus
5 Replies

5. Shell Programming and Scripting

How to remove spaces using awk,sed,perl?

Input: 3456 565 656 878 235 8 4 8787 3 7 35 878 Expected output: 3456 565 656 878 235 8 4 8787 3 7 35 878 How can i do this with awk,sed and perl? (10 Replies)
Discussion started by: cola
10 Replies

6. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

7. Shell Programming and Scripting

Remove extra spaces in a line

Hi, I need a help in deleting extra spaces in a text. I have a huge file, a part of it is :- 3 09/21/08 03:32:07 started undef mino Oracle nmx004.wwdc.numonyx.com Message Text : The Oracle session with the PID 1103 has a CPU time ... (6 Replies)
Discussion started by: vikas027
6 Replies

8. Shell Programming and Scripting

remove leading spaces from a line

Hi friends I need some help, I have a file which looks as follows TEMP 014637065 014637065 517502 517502 RTE 517502 517502 RTE AWATER_TEST 12325 23563 588323 2323 5656 32385 23235635 ANOTHER_TEST 12 5433 FTHH 5653 833 TEST 123 123 3235 5353 353 53 35 353 535 3 YTERS GJK JKLS ... (6 Replies)
Discussion started by: lijojoseph
6 Replies

9. Shell Programming and Scripting

How to 'sed' the middle line?

Hi, If I have 90 lines and I want to print the 45th line, is there a quick sed command for this? thx. (7 Replies)
Discussion started by: g_jumpin
7 Replies

10. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies
Login or Register to Ask a Question