LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2015, 08:32 PM   #1
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Rep: Reputation: 46
sed work in shell but not script


Ladies & Gents

It seams that I am unable to figure this out at this time. I do remember reading about a similar thing at one point over on stack exchange or somewhere but can't find it at the moment.

What I am trying to do is change a number (100 through 150) say 101 to a1 to be used as part of a file name.

Code:
count=101
elif ((count > 99))&&((count < 110));then
 echo $count | sed '/^10/a/'> $chapter
 echo $chapter
results in the following when executed
Code:
+ echo 101
+ sed '/^10/a/'
+ echo 71
I have tried several iterations of the above sed command with no joy.

In another place where I have a very similar sed, except with a "d" instead of an "a" I get a different error

Code:
+ echo 131
+ sed '/^13/d/'
sed: -e expression #1, char 7: extra characters after command
+ echo 71
The relevant code
Code:
count=$1
for (( a = 1; a < 6; a++)); do
  local chapter
  
  if ((count < 100)); then
    chapter=$count
    echo $chapter
  elif ((count > 99))&&((count < 110));then
    echo $count | sed '/^10/a/'> $chapter
    echo $chapter
  elif ((count > 109))&&((count < 120));then
    echo $count | sed '/^11/b/' > $chapter
    echo $chapter
  elif ((count > 119))&&((count < 130));then
    echo $count | sed '/^12/c/' > $chapter
    echo $chapter
  elif ((count > 129))&&((count < 140));then
    echo $count | sed '/^13/d/' > $chapter
    echo $chapter
  elif ((count > 139))&&((count < 150));then
    echo $count | sed '/^14/e/' > $chapter
    echo $chapter
  else
    chapter="f0"
  fi
count=$((count + 30))

done
It seams to exhibit this behavior every time it tries to make the change. If I had more time to play I might be able to figure it out by myself but play times is very limited due to my work load.

Thanks for any pointers.
 
Old 05-10-2015, 08:47 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Perhaps you should be using the substitute command ? As in
Code:
sed 's/^10/a/'

Last edited by syg00; 05-10-2015 at 08:49 PM. Reason: example
 
Old 05-10-2015, 10:20 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
echo ... > $chapter
what does this do?

I would use this:
Code:
chapter=$(echo ...)
 
Old 05-11-2015, 02:59 PM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by NevemTeve View Post
I would use this:
Code:
chapter=$(echo ...)
no, i would use:
Code:
chapter="..."
 
Old 05-11-2015, 05:29 PM   #5
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks guys,

Using a combination of the suggestions you posted got me past it. I added the 's/ back in as sgv00 suggested and that got me part way, but as I was thinking about it today while on the road what NevemTeve suggested made sense. So I did that too. And we have joy

I looked at ondoho's suggestion and would like to understand it better. I think it has something to do with shell expansion, but the only info I have at this point is
https://github.com/koalaman/shellcheck/wiki/SC2001

That explanation indicates that it will not work very well for complex changes. Is mine to complex? With needing to replace the first 2 digits of the number with a letter.

Anyway thanks
 
Old 05-12-2015, 11:59 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by rbees View Post
I looked at ondoho's suggestion and would like to understand it better.
i took nevemteve's reply literally. obviously the 3 dots are supposed to stand for sth else.
my reply was a little flippant.
applied to the above code example, nevemteve's answer is correct, and not mine.

upon deeper examination, i'm not sure if you can "echo ... > $var" at all.
i would think ">" would write into a file by the name of whatever $var is set to.

edit: btw, the following two lines do the same:
Code:
chapter=$(echo $count | sed 's/^10/a/')
chapter=${count/#10/a}
see here.

Last edited by ondoho; 05-12-2015 at 12:07 PM.
 
Old 05-12-2015, 12:35 PM   #7
rbees
Member
 
Registered: Mar 2004
Location: northern michigan usa
Distribution: Debian Squeeze, Whezzy, Jessie
Posts: 921

Original Poster
Rep: Reputation: 46
Thanks

Quote:
the following two lines do the same:
Code:
chapter=$(echo $count | sed 's/^10/a/')
chapter=${count/#10/a}
does the second need the # wouldn't chapter=${count/10/a} don the same thing?
 
Old 05-14-2015, 06:06 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by rbees View Post
does the second need the # wouldn't chapter=${count/10/a} don the same thing?
depends on what you want.
please read the link i provided.

edit: actually, no. "#" is equivalent to "^" in this example.

Last edited by ondoho; 05-14-2015 at 06:08 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script, sed nushki Linux - Newbie 1 03-31-2009 01:36 PM
awk sed shell script slcalice Programming 3 03-09-2009 05:40 AM
Shell Script , using Sed mogra Linux - Newbie 8 05-07-2008 01:18 PM
sed and awk in shell script bondoq Linux - Newbie 14 07-27-2007 01:52 AM
Using sed in a shell script RobHill Linux - General 4 05-29-2007 03:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:52 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration