Get specified line as output from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get specified line as output from a file
# 1  
Old 03-22-2006
Get specified line as output from a file

hi all,
One simple question. How to print a perticular line from a file.

For example if a file "test" has some list of commands as below

ls
chfn
cut
grep
...
...
...


now i have to print 3 rd line of it i.e expected output is to be"cut"..

Can you please help me ...

Thankls in advance,
Chanakya
# 2  
Old 03-22-2006
Code:
sed -n '4q;3p' test

# 3  
Old 03-22-2006
Thanks Vino but can u just explain me if i have to take input from user
# 4  
Old 03-22-2006
I think the solution vino has given is perfect.if you really know the line number then put that line no just before p and line no+1 just before q.
like
sed -n '4q;3p' test.txt

that sounds strange requirement, i guess u r trying to run that command if so
run below script if you just want to print that line replace exec $line with echo "$line".
But i think sed solution is superior.

echo "enter line no"
read x

count=1

while read line
do
if [ $count -eq $x ]
then
exec $line
exit 0
fi
count=`expr $count + 1`
done <test.txt
# 5  
Old 03-22-2006
Hi,
Solution with awk

Quote:
linetoecho=$1
awk -v lte=$linetoecho 'NR==ltd{print}' filename
pass the line to print as a parameter to the script

Gaurav
# 6  
Old 03-22-2006
thank you friends...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. UNIX for Beginners Questions & Answers

Output to file print as single line, not separate line

example of problem: when I echo "$e" >> /home/cogiz/file.txt result prints to file as:AA BB CC I need it to save to file as this:AA BB CC I know it's probably something really simple but any help would be greatly appreciated. Thank You. Cogiz (7 Replies)
Discussion started by: cogiz
7 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

4. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. UNIX for Dummies Questions & Answers

Writing a script that will take the first line from each file and store it in an output file

Hi, I have 1000 files names data1.txt through data1000.txt inside a folder. I want to write a script that will take each first line from the files and write them as output into a new file. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. Shell Programming and Scripting

egrep output each file in new line

I have the following script that searches in several files and shows the search results and the matches filename on the screen. find . -exec egrep -wH "word1|word2" {} \; the output from the search display as: file1 word1 word2 I need to show each file search output result on new... (5 Replies)
Discussion started by: konddor
5 Replies

8. Shell Programming and Scripting

output in another file in one line

Hello, I know this is maybe a stupid question, but I have an output like this: # gensky 1 1 1:00YST +s -a37.24 -o126.58 # Local solar time: 1.80 # Solar altitude and azimuth: -55.1 -47.0 # Ground ambient level: 0.0 void brightfunc skyfunc 2 skybr skybright.cal 0 7 1 0.00e+00 0.00e+00... (3 Replies)
Discussion started by: ergy1983
3 Replies

9. Shell Programming and Scripting

Insert output into file at line number

I need to insert the output of a script into a specific line number of a txt file. I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this. (4 Replies)
Discussion started by: pluto7777
4 Replies

10. Shell Programming and Scripting

print out a line from a output file

I am trying to have a script ping all the clients then output it to a file so I know which clients are off then have the next script pull the ones that are online and reboot them. This is what I am running with right now. If there is something KISS then by all means please let me know. ... (3 Replies)
Discussion started by: deaconf19
3 Replies
Login or Register to Ask a Question