sed: -e expression #1, char 0: no previous regular expression


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users sed: -e expression #1, char 0: no previous regular expression
# 1  
Old 08-19-2012
sed: -e expression #1, char 0: no previous regular expression

Hello All,

I'm trying to extract the lines between two consecutive elements of an array from a file.
My array looks like:
Code:
problem_arr=(PRS111 PRS213 PRS234)

j=0
while [ $j -le ${problem_arr[@]} ]
do
    k=`expr $j + 1`
    sed  -n "/${problem_arr[$j]}/,/${problem_arr[$k]}/p" problemid.txt
    ---some operation goes here----
    j=`expr $j + 1`
done

However, i get
Code:
sed: -e expression #1, char 0: no previous regular expression

error message.

Just to help you narrow down the problem i want to convey u that following line works:
Code:
sed  -n "/${problem_arr[0]}/,/${problem_arr[1]}/p" problemid.txt

Could you please help me how to use array subscript variable in such cases? Thank you so much in advance!

Regards,
Indu


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by zaxxon; 08-19-2012 at 03:07 PM.. Reason: code tags
# 2  
Old 08-19-2012
Looks like you've got two problems in your script - obviously both with the linewhile [ $j -le ${problem_arr[@]} ]
First: ${problem_arr[@]} will yield ALL array members and the shell should complain when trying to compare. I guess you want it to read ${#problem_arr[@]} which yields the count of members in array.
Second: You correctly start the loop at j=0, but you run it one too far up - ${problem_arr[${#problem_arr[@]}]} will be empty and thus sed complains. make the comparison [ $j -lt ... ] and it will fly.
# 3  
Old 08-19-2012
sed: -e expression #1, char 0: no previous regular expression

Thanks RudiC for your response.
I verified the points you have highlighted, and there is no problem with array size & initialization.Anyways, i can assign array size to another variable and use it in while condition.
I'd want you just focus on sed part of it, as it throws error there.

Hope you got me right!

Regards,
Indu
# 4  
Old 08-19-2012
Quote:
Originally Posted by InduInduIndu
I'd want you just focus on sed part of it, as it throws error there.
That sed error means that the first use of a regular expression is empty. That's happenning because your variable expansion yields nothing.

If you modified your script according to RudiC's suggestions, post it so we can see the current version.

Also, you may find it instructive to insert set -x before the while-loop.

Regards,
Alister
# 5  
Old 08-19-2012
You haven't said what shell you're using, but at least with bash and ksh, setting an array with:
Code:
array=(a b c)

sets ${array[1]} to a, ${array[2]} to b, and ${array[3]} to c. It doesn't set ${array[0]} so your first call to sed
Code:
sed  -n "/${problem_arr[0]}/,/${problem_arr[1]}/p" problemid.txt

expands to:
Code:
sed  -n "//,/PRS111/p" problemid.txt

and since your first RE is //, it is trying to search for the previously used RE (but there is no previously used RE in this invocation of sed.
# 6  
Old 08-20-2012
Quote:
Originally Posted by Don Cragun
You haven't said what shell you're using, but at least with bash and ksh, setting an array with:
Code:
array=(a b c)

sets ${array[1]} to a, ${array[2]} to b, and ${array[3]} to c. It doesn't set ${array[0]} so your first call to sed
Code:
sed  -n "/${problem_arr[0]}/,/${problem_arr[1]}/p" problemid.txt

expands to:
Code:
sed  -n "//,/PRS111/p" problemid.txt

and since your first RE is //, it is trying to search for the previously used RE (but there is no previously used RE in this invocation of sed.
Sorry, disagree. From the bash man page:
Quote:
Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [sub- script]=string. Indexed array assignments do not require the bracket and subscript. When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; other- wise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero.
Running InduInduIndu's code snippet - using ${#...} to correctly get the member count - fails in the fourth loop,
Code:
++ k=4
++ sed -n //,//p problemid.txt
sed: -e expression #1, char 0: no previous regular expression

indicating that he should limit his loop count to 0 - 2. The first loop works fine:
Code:
++ sed -n /PRS111/,/PRS213/p problemid.txt

# 7  
Old 08-20-2012
Quote:
Originally Posted by RudiC
Sorry, disagree. From the bash man page:
Running InduInduIndu's code snippet - using ${#...} to correctly get the member count - fails in the fourth loop,
Code:
++ k=4
++ sed -n //,//p problemid.txt
sed: -e expression #1, char 0: no previous regular expression

indicating that he should limit his loop count to 0 - 2. The first loop works fine:
Code:
++ sed -n /PRS111/,/PRS213/p problemid.txt

Ouch. Yes, you're right. Both bash and ksh use the same notation for setting an indexed array, but bash uses 0 based indexing and ksh uses 1 based indexing. (I guess you can tell that I do most of my shell scripting with ksh. Smilie)
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed regular expression

Hi , I need to remove pipe character from a |^ delimeted file. Something like |^tran|sformers||^|revenge |of fallen|^ to |^transformers|^revenge of fallen|^... Cold anybody please help to build the regular expression using sed . many thanks. Please use code tags next time for... (1 Reply)
Discussion started by: kokjek
1 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

sed: -e expression #1, char 21: unterminated `s' command

I have read many threads, but I still didn't find the right answer. May be i didn't find the right thread, though are so many threads for the same question. Basically the situation is - find date in a file and replace it with another date. (its not homework, its part of lot of a big processing,... (10 Replies)
Discussion started by: avinthm
10 Replies

4. UNIX for Dummies Questions & Answers

Regular Expression In Sed

Hi , I am learing sed echo abc 123 def 456 | sed 's|\(*\) \(*\)|\1|' is returning abc def 456 i was hoping abc def "\1" should only print the occurence of the first pattern but according to my understanding it is just removing the first occurence of the second pattern... (7 Replies)
Discussion started by: max_hammer
7 Replies

5. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

6. Shell Programming and Scripting

sed regular expression help

please consider this: echo "11111*X*005010X279~ST*270*1111111*005010X279~BHT*0011*11" | sed 's/.*\(005010X(\d)(\d)(\d)*\).*$/\1/'i'm searching for first occurrence of 005010X while leaving rest of characters out. :confused: any tips? thnx in advance guys. (7 Replies)
Discussion started by: grep01
7 Replies

7. Shell Programming and Scripting

Regular expression (sed)

Hi I need to get text that are within "" For example File: asdasd "test test2" sadasds asdda asdasd "demo demo2" Output: test test2 demo demo2 Any help is good Thank you (12 Replies)
Discussion started by: blito_loco
12 Replies

8. Shell Programming and Scripting

Regular expression with SED

Hi! I'm trying to write a regexp but I have no luck... I have a string like this: param1=sometext&param2=hello&param3=bye Also, the string can be simply: param2=hello I want to return the value of param2: "hello". How can I do this? Thanks. (3 Replies)
Discussion started by: GagleKas
3 Replies

9. Shell Programming and Scripting

Regular expression with sed

Hi, I'm trying following:echo "test line XA24433 test" | sed 's/.*X\(.*\)/X\1/' XA24433 test While I want the output as: XA24433 I want to grab the words starting with letter X till the next space, this word can be anywhere in the line. (9 Replies)
Discussion started by: nervous
9 Replies
Login or Register to Ask a Question