LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 10-24-2011, 10:21 AM   #1
nano2
Member
 
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:
[code]
Hey this is the test dasfdafafaf ffasfad 12312444...
blab blab Mylist: [AB, BA, CA,DE,ED]
[\code]

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

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

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

My desired output
AB
BA
CA
DE
ED

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

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

try this:
Code:
sed -r "N;s/^Hey[^[]+\[([^]]*)]/\1/;s/,[[:blank:]]*/\n/g" file
If the 'Hey' occurs somewhere in the middle of the text then
Code:
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:
Code:
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: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.

Code:
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:

Code:
while read -r line; do

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

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.
 
  


Reply

Tags
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


All times are GMT -5. The time now is 11:41 PM.

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