LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-12-2018, 07:07 AM   #1
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Rep: Reputation: Disabled
SED with a string


I can extract line one from a text file and place it into the string $LINE using:
Code:
LINE=$(sed -n 1p myfile.txt)
In my script I want to iterate through all the lines and extract one line at a time to do some work on them.
I can't figure out how to enter a string holding a variable into this bit of code. I was expecting something like the line below to work but it fails.
Code:
LINE=$(sed -n $LINENUMBER p myfile.txt)
What is the correct syntax?

Many thanks for any help.
 
Old 09-12-2018, 07:16 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi

You're close, but there will be a space between 1 and p. Try {} around the variable name, and remove the space.

Code:
LINE=$(sed -n ${LINENUMBER}p myfile.txt)
 
1 members found this post helpful.
Old 09-12-2018, 07:19 AM   #3
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
Hi

You're close, but there will be a space between 1 and p. Try {} around the variable name, and remove the space.

Code:
LINE=$(sed -n ${LINENUMBER}p myfile.txt)
Unfortunately that fails also. What should the {} do?

Cheers
 
Old 09-12-2018, 07:22 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
However, if you want to iterate over all line in a file, it is not the best way to do it. Here's a much cleaner and faster way to iterate over all lines of a file:
Code:
while read line ; do
    echo "== work on this this line: $line"
done < myfile.txt
 
Old 09-12-2018, 07:27 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Are you sure LINENUMBER has a value? Using ${LINENUMBER} just means that the variable name is $LINENUMBER and not $LINENUMBERp, so the result will be something like 1p and not 1 p.

Put the command "set -x" in the beginning of your script, and you will get debug information and can see what is happening.
 
Old 09-12-2018, 07:31 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Entropy1024 View Post
Unfortunately that fails also. What should the {} do?
For sed, the line number and the 'p' cannot be seperated by space. And this would not work:
Code:
LINE=$(sed -n $LINENUMBERp myfile.txt)   #### WRONG
...because the shell would read the variable as LINENUMBERp, including the 'p'.

So, in bash, this can be solved to put {} around LINENUMBER to indicate what exactly the variable name is.
Are you using bash to run the script?

Anyway, don't iterate over lines in a file this way. See my other post above.

Last edited by Hko; 09-12-2018 at 07:32 AM.
 
Old 09-12-2018, 08:04 AM   #7
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Hko View Post
For sed, the line number and the 'p' cannot be seperated by space. And this would not work:
Code:
LINE=$(sed -n $LINENUMBERp myfile.txt)   #### WRONG
...because the shell would read the variable as LINENUMBERp, including the 'p'.

So, in bash, this can be solved to put {} around LINENUMBER to indicate what exactly the variable name is.
Are you using bash to run the script?

Anyway, don't iterate over lines in a file this way. See my other post above.
Yes I'm using BASH.

If I use:
Code:
LINE=$(sed -n {$LINENUMBER}p myfile.txt)
sed: -e expression #1, char 0: unmatched `{'
If I use:
Code:
LINE=$(sed -n ${$LINENUMBER}p myfile.txt)
line 57: ${$LINENUMBER}p: bad substitution
If I use:
Code:
LINE=$(sed -n {LINENUMBER}p myfile.txt)
sed: -e expression #1, char 3: unmatched `{'
What is thew correct syntax please?
 
Old 09-12-2018, 08:11 AM   #8
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Entropy1024 View Post
What is thew correct syntax please?
The correct syntax is the one Guttorm posted.
 
Old 09-12-2018, 08:18 AM   #9
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Hko View Post
The correct syntax is the one Guttorm posted.

If you are refering to:
Code:
LINE=$(sed -n ${LINENUMBER}p myfile.txt)
It returns with:
sh: 1: myfile.txtp: not found
 
Old 09-12-2018, 08:20 AM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Entropy1024 View Post
If you are refering to:
Code:
LINE=$(sed -n ${LINENUMBER}p myfile.txt)
It returns with:
sh: 1: myfile.txtp: not found
It shouldn't.
It is correct.

Guessing from the error message you posted, renaming your file to myfile.txtp might make it work though.

If not, what does this output on your system?
Code:
echo ${LINENUMBER}

Last edited by Hko; 09-12-2018 at 08:26 AM.
 
Old 09-12-2018, 09:08 AM   #11
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Hko View Post
It shouldn't.
It is correct.

Guessing from the error message you posted, renaming your file to myfile.txtp might make it work though.

If not, what does this output on your system?
Code:
echo ${LINENUMBER}
I can see where you are going with concatenating a p to the filename. However would like to know why the correct solution is not working.
Code:
echo ${LINENUMBER}
returns a line within the myfile.txt

Last edited by Entropy1024; 09-12-2018 at 09:10 AM.
 
Old 09-12-2018, 09:11 AM   #12
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Entropy1024 View Post
Code:
echo ${LINENUMBER}
returns a line within the myfile.txt
What exactly?
A number?
 
Old 09-12-2018, 09:21 AM   #13
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Here's an entire script as a Proof Of Concept.

Copy-Paste into your editor.
Save as poc.sh.
Make executable: chmod +x poc.sh
Run it: ./poc.sh

Code:
#!/bin/bash

cat <<EOF >myfile.txt
line one
line two
line three
EOF

LINENUMBER=2
LINE=$(sed -n ${LINENUMBER}p myfile.txt)

echo "LINENUMBER = $LINENUMBER"
echo "LINE = $LINE"
Output is:
Code:
LINENUMBER = 2
LINE = line two
If it is not, check your operating system, and bash shell etc...

Last edited by Hko; 09-13-2018 at 03:26 AM.
 
Old 09-12-2018, 09:23 AM   #14
Entropy1024
Member
 
Registered: Dec 2012
Location: UK
Distribution: Ubuntu 16 & 17
Posts: 131

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Hko View Post
What exactly?
A number?
No. I understand whats going wrong now.
Originally I had a loop from 1-64, as the file always has 64 lines in it. Then was trying to get each one of the lines extracted using sed.
So I was passing this number to SED in the "sed -n $line p" which did not work. However I think at that point "sed -n ${line}p" would have worked. However I followed an earlier suggestion from Hko to use "while read line ; do" command.
Now this command was sending the actual line to that value and so sed was receiving a line of text and not a number.

Makes sense now.
Thanks for your help.
 
Old 09-12-2018, 10:10 PM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Each call to sed must open the file.
The smart alternative was stated in post#4:
The file is opened once for the while-do-done block, and each call to read reads a new line into a variable.
Code:
while read line
reads a line into the variable $line, tests the exit status, and ends the loop if there is no more line (eof).
The whole loop is redirected from the file.
If you do not want a loop, you can redirect a { code block } like this
Code:
{
for demo in 1 2 3
do
  if read line
  then
    echo "line#$demo is $line"
  else
    echo "eof"
  fi
done
} < myfile.txt
You can take an explicit file descriptor 3 or 4 or ... to avoid a conflict with stdin
Code:
{
...
  read line <&3
...
} 3< myfile.txt
If you do not want to have a code block then you can associate a descriptor with exec
Code:
exec 3< myfile.txt
...
for demo in 1 2 3
do
  if read line <&3
  then
    echo "line#$demo is $line"
  else
    echo "eof"
  fi
done
 
  


Reply

Tags
sed



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
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 03:41 AM
sed search for a string, duplicate original and replace the string Jykke Programming 17 06-29-2016 10:14 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM

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

All times are GMT -5. The time now is 02:19 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