Getting sed to work on part of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting sed to work on part of a line
# 1  
Old 08-01-2008
Getting sed to work on part of a line

I been trying to get this right. I have trying to get rid of spaces in between the character < and the character >. Everytime I try, sed gets too greedy and do the whole line.

Ex.

< T AG 1> Hello, how are you doing?
<Tag 2> I am doing fine.

I want this:

<TAG1> Hello, how are you doing?
<Tag2> I am doing fine.


What I keep on getting is this:

<TAG1>Hello,howareyoudoing?
<Tag2>Iamdoingfine.


Does anyone has a way to only do part of a line when I find that pattern using sed?
# 2  
Old 08-01-2008
Use awk:

Code:
awk -F'>' 'BEGIN {OFS = ">"} {gsub(/ /,"",$1); print}'

# 3  
Old 08-04-2008
Does not work

Quote:
Originally Posted by cfajohnson
Use awk:

Code:
awk -F'>' 'BEGIN {OFS = ">"} {gsub(/ /,"",$1); print}'


This does not seem to work. Keeps giving me illegal statement error.
# 4  
Old 08-04-2008
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 5  
Old 08-04-2008
This seems to partially work. It works fine for lines that have a tag <Some tag> but it causes unintended side effect. It also does lines that don't have tag in it.
Ex.
I am fine. This is line 200
It becomes:
Iamfine.Thisisline200

Is there any way to get around this? Let me expand the example a little further.
Ex.
< T AG 1> Hello, how are you doing?
<Tag 2> I am doing fine.
This is a test < T A G 3> I
am doing this to test how to
get rid of spaces on part of a
line.


I just want to find a way to only get rid of spaces between the < and the > and nothing else on that line and nothing else on lines that don't have < and >.

Last edited by quixoticking11; 08-04-2008 at 02:13 PM.. Reason: Clarification
# 6  
Old 08-04-2008
awk -F'<|>' '{gsub(/ /,"",$2);print $1 "<" $2 ">" $3}' file | sed 's/<>//'
# 7  
Old 08-04-2008
Quote:
Originally Posted by quixoticking11
I just want to find a way to only get rid of spaces between the < and the > and nothing else on that line and nothing else on lines that don't have < and >.

Using bash or ksh93:
Code:
while IFS= read -r line
do
  while :
  do
    case $line in
      *"<"*">"*)
           left=${line%%<*}
           line=${line#"$left"}
           tag=${line%%>*}
           line=${line##*"$tag"}
           printf "%s" "$left" "${tag// /}"
           ;;
      *) break;;
    esac
  done
  printf "%s\n" "$line"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - How to replace right part of equal sign (=) on a line

Hello. Using a bash script , I have a variable name for the file I want to modify FILE_TO_EDIT="/etc/my_config_file"And I have a variable name for the parameter to change PARAMETER="fallback_node" PARAMETER_NEW_VALUE="http://my_server_name.com/new_path" A config file may contain : 1°)... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

3. Shell Programming and Scripting

SED and Solaris Append line to the end of File does not work

Hello, I have to add a new line at the end of a File on Solaris-System: I think my script should be right, because I evaluated it to other threads. However the script does not what I am expected it should do. My file might look like this: Line1 Line2 Line3 And my script could... (7 Replies)
Discussion started by: Timo_HR
7 Replies

4. Shell Programming and Scripting

sed command for picking a part of line

Hello Friends I want to use sed command to pick a part of line. FOr example I only need the /home_put1/bidds/myfo part of following line fish://ulavet@rits1.ula.com.tr/home_put1/bidds/myfo How can I do this bu using sed command ? (2 Replies)
Discussion started by: rpf
2 Replies

5. UNIX for Dummies Questions & Answers

sed or awk - removing part of line?

hi all, I am having trouble finding the right string for this - I dont know whether to use awk or sed.. If I have a file with alot of names and phone numbers like this McGowan,Sean 978-934-4000 Kilcoyne,Kathleen 603-555-1212 Club603,The 617-505-1332 Boyle,William 301-444-1221 And... (11 Replies)
Discussion started by: alis
11 Replies

6. Shell Programming and Scripting

Using Sed to remove part of line with regex

Greetings everyone. Right now I am working on a script to be used during automated deployment of servers. What I have to do is remove localhost.localdomain and localhost6.localdomain6 from the /etc/hosts file. Simple, right? Except most of the examples I've found using sed want to delete the entire... (4 Replies)
Discussion started by: msarro
4 Replies

7. Shell Programming and Scripting

change part of a line with sed

Hi gurus, I'd like to change this complete line on a file: BAN_COMMAND="/etc/apf/apf -d $ATTACK_HOST {bfd.$MOD}" to this one: BAN_COMMAND="/sbin/iptables -A INPUT -s $ATTACK_HOST -j DROP" I've tried a lot without any successful . :( thanks in advance Israel. (2 Replies)
Discussion started by: iga3725
2 Replies

8. Shell Programming and Scripting

Does Sed Search/Replace Work For Multiple Matches On The Same Line?

Hello, I would like to delete all the footnotes in all my htm files. Hence, I have to delete the whole font tag pairs, i.e. deleting everything between the begin/end font tags. I create a testfile, of which data parts of all four lines are the same except for the number of font tag pairs,... (3 Replies)
Discussion started by: cibalo
3 Replies

9. Shell Programming and Scripting

SED - replace with new line didn´t work for solaris

Hi This is what I was trying to do, comment one line and add something different in a new line right next. This is the command I want to do more .profile | sed 's,STRING1, #STRING1 NEWLINE STRING2,' (I´m using ',' because my string is something like this exec... (3 Replies)
Discussion started by: alcalina
3 Replies

10. Shell Programming and Scripting

Replace part of a line with sed/awk

Hello I have a document and in this document I have several occurrence of "VAR == xxxxxxx" and xxxxx can be anything. I don't know what it is. I want to replace the 'xxxxx's with something I know. What I know however, is the line numbers of the VAR =='s in the file. How can I replace... (1 Reply)
Discussion started by: alirezan
1 Replies
Login or Register to Ask a Question