Computing.Net > Forums > Unix > sed substitution

sed substitution

Reply to Message Icon

Original Message
Name: Jonny1234
Date: December 28, 2004 at 12:03:15 Pacific
Subject: sed substitution
OS: Unix
CPU/Ram: xxx
Comment:

Hi,

I would like to remove all characters between [ and ] in each line of a text file but only if there is NOT an X between the [ and ]. If there IS an X then the string between the [ and ] should remain unchanged.

Is this possible with sed?

Many Thanks,
Jonny


Report Offensive Message For Removal

Response Number 1
Name: cdac1000
Date: December 29, 2004 at 03:19:25 Pacific
Subject: sed substitution
Reply: (edit)

grep -v "X" FILENAME | sed "s/[0-9,A-Z]//g"


Report Offensive Follow Up For Removal

Response Number 2
Name: David Perry
Date: December 29, 2004 at 06:28:31 Pacific
Subject: sed substitution
Reply: (edit)

# echo "This [.] is a test" | sed -e '/\[[^X]\]/s/\(\[\)[^]]*\(\]\)/\1\2/'

# echo "This [X] is a test" | sed -e '/\[[^X]\]/s/\(\[\)[^]]*\(\]\)/\1\2/'


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: December 29, 2004 at 08:09:37 Pacific
Subject: sed substitution
Reply: (edit)

David:

Would you mind providing an explanation of your sed solution? Thanks!

Nails


Report Offensive Follow Up For Removal

Response Number 4
Name: Jim Boothe
Date: December 29, 2004 at 08:27:09 Pacific
Subject: sed substitution
Reply: (edit)

I think the solution needs to allow for any number of characters inside the brackets - not just a single character. And also allow for multiple bracketed expressions in each line:

echo "This [abc][rXst][def] is a test" | sed -e 's/\[[^X[]*\]/[]/g'


Report Offensive Follow Up For Removal

Response Number 5
Name: David Perry
Date: December 29, 2004 at 09:12:15 Pacific
Subject: sed substitution
Reply: (edit)

I was really hoping someone more clever than me would answer this but I just couldn't leave grep -v as the answer.

sed -e '/\[[^X]\]/s/\(\[\)[^]]*\(\]\)/\1\2/

/\[^X]\]/ match a line that does not have X between the [ and ]. This should be done better.

/\(\[\)[^]]*\(\]\)/ grab the [ into \1 and ] into \2 omitting the string of anything other than ] in between.



Report Offensive Follow Up For Removal


Response Number 6
Name: Jim Boothe
Date: December 29, 2004 at 09:35:55 Pacific
Subject: sed substitution
Reply: (edit)

My solution posted earlier searches for a string that begins with [ that is followed by any number of characters that are NOT X or [ and terminates with ]. And it will change that string to []. The /g flag makes it process all occurrences of this pattern on each line instead of just the first occurrence.

I exclude the [ in my pattern because an RE will always qualify the longest possible string. If I did not exclude the [, then it would locate a single pattern in the string:

abc[def]ghi[jkl]mno

and change it to:

abc[]mno

instead of the correct:

abc[]ghi[]mno


Report Offensive Follow Up For Removal

Response Number 7
Name: Jonny1234
Date: December 29, 2004 at 13:31:35 Pacific
Subject: sed substitution
Reply: (edit)

Many thanks to all of you for replying.

Jim's one seems to be working.

Regards,
Jonny


Report Offensive Follow Up For Removal

Response Number 8
Name: fxcre8or
Date: January 3, 2005 at 20:49:01 Pacific
Subject: sed substitution
Reply: (edit)

If anyone knows how to append a blank line after every
single space - I would greatly appreciate it.

ie:

before:

hello world how are you?


after:

hello
world
how
are
you?

Thank you in advance!  


Report Offensive Follow Up For Removal

Response Number 9
Name: nails
Date: January 3, 2005 at 21:21:45 Pacific
Subject: sed substitution
Reply: (edit)


echo "hello world how are you?"|xargs -n1



Report Offensive Follow Up For Removal

Response Number 10
Name: fxcre8or
Date: January 4, 2005 at 12:24:05 Pacific
Subject: sed substitution
Reply: (edit)

thank you for the response - but i should have been
clearer. i have a file with a list of items separated by
spaces - and i want to have each item on a separate line.

i tried

cat filename|xargs -nl > filename2

and i get

xargs: illegal argument count

i am using the terminal on OS X - is that a factor?

excuse my ignorance - and thanks again!

fxcre8or


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: sed substitution

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software