reading specific line from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading specific line from file
# 1  
Old 04-25-2005
reading specific line from file

Hi all...

I not a expert unix script programmer, Kindly adjust.

My requirement is that, i have a file which contains the about 10 lines -
say

1
2
3
...
8 war of the worlds: => text in this line
9 9000,80,78,77,334,445 => this line contains some numbers separted by commas
10

now i know that i have to search for the string "war of the worlds" which I can just grep and get.. but I want the line immediately after this line.


can any body help !!!

thank you in advance

Boss.
# 2  
Old 04-25-2005
Check your man page for the grep command - should have an option to print the line number that the text is in ( -n on my OS)
# 3  
Old 04-25-2005
Thank you for that quick answer.. I tried it out..

and I got the line number as-

linenum=`cat $ROOT/$1/$2 | grep -n "war of worlds:"|cut -d: -f1`;
echo $linenum;
linen=`expr $linenum + 1`;
echo $linen;

now what do i do with this line number.. i am stuck again.. "cat" doesnt have a option to read the file with specified line number.

Actually i want to change the line after this line, say -

war of worlds:
200,300,400

to

war of worlds:
100,200,300,400 => append a 100, to the begining of this line..

Pls show some more light

thank you !!

Boss
# 4  
Old 04-25-2005
You can use sed -n to get the content at a particular line

sed -n '2,3p' filename
# 5  
Old 04-25-2005
Quote:
Originally Posted by cool_boss2121

war of worlds:
200,300,400

to

war of worlds:
100,200,300,400 => append a 100, to the begining of this line..

Pls show some more light

thank you !!

Boss
Code:
echo -e "100,\c" ; sed -n '5 p' file1

# 6  
Old 04-26-2005
sed '/war of worlds/{n;s/^/100,/;}' file1
# 7  
Old 04-26-2005
Thank you everybody.. I tried all of them and they all work.. but the one by Ygor suits me best !! :-)

Thank you !

However, in this line -

sed '/war of worlds/{n;s/^/100,/;}' file1

the number 100 is not fixed. I need to run this shell script recursively and so this number should be the output of a variable, like

sed '/war of worlds/{n;s/^/$3,/;}' file1

But unfortunately, unix doesnt seem to replace the variable with its value and prints $3 instead of the value. Any workarounds ?

Boss
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

2. UNIX for Dummies Questions & Answers

Reading a specific line from a file

Hi All, I am having 100 lines a text file say a.txt. I want read the 'nth' line from that file inside a script. Kindly tell us how to that. (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

3. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

4. Shell Programming and Scripting

Update a specific line in a file while reading sequentially

All, I know this is a very naive question but I could not find a way to get this working! I have a file with values like input.file Value1 Value2 server1/mylogin,mypasswd Value3 Value4 And in my code, I am reading the file line by line and processing it. #! /bin/ksh... (6 Replies)
Discussion started by: bharath.gct
6 Replies

5. UNIX and Linux Applications

Reading a file for specific words

Hi I have a script where the user calls it with arguments like so: ./import.sh -s DNSNAME -d DBNAME I want to check that the database entered is valid by going through a passwd.ds file and checking if the database exists there. If it doesn't, the I need to send a message to my log... (4 Replies)
Discussion started by: ladyAnne
4 Replies

6. Shell Programming and Scripting

Reading data from a specific line in a text file

Hello, I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing # Program gets an input x from user while read line ; do echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies

7. Shell Programming and Scripting

Reading data from a specific line in a text file

hello, I have got the following problem that I am hoping someone can help with please. 1. I have got the following text file (below) , the columns data are 'Test Day', 'Board', 'Betting Number'. TEXT FILE ============================================ 1 3 02-01-27-28-29-30 0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies

8. Shell Programming and Scripting

Reading from a specific line in a loop

Hello All, Request you to let me know how to do the below urgently.. Requirement File A Contains: for i in file A DEV1 DEV5 STG1 STG5 File B Contains: for j in file B DEV1 DEV5 STG1 STG5 (3 Replies)
Discussion started by: kaushikraman
3 Replies

9. UNIX for Dummies Questions & Answers

Reading specific part of file

I have a requirement to go to particular line in the file and from there read the contents till it meets a particular criteria. For eg if the contents of the file is like 81 abcd ------------------- Line 1 82 cdfe ------------------- Line 2 83 dfj ------------------- Line 3 84 df... (5 Replies)
Discussion started by: guptan
5 Replies
Login or Register to Ask a Question