Appending to the first line of a document

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending to the first line of a document
# 1  
Old 02-17-2008
Appending to the first line of a document

Hi all, I'm new to the forums and a total beginner with Unix. I've been given the seemingly simple task of writing a script that asks for two arguments: the first argument being a line of text, the second argument being a document. The script has to take the first argument of text and append it to the specified document in the second argument (no problem), but then the text must be appended to the first line of the document (without using SED or AWK) and I'm confused as to what command I use to do this.

I thought one way around this would be to CAT the first arg and then the second, then save the output to the file noted in the second arg, but because the input and output sources are the same in the second arg, shell doesnt want to know. I have a feeling there is something so simple that I'm missing, but going back over my notes, I can't seem to find what it is that I should know here? Any thoughts?

Perhaps there some way to run VI or EX commands from the shell so that I can paste the input from the first arg into the specified document?

Would really appreciate some advice here! Many thanks.
# 2  
Old 02-17-2008
Originally Posted by oldhoi
Hi all, I'm new to the forums and a total beginner with Unix.
Would really appreciate some advice here! Many thanks.
here's the advice
# 3  
Old 02-17-2008
Thanks...but what should I be looking at here?
# 4  
Old 02-17-2008
Sorry, but "no awk or sed" is obviously a homework assignment. I hate being a stickler for rules, but there's a rule on this board saying don't post such questions. It's for the sake of (a) your fellow students, and (b) others on here who are professionals and need this board for their work. Or something like that.

Having said that, what happens with the following code?

( cat file.txt; echo "Middle Line"; cat file.txt; ) >output.txt

# 5  
Old 02-17-2008
Sorry otheus, I didn't realise. Thanks for the tip anyhow, I'll give it a try.
# 6  
Old 02-18-2008
shell without sed and awk


I am not sure whether i understood your repuirements totally.
If so, below should be ok for you.

echo $1 > temp
cat $2 >> temp

test file(a.txt):
This is the second line.
This is the third line.

Code: "This is the first line" a.txt

This is the first line
This is the second line.
This is the third line.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove single-line breaks only in document

Regarding copy/pasted text of copyright-free book from (link below), in attempt to expand single-line-break paragraph text (not section headings or paragraph breaks) to wider right margin, Justify or Wrap in LIbreOffice is not working, and Find/Replace the paragraph mark ($) wraps all... (2 Replies)
Discussion started by: p1ne
2 Replies

2. UNIX for Dummies Questions & Answers

Script to delete files line by line in .txt document

Friends, I am in need to delete files from a directory on a regular basis. I want to place all the files to be deleted in delete .txt file and require a script to delete files, line by line from this delete.txt file. Please help me with this script as am new to unix scripting. (3 Replies)
Discussion started by: fop4658
3 Replies

3. UNIX for Advanced & Expert Users

Issuing a Here Document as a Single Line Command

How can I run a here document on just one line? I ask, because I need to issue it from C++ as a system() or similar command and for security reasons I don't want to write out a shell script file from the program and run it. For example, how could I write: passwd test <<EOF n3wp3ss... (3 Replies)
Discussion started by: BrandonShw
3 Replies

4. UNIX for Dummies Questions & Answers

Parse out part of line in text document

I have an XML file that I am attempting to create a new text document from it with only one of the pieces of data printed. Here's an example: Source: <?xml version="1.0"?> <Reply Success="TRUE"> <ProfileList NrOfProfiles.DWD="645"> <Profile_0 Name="Test" Description="A... (3 Replies)
Discussion started by: Davinator
3 Replies

5. Shell Programming and Scripting

Appending the first word of each line to the end of each line

Hi Experts, Am relatively new to shell programming so would appreciate some help in this regard. I am looking at reading from a file, line by line, picking the first word of each line and appending it to the end of the line. Any suggestions? INPUT FILE - 3735051 :... (7 Replies)
Discussion started by: hj007
7 Replies

6. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

7. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

8. Shell Programming and Scripting

Appending a line in a file after a particular line

Hello, I have got a C file in which I would like to add an include statement of my own. There are already a few include statements and mine should come right after the last existing one (to be neat). With grep I can get the lines containing the word 'include' and I guess I should feed the... (7 Replies)
Discussion started by: maxvirrozeito
7 Replies

9. Shell Programming and Scripting

Appending line ending with '}" to new line

Hello masters. I have a rather simple problem but its been killing me. I have a file "x" with only 1 line inside it. The line looks something like Now this is only part of the line. Its actually about 4000 characters. What i need to do is whenever there is a "}", i need to append the next... (4 Replies)
Discussion started by: aismann
4 Replies

10. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies
Login or Register to Ask a Question