Inputting text to a specific line of a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inputting text to a specific line of a file
# 1  
Old 06-30-2010
Inputting text to a specific line of a file

Hi all, I have a script which uses a basic line to add text into another file e.g.

Code:
grep -i test * >> test.txt

Is there a way I can get the output of the grep to output to a specific line in the text.txt for example output above the line starting "Bottom line..."
JayC89
# 2  
Old 06-30-2010
can u explain me with a little bit of more examples. what i am understanding here is.. grep the name of all the files having the name test in their name and put all those file in test.txt.

now what i dont understand exactly is
Quote:
output of the grep to output to a specific line in the text.txt for example output above the line starting "Bottom line..."
can you explain me what are you trying to do here.
# 3  
Old 06-30-2010
Something like this?
Code:
awk '/Bottom line/{system("grep -i test *")}1' test.txt > newfile
mv newfile test.txt


Last edited by Franklin52; 06-30-2010 at 08:21 AM.. Reason: Typo, forgot closing brace
# 4  
Old 06-30-2010
Thanks for get replies guys.

Quote:
Originally Posted by dazdseg
can u explain me with a little bit of more examples. what i am understanding here is.. grep the name of all the files having the name test in their name and put all those file in test.txt.

now what i dont understand exactly is


can you explain me what are you trying to do here.
Say the last line of the file starts with;

"This is the end of the file..."

I want the output of my grep to be inserted into the file above the last line - if that makes sense?

Quote:
Originally Posted by Franklin52
Something like this?
Code:
awk '/Bottom line/{system("grep -i test *")1' test.txt > newfile
mv newfile test.txt

I just tried running that Frankin and get an error;
Code:
bash-2.05$ awk '/Bottom line/{system("grep -i test *")1' test.txt > newfile
awk: syntax error near line 1
awk: illegal statement near line 1

Any ideas? This is on Solaris10 if it helps Smilie
JayC89
# 5  
Old 06-30-2010
Quote:
Originally Posted by JayC89
I just tried running that Frankin and get an error;
Code:
bash-2.05$ awk '/Bottom line/{system("grep -i test *")1' test.txt > newfile
awk: syntax error near line 1
awk: illegal statement near line 1

Any ideas? This is on Solaris10 if it helps Smilie
Sorry, I had a typo in the code:
Code:
awk '/Bottom line/{system("grep -i test *")}1' test.txt > newfile
mv newfile test.txt

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards

Last edited by Franklin52; 06-30-2010 at 08:19 AM.. Reason: Typo
This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 06-30-2010
Thank you Smilie
JayC89
# 7  
Old 07-02-2010
Just a crappy shell solution. I don't know if this is what you were looking for. It adds a line before the line that says last


Code:
(03:19:41\[D@DeCoBox15)
[~]$ cat file
first
second
third
fourth
fith
last

(03:19:45\[D@DeCoBox15)
[~]$ type insert
insert is a function
insert ()
{
    tac $1 | while read line; do
        if [[ $line == "last" ]]; then
            echo $line;
            echo "$2";
        else
            echo $line;
        fi;
    done | tac
}

(03:19:48\[D@DeCoBox15)
[~]$ insert file "look ma, new text"
first
second
third
fourth
fith
look ma, new text
last


I just put it in a function so type would auto format it from the one-liner I wrote it as.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. Shell Programming and Scripting

Update specific field in a line of text file

I have a text file like this: subject1:LecturerA:10 subject2:LecturerA:40 if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example. The... (6 Replies)
Discussion started by: bmtoan
6 Replies

3. Shell Programming and Scripting

how read specific line in a file and write it in a new text file?

I have list of files in a directory 'dir'. Each file is of type HTML. I need to read each file and get the string which starts with 'http' and write them in a new text file. How can i do this shell scripting? file1.html <head> <url>http://www.google.com</url> </head> file2.html <head>... (6 Replies)
Discussion started by: vel4ever
6 Replies

4. Linux

Get a specific line number from a text file

Hello! All, Could you please tell me how to get a specific line number from a text file? For example below, ABC DEF ---> Get this line number, return to an variable GHI My OS is Linux. Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

8. Shell Programming and Scripting

Modify Specific Line of a Text File

Given a text file, how do you add a line of text after a specific line number? I believe I would want to use "sed" but I am unsure of the syntax. Thank you. Mike (5 Replies)
Discussion started by: msb65
5 Replies

9. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

10. Programming

jumping to a specific line in a text file

hi everybody! i need to read a specific line from a text file using C. can any one suggest how to do it. i m aware abt fread(), fwrite(), fseek()... but using these allows the pointer to be moved 1 character at a time. Is there a way i could jump directly to a line if i know the line number?... (4 Replies)
Discussion started by: mridula
4 Replies
Login or Register to Ask a Question