LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-27-2020, 06:38 AM   #1
daichiash
LQ Newbie
 
Registered: Jun 2020
Posts: 2

Rep: Reputation: Disabled
Sed not substituting line number from variable when used in while loop while normally it works.


When i try to use line number from variable in sed it works individually but when same is done using while loop it not works.

eg.
Code:
cat file
den 
run
ten
run
fan

linenumber=2
replacetext=34
sed -i ""$linenumber"s/run/run\n"$replacetext"/" file

Output:
cat file
den
run
34
ten
run
fan
Above works and sed command is generated after variable expansion.

But when i use same in while loop it does work only for first instance and not all as i wanted to replace all instances with incrementing line number using pure sed solution. Awk or perl solution is not wanted.

eg.
Code:
replacetext=1
while read in; do replacetext=$((replacetext++)) && sed ""$in"s/run/run\n"$replacetext"/" file; done <<< "$(cat file | grep 'run' -n | cut -f1 -d ':')"


Expected Output: Match text will be replaced with itself followed by newline and a incrementing number.

den 
run
2
ten
run
3
fan


Obtained output:

den
run
2
ten
run
fan
Please only sed based solution required. And i wanted to know what unusual behaviour was occuring in while loop that it was not working.Because the loop variable is given the value of line number and then that line number is used in sed to replace macthed text on that line with itself folowed by newline and incremented number.

Sed seems to work when same variable line number is provided but not when usind in while loop.

Last edited by daichiash; 06-30-2020 at 02:43 AM.
 
Old 06-27-2020, 09:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,167

Rep: Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393Reputation: 7393
probably -i is missing ?
 
Old 06-27-2020, 09:42 AM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Quote:
Originally Posted by daichiash View Post
When i try to use line number from variable in sed it works individually but when same is done using while loop it not works.

eg.

cat file
den
run
ten
run
fan

linenumber=2
replacetext=34
sed -i ""$linenumber"s/run/run\n"$replacetext"/" file

Output:
cat file
den
run
34
ten
run
fan


Above works and sed command is generated after variable expansion.

But when i use same in while loop it does work only for first instance and not all as i wanted to replace all instances with incrementing line number using pure sed solution. Awk or perl solution is not wanted.

eg.

replacetext=1
while read in; do replacetext=$((replacetext++)) && sed ""$in"s/run/run\n"$replacetext"/" file; done <<< "$(cat file | grep 'run' -n | cut -f1 -d ':')"


Expected Output: Match text will be replaced with itself followed by newline and a incrementing number.

den
run
2
ten
run
3
fan


Obtained output:

den
run
2
ten
run
fan


Please only sed based solution required. And i wanted to know what unusual behaviour was occuring in while loop that it was not working.Because the loop variable is given the value of line number and then that line number is used in sed to replace macthed text on that line with itself folowed by newline and incremented number.

Sed seems to work when same variable line number is provided but not when usind in while loop.
Please use [code] tags around your code sections and format your code properly, otherwise it is hard to read. I ran your while loop with the data you provided and the output was:

Code:
replacetext=1
while read in; do
    replacetext=$((replacetext++)) && sed ""$in"s/run/run\n"$replacetext"/" file
done <<< "$(cat file | grep 'run' -n | cut -f1 -d ':')"
den
run
1
ten
run
fan
den
run
ten
run
1
fan
which is different from what you posted. So what did you actually execute to obtain your output?
One problem is that you are incrementing wrong. It should be just

Code:
((replacetext++))
without the assginment. The sed is executed twice because there are two matches from grep, thus the loop runs twice which results in the output that I got.
 
Old 06-27-2020, 09:56 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
Looks like a homework question. Moreover, I believe you misunderstood what you were required to do in this assignment.
Quote:
Find all lines matching a string and display their line numbers on the next line after the line matched. Use only sed for this.
^ This sounds like a reasonable homework question for somebody learning how to sed. The sed solution is simple, concise and efficient:
Code:
sed -n 'p;/run/=' file
OTOH,
Quote:
Originally Posted by daichiash View Post
i wanted to replace all instances with incrementing line number using pure sed solution. Awk or perl solution is not wanted.
...
Expected Output: Match text will be replaced with itself followed by newline and a incrementing number.
Yeah, the difference may seem insignificant at first glance. Count all lines vs. count matched lines. But in reality what you have asked is ridiculously hard to do with sed. No teacher in their right mind would force this onto their students requiring it to be done in sed only. If you still foolishly insist on doing it in pure sed then you may adapt the cat -n sed implementation from the sed manual:
Code:
#!/usr/bin/sed -nf
p
/run/{
  x; s/^$/1/
  p
  /^9*$/s/^/0/
  s/.9*$/_&/
  h; s/^.*_//; y/0123456789/1234567890/; x
  s/_.*$//
  G; s/\n//; h
}
IMO, refusing to do it in awk or perl is just plain stupid. Compare the code above to this:
Code:
awk '1;/run/,$0=++c' file

Last edited by shruggy; 06-28-2020 at 09:38 AM.
 
Old 06-28-2020, 05:09 AM   #5
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 daichiash View Post
sed -i ""$linenumber"s/run/run\n"$replacetext"/" file
There's just too many "-quotes in that.
 
Old 06-30-2020, 02:39 AM   #6
daichiash
LQ Newbie
 
Registered: Jun 2020
Posts: 2

Original Poster
Rep: Reputation: Disabled
Solved Myself with sed only solution Just thinked over it and slightky modified it.
Code:
replacetext="$(expe "$(cat file | grep 'run' -n | cut -f1 -d ':' | wc -l) + 1)"
while read in; do replacetext=$((replacetext-1)) && sed ""$in"s/run/run\n"$replacetext"/" file; done <<< "$(cat file | grep 'run' -n | cut -f1 -d ':' | tac)"

Last edited by daichiash; 06-30-2020 at 02:40 AM.
 
Old 06-30-2020, 02:53 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,688

Rep: Reputation: Disabled
Quote:
Originally Posted by daichiash View Post
Solved Myself with sed only solution
Nope. This is the output I get with your code (after fixing the typos):
Code:
den
run
ten
run
2
fan
den
run
1
ten
run
fan
 
Old 06-30-2020, 01:41 PM   #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
It's not very important here, but each instance of
Code:
cat file | grep 'run' -n
can be replaced with
Code:
grep 'run' -n file
 
  


Reply

Tags
sed


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
SED command failing while substituting variable with error unterminated `s' command vnarvankar Linux - Software 2 03-10-2016 09:10 AM
Domain.com/sub/ works normally but, sub.domain.com not works DeSouffle Linux - Server 1 10-11-2013 10:19 AM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
[SOLVED] Substituting a word in a column with a number increasing by one udiubu Programming 2 11-08-2011 04:03 PM
need help substituting with sed parker Programming 2 08-08-2003 10:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:57 PM.

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