LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 12-09-2004, 06:33 PM   #1
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Rep: Reputation: 30
Question bash read a given line number from text


I'm writing a bash script that reads entries from a fext file (kinda like a database)
The script is scanning in directories off of the text file.

I tried to use this but I this don't work with awk.
Code:
DCOUNT=1
      DCNT=cont
      
      while [ $DCNT != no ]
      do
         D[$DCOUNT]=`awk '{ printf $'$DCOUNT' }' data/dirlist.dat`  
	 # DOEASN'T WORK WITH SPACES. NEEDS SOME OTHER WAY OF RESTORING THE DIRECTORIES.
	 if [ ! "'${D[$DCOUNT]}'" ];
         then
            DCNT=no
         else
            echo TEST
         fi
      done
I tried using cat and grep, but they read in all the lines.
Is there a way to get either of these to only read in a specified line or another program similar that would do this?

THANKS IN ADVANCE
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-09-2004, 07:58 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
The sed command for printing line 52 is:

Code:
sed -n '52p' (file)
(taken from Handy one-liners for sed). There are many other ways to do it, but this way is short and sweet.
 
2 members found this post helpful.
Old 12-09-2004, 08:00 PM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
I'm a little unsure what you're trying so forgive me if this isn't what you need.

To pick line 4 out of a file foo.txt, you can do it this way

line4="`sed -n 4p foo.txt`"
 
1 members found this post helpful.
Old 12-09-2004, 09:05 PM   #4
bendeco13
Member
 
Registered: Oct 2004
Distribution: Fedora 7
Posts: 232

Original Poster
Rep: Reputation: 30
Thanks!!
That Works
 
Old 08-31-2012, 12:23 PM   #5
figure20012
Member
 
Registered: Apr 2012
Posts: 75

Rep: Reputation: Disabled
Quote:
Originally Posted by randyding View Post
I'm a little unsure what you're trying so forgive me if this isn't what you need.

To pick line 4 out of a file foo.txt, you can do it this way

line4="`sed -n 4p foo.txt`"
instead of 4 can i pass a variable let say $linenum
please help
 
Old 08-31-2012, 12:49 PM   #6
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Of course, yes. The only catch is you have to surround the variable name with { } to distinguish it from the alphanumeric character immediately following.

Code:
sed -n ${linenum}p foo.txt
 
Old 08-31-2012, 12:54 PM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Code:
line = $(sed -n "$linenum{p;q;}")
The q here tells sed to quit immediately after the print command -- we do not need to iterate through the rest of the file.
 
2 members found this post helpful.
Old 08-31-2012, 03:49 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
There are any number of ways to print out a specific line number from a file. The sed way above is one. Here are a few more:

Code:
inputfile=yourfile.txt
lineno=10

awk "NR==$lineno" "$inputfile"

echo "${lineno}p" | ed -s "$inputfile"

cut -d $'\n' -f "$lineno" <"$inputfile"

head -n "$lineno" "$inputfile" | tail -n 1

mapfile -t -O 1 lines <"$inputfile"
echo "${lines[lineno]}"

n=1
while read line; do
	case $n in
		$lineno) echo "$line" && break ;;
		      *) : $(( n++ )) && continue ;;
	esac
done <"$inputfile"
The last two are pure shell versions. mapfile requires bash v.4+ and is not recommended for large files (since the contents get stored in RAM), but is otherwise an efficient solution.

The cut solution uses ansi-c style quoting to set the delimiter to newline. This is only available in modern shells like bash or ksh. You can use a literal newline instead if portability is a concern.
 
2 members found this post helpful.
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 06:07 PM
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
bash: read a line from file zokik Linux - General 6 12-10-2008 09:24 AM
Read in an Octal number from a text file using C++ pjordan Programming 2 11-18-2004 03:03 PM
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 > Non-*NIX Forums > Programming

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