Help with sed command - find a string between two characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with sed command - find a string between two characters
# 1  
Old 10-12-2012
Help with sed command - find a string between two characters

Hi,

I have a xml file (Config.xml)
Code:
<Header name="[Measure]" TDate="[COB]" PDate="[PCOB]">
        <Config>
                {"config" { "Nation" "Pri:[Region]|Sec:[SecRegion]"}}
        </Config>
</Header>

Now I wanted to printed all the strings between "[" and "]". I tried the following
Code:
cat Config.xml | sed -n 's/.*\[//;s/\].*//p'

But its only printing below strings (the last occurance in each line).
Code:
PCOB
SecRegion

What I expect is
Code:
Measure
COB
PCOB
Region
SecRegion

Can someone help please?

Thanks
# 2  
Old 10-12-2012
This is definitely not a good solution, but it will help:-

Code:
#!/bin/ksh
 
for char in `cat Config.xml | awk '{ for(i=1;i<=length;i++) print substr($0, i, 1)}'`
do
        if [ "${char}" = "[" 2> /dev/null ]
        then
                flag=1
        fi
        if [ "${char}" = "]" 2> /dev/null ]
        then
                flag=0
                print "\n"
        fi
        if [ ${flag} -eq 1 2> /dev/null ] && [ "${char}" != "[" ]
        then
                print "${char}\c"
        fi
done

Let me know if it worked...
# 3  
Old 10-12-2012
Code:
awk -F'[][]' '{for(i=2;i<=NF;i=i+2) print $i}' Config.xml

# 4  
Old 10-13-2012
Code:
perl -lne '@arr=($_=~/\[(\w+)\]/g);$"="\n";print "@arr";'  input_file

# 5  
Old 10-13-2012
Or..

Code:
awk 'NR>1{print $1}' RS=[ FS=] file

Code:
perl -lne 'print $1 while (/\[(.*?)\]/g)' file


Last edited by pamu; 10-13-2012 at 07:38 AM.. Reason: unable to access the link
# 6  
Old 10-13-2012
Try
Code:
sed -n '/\[/ {
        s/^[^\[]*\[//
        s/\]/\n/
        P
        D
        }
      ' Config.xml
Measure
COB
PCOB
Region
SecRegion

# 7  
Old 10-13-2012
Another awk:
Code:
awk '{while(match($0,/\[[^]]*]/))
{
 print substr($0,RSTART+1,RLENGTH-2)
 sub(/\[[^]]*]/,"")
}}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. Shell Programming and Scripting

sed replace nth characters with string

Hi, I hope you can help me out please? I need to replace from character 8-16 with AAAAAAAA and the rest should stay the same after character 16 gtwrhtrd11111111rjytwyejtyjejetjyetgeaEHT wrehrhw22222222hytekutkyukrylryilruilrGEQTH hrwjyety33333333gtrhwrjrgkreglqeriugn;RUGNEURGU ... (4 Replies)
Discussion started by: stinkefisch
4 Replies

3. Shell Programming and Scripting

Find/replace alpha characters in string

Hi, I would like to find a 3-letter character series in a string/variable and replace it with x's. An example set of strings is: 563MS333_101_afp_400-100_screening 563MS333_104-525_rjk_525_screening 563MS333_110_dlj_500-100_w24 563MS333_888-100_mmm_424_screening The only constants... (5 Replies)
Discussion started by: goodbenito
5 Replies

4. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

5. Shell Programming and Scripting

Find a string and then return the next 20 characters in multiple files

Hello all, I have a directory with 2000+ files. I need to look in each file for an invoice number. To identify this, i can search for the string 'BIG' and then retrieve the next 30 characters. I was thinking awk for this, but not sure how to do it. Each file contains one long string and in... (8 Replies)
Discussion started by: jdinero
8 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

sed cut characters of string

helloo I wonder if there's a way to cut characters out of a string and keep only the last 2 by using sed. For example if there's the todays' date: 2012-05-06 and we only want to keep the last 2 characters which are the day. Is there a quick way to do it with sed? (2 Replies)
Discussion started by: vlm
2 Replies

8. Shell Programming and Scripting

SED help delete characters in a string

Hi Please help me to refine my syntax. I want to delete the excess characters from the out put below. -bash-3.00$ top -b -n2 -d 00.20 |grep Cpu|tail -1 | awk -F ":" '{ print $2 }' | cut -d, -f1 4.4% us now i want to delete the % and us. How wil i do that to make it just 4.4. Thanks (7 Replies)
Discussion started by: redtred
7 Replies

9. Shell Programming and Scripting

Help to find string and return following characters from list of files

Hi, I'm fairly new to UNIX, but hopefully some-one can help me with this: I am using the following code to find files with the name "example.xml": find . -name "example.xml" -print that would print me a list like the example here: ./dir1/dir2/example.xml... (5 Replies)
Discussion started by: boijie
5 Replies

10. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies
Login or Register to Ask a Question