LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 06-22-2020, 08:10 AM   #1
slac
Member
 
Registered: May 2019
Posts: 265

Rep: Reputation: Disabled
Question How to read line by line a variable in bash? (Slackware Packages)


I'm saving a list of files to a bash variable and I want to read that variable line by line.

The code for saving the file list to a variable is:

Code:
 filelist_number="$((1 + "$(grep -n "FILE LIST:" /var/lib/pkgtools/packages/sc-7.16-x86_64-8 | cut -d : -f 1)"))"
filelist="$(sed -n "${filelist_number},\$p" /var/lib/pkgtools/packages/sc-7.16-x86_64-8)"
echo "${filelist}"
... and now I want to read "${filelist}" line by line. An example just doing "echo" of each line should be sufficient.
 
Old 06-22-2020, 08:27 AM   #2
0XBF
Member
 
Registered: Nov 2018
Distribution: Slackware
Posts: 765

Rep: Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863
It's more of a general bash scripting question than slackware specific but just going off what you posted I think the following would work fine.

Code:
for ITEM in $(sed -n "${filelist_number},\$p" /var/lib/pkgtools/packages/sc-7.16-x86_64-8); do
  echo ${ITEM}
done
 
1 members found this post helpful.
Old 06-22-2020, 09:10 AM   #3
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
I assume filenames in filelist don't include whitespace. Then you can just read the contents of the variable into an array:
Code:
files=($filelist)
for f in ${files[@]}
do echo "$f"
done

Last edited by shruggy; 06-22-2020 at 09:11 AM.
 
1 members found this post helpful.
Old 06-22-2020, 09:10 AM   #4
slac
Member
 
Registered: May 2019
Posts: 265

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by 0XBF View Post
It's more of a general bash scripting question than slackware specific but just going off what you posted I think the following would work fine.

Code:
for ITEM in $(sed -n "${filelist_number},\$p" /var/lib/pkgtools/packages/sc-7.16-x86_64-8); do
  echo ${ITEM}
done
Though I had something different in mind, that solution also worked to do what I wanted. Thanks.
 
Old 06-22-2020, 09:45 AM   #5
slac
Member
 
Registered: May 2019
Posts: 265

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by shruggy View Post
I assume filenames in filelist don't include whitespace. Then you can just read the contents of the variable into an array:
Code:
files=($filelist)
for f in ${files[@]}
do echo "$f"
done
That worked too. Thanks!
 
Old 06-22-2020, 10:29 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
And I think there's an easier way to populate filelist. How about
Code:
filelist=$(sed '0,/^FILE LIST:/d' /var/lib/pkgtools/packages/sc-7.16-x86_64-8)
 
1 members found this post helpful.
Old 06-22-2020, 11:31 AM   #7
slac
Member
 
Registered: May 2019
Posts: 265

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shruggy View Post
And I think there's an easier way to populate filelist. How about
Code:
filelist=$(sed '0,/^FILE LIST:/d' /var/lib/pkgtools/packages/sc-7.16-x86_64-8)
Awesome! Thank you, again!

Do you know if there is a way to do that without deleting the line matched?
 
Old 06-22-2020, 11:41 AM   #8
0XBF
Member
 
Registered: Nov 2018
Distribution: Slackware
Posts: 765

Rep: Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863
This would only print from match to the end. Should do what you want I think.
Code:
sed -n '/^FILE LIST:/,$p' /var/lib/pkgtools/packages/sc-7.16-x86_64-8
 
1 members found this post helpful.
Old 06-22-2020, 11:48 AM   #9
slac
Member
 
Registered: May 2019
Posts: 265

Original Poster
Rep: Reputation: Disabled
Smile

Quote:
Originally Posted by 0XBF View Post
This would only print from match to the end. Should do what you want I think.
Code:
sed -n '/^FILE LIST:/,$p' /var/lib/pkgtools/packages/sc-7.16-x86_64-8
Yes, that is it. Thanks!
 
  


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
[SOLVED] ksh read variable from text file and populate with content of shell variable WindozBytes Programming 4 09-17-2012 01:48 PM
Bash: How do I read data from a variable while that variable is being populated? theaceoffire Programming 4 04-23-2010 02:29 PM
-bash: HISTSIZE: readonly variable -bash: HISTFILESIZE: readonly variable deathsfriend99 Linux - Newbie 4 12-08-2009 12:51 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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