how do I insert argument into TOP of file using vi?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how do I insert argument into TOP of file using vi?
# 1  
Error how do I insert argument into TOP of file using vi?

when directing some text into a file can you choose where it goes like the top of the file (which is text aswell) or the middle??

if so how - especially would like to know how to do so in vi (text editor)


If i were to enter an argument ([text]$1) into a another argument ([file]$2)

would it would be something like

$1 >> $2

?

please help

Last edited by cbkihong; 07-20-2007 at 09:40 PM.. Reason: Remove email (not allowed)
# 2  
I do not fully understand your question.

In any event, if you want to enter a parameter in the unix prompt
and insert it to the top of a file:
Code:
(echo $1;cat input_file) > $$Temp
mv $$Temp input_file

# 3  
using vi i want to make a script that takes 1 argument (some text) and inserts it into a 2nd argument (another file)

I need to know how to insert that text into the top and middle of the file.
the middle being the hardest part
# 4  
You'll need to use wc -l to find the number of lines in the file, use shell builtins or bc to perform some arithmetic and work out where the middle of the file would be, and then use sed's insert command to insert your text. There are many ways to do this sort of thing.

Please let us see a snippet of what you've done so far, and we can provide feedback.

Cheers
ZB
# 5  
the only thing i understood from that was wc.

i just need to know how to put the text like 2 lines in or if there is an automatic way - i.e. a command that can enter text at around 50% of wc.
# 6  
OK, sed's insert command would be hassle here, so awk is probably easiest. "line_file" is your input filename.

Code:
# get midway point - will be a floored integer
$ line_num=$(( $( wc -l < line_file ) / 2 ))
# insert text after that line
$ awk '{if ( NR=='"${line_num}"' ) {printf("%s\n%s\n",$0,"insert this");} else {print}}' line_file > line_file.new && mv line_file.new line_file

Cheers,
ZB
# 7  
cant use sed or awk

i cant use sed or awk so i was thinking of something like using head and tail to split up the file but it aint working quite right, plus it doesnt fit with the notion of adding any argument

head -$N file > f1
tail -$N file > f2
cat f1 #need to echo and cat $1 here# f2 > newfile
mv! newfile file #needs to retain file name

could n be subbed for a variable that halfs the wc -l or something.

if anyone can shed some differing light upon this idea i wud b eternally grateful
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #787
Difficulty: Medium
In early 2019, Bill Gates responded to the public backlash against billionaires by demanding a lower estate tax.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert date/time header at top of file

I'm trying to take mrt output and put it at the top of a file along with the date and time. I was able to do it at the bottom of the file with the following printf "********** $(date) **********\n\n" >> $OUTPUT_PATH/$HOSTNAME mtr -r -w -c 10 $HOSTADDRESS >> $OUTPUT_PATH/$HOSTNAME printf... (2 Replies)
Discussion started by: kramer65
2 Replies

2. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

3. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

4. UNIX for Advanced & Expert Users

Insert string in binary file at top

How can i append a EBCDIC string of 100 bytes to 0th position of a binary file in UNIX. (4 Replies)
Discussion started by: param_it
4 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with -

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

7. Shell Programming and Scripting

best way to insert a line at the top of a file?

say I want to insert "this is a test" as the first line into file A, besides echo "this is a test" > /tmp/tmpfile cat /tmp/tmpfile fileA >> /tmp/result, is there any simple way I can do it? thanks (7 Replies)
Discussion started by: fedora
7 Replies

8. UNIX for Dummies Questions & Answers

Inserting argument into top of a file

Hi, I am new to Unix, and I am trying to append a line of argument into a current file. I need this line to be inserted into the very top of the file. Does anyone know how this is done? For example, I am trying: echo "insert to top" >> filename. This inserts the line at the bottom of the... (7 Replies)
Discussion started by: Dev06
7 Replies

9. Shell Programming and Scripting

insert text into top of file

how would you insert text into a existing file using aguments first arguments being the line of text and the second argument being file name (1 Reply)
Discussion started by: jimbob
1 Replies

10. Shell Programming and Scripting

SED- Insert text at top of file

Does anyone know how to insert text at the top and bottom of a file using sed? (12 Replies)
Discussion started by: MBGPS
12 Replies

Featured Tech Videos