Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 10-24-2011, 10:21 AM   #1
Registered: May 2007
Posts: 100

Rep: Reputation: 15
BASH :sed find and replace

I have the following scenario

Have a file that has the following:
Hey this is the test dasfdafafaf ffasfad 12312444...
blab blab Mylist: [AB, BA, CA,DE,ED]

I want to do some processing on the list values AB BA CA DE ED

so I need to remove the following text Hey as far ]

this is what i have

sed -c -i "s/\( *[ *\).*/\1]/" test.dat

for line in `cat test.dat`;
echo $line;

My desired output

But not getting this any spot why ?
Old 10-24-2011, 10:49 AM   #2
Senior Member
Registered: Jan 2010
Posts: 1,614

Rep: Reputation: 450Reputation: 450Reputation: 450Reputation: 450Reputation: 450

try this:
sed -r "N;s/^Hey[^[]+\[([^]]*)]/\1/;s/,[[:blank:]]*/\n/g" file
If the 'Hey' occurs somewhere in the middle of the text then
sed -r "/^Hey/{N;s/^Hey[^[]+\[([^]]*)]/\1/;s/,[[:blank:]]*/\n/g}" file
If there are more than two lines from 'Hey' to the pattern AB, BA, ...
then let me know. In this case we will have to tweak the sed a bit more.

BTW, you should not use a for-loop for this. Use this instead:
while read -r line; do
 echo "${line}"
done < file
Old 10-24-2011, 04:56 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
I'd like to have a bit more clarification on the input. Is there only the one [...] section in the text? Are there multiple entries? Multiple fields on a line? Can there be other lines with brackets in them that you don't want to process? etc.

Assuming that the lines above are representative, here are a couple of other ways about it.

First, here's my attempt to use awk to extract the fields.

awk -F '[][]' '/Mylist/ { split( $2 , arr, /[, ]+/) ; for ( i=1; i<=asort(arr); i++ ) { print  arr[i]} }' file
This will extract every field in the file at once however. You'll need to run it in a loop it if you want it to work on only a line at a time.

Second: a bash-only solution, this time going line-by-line, using with the while loop crts provided:

while read -r line; do

	if [[ $line == *[* ]]; then
		IFS=" ," arr=( $l2 )
		echo "${arr[@]}"

done < file
This should put each entry in the bracketed field into its own array element. Note though that each loop will reset the array, so make sure you process it/save it to somewhere else before the next iteration.


bash sed find

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with sed doing find and replace lostNspace Programming 2 08-25-2011 07:23 PM
sed find and replace with wildcard shak4031 Linux - Newbie 3 08-05-2011 03:17 PM
find or replace without using sed adarshmca Linux - Newbie 9 12-28-2010 05:41 AM
find or replace through sed adarshmca Linux - Newbie 3 12-27-2010 11:35 PM
bash or perl replace .. maybe sed hostpc Linux - Software 4 10-11-2007 04:21 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration