stuck and confused


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers stuck and confused
# 1  
Old 09-05-2007
stuck and confused

#!/bin/bash
echo $1 | cat - $2 >> /tmp/$$ && mv /tmp/$$ $2

im trying to get the first argument to go in the middle of the second argument which is a file, anyone any ideas. i have only managed to get it to go on the end or the front.

been fiddling about with wc -l, i get the number of lines but not sure how i use that to insert the argument
# 2  
Old 09-06-2007
Quote:
Originally Posted by iago
#!/bin/bash
echo $1 | cat - $2 >> /tmp/$$ && mv /tmp/$$ $2

im trying to get the first argument to go in the middle of the second argument which is a file, anyone any ideas. i have only managed to get it to go on the end or the front.

been fiddling about with wc -l, i get the number of lines but not sure how i use that to insert the argument

It is not clear what you want.

What do you want to insert?

Where do you want to insert it?

What is $1? Is it text? Is it a filename?

(Note that cat is for concatenating files, not inserting things into them.)
# 3  
Old 09-06-2007
What do you want to insert?
I want to insert the first argument, which will be a line of text

Where do you want to insert it?
In the middle of the second argument which is a text file


What is $1? Is it text? Is it a filename?
It is text
# 4  
Old 09-06-2007
Quote:
Originally Posted by iago
What do you want to insert?
I want to insert the first argument, which will be a line of text

Where do you want to insert it?
In the middle of the second argument which is a text file

I assume that you do not want it in the middle of the second argument, but in the middle of the file pointed to by the second argument.

If that is the case, you need to find out how many lines are in the file, use head to read that number of lines; insert the text; use tail to get the rest of the file, or use awk.

Code:
lines=$( wc -l < "$2" )
middle=$(( $lines / 2 ))

awk -v mid=$middle -v text="$1" '
   NR == mid { print text }
             { print }' "$2" > tempfile

# 5  
Old 09-06-2007
can it be done without using sed or awk?
# 6  
Old 09-06-2007
Quote:
Originally Posted by iago
can it be done without using sed or awk?


Using head and tail:
Code:
lines=$( wc -l < "$2" )
middle=$(( $lines / 2 ))

n=0
{
   head -n "$middle" "$2"
   printf "%s\n" "$1"
   tail -n "$(( $middle + 1 ))" "$2"
} > tempfile

Without any external commands, it will be very slow on a long file:
Code:
lines=$( wc -l < "$2" )
middle=$(( $lines / 2 ))

n=0
while IFS= read -r line
do
   n=$(( $n + 1 ))
   printf "%s\n" "$line"
   [ $n -eq $middle ] && printf "%s\n" "$1"
done < "$2" > tempfile

Somewhat faster (it uses cat for the second half of the file):
Code:
while IFS= read -r line
do
   n=$(( $n + 1 ))
   printf "%s\n" "$line"
   [ $n -eq $middle ] && { printf "%s\n" "$1"; cat; exit; }
done < "$2"


Last edited by cfajohnson; 09-06-2007 at 06:13 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confused with if/then

Hi All, I'm pretty new to this so please bear with me... I'm trying to write a bash script to first search in a file for a string of characters; if the characters exist than skip the rest of the code until you get to the last line and run that command /sbdin/ldconfig; if the string doesn't... (2 Replies)
Discussion started by: gmdune
2 Replies

2. Ubuntu

New and Confused

Hello, I am having a problem with Dual Booting Windows XP Pro and Linux Mint. I have Three Hard Drives, One Hard Drive has Linux Mint Loaded on it. When it is hooked up to the computer by itself it works great. This is an IDE Drive. The Second Hard Drive has Window XP Pro loaded on it.... (3 Replies)
Discussion started by: Forextrading
3 Replies

3. Solaris

Confused c#t#d#s#

I am confused c#t#d#s# once I learn the following : slice 0 ...... 0 to 2520 slice 1....... 2521 to 2840 slice 6........2841 to 8891 slice 2........0 to 8891 really really confused. Please explain. (8 Replies)
Discussion started by: deltakutty
8 Replies

4. Shell Programming and Scripting

sed very confused

Hello experts, I have this complicated code that output value in between pattern and add "a string" to the front of the output. The problems I have many pattern that are the same. so I want to know how to get the value between 1st pattern, 2nd pattern etc. Any suggestions? sed -n... (14 Replies)
Discussion started by: minifish
14 Replies

5. UNIX for Dummies Questions & Answers

confused

A red hat linux ftp server exists in which a file exists. My problem is I need to connect to this server from my windows xp terminal which is in the same network & retrieve the file then convert it to xcel for some data Pls advs commands and procedure to connect to the machine...oh my god... (1 Reply)
Discussion started by: sauravjung
1 Replies

6. UNIX for Dummies Questions & Answers

confused on ls -l output

Here is my output from ls -l: drwxr-xr-x 2 username username 4096 2005-12-27 09:51 bin drwxr-xr-x 2 username username 4096 2006-01-30 13:03 Desktop drwxr-xr-x 1 root root 4096 2006-01-30 09:13 somedirectory drwxr-xr-x 6 username username 4096 2005-12-15 08:09 mystuff drwxr-xr-x ... (2 Replies)
Discussion started by: outtacontrol
2 Replies

7. Shell Programming and Scripting

confused with cp

may i know what cp $1 $2 $0 $2 does? (12 Replies)
Discussion started by: C|[anti-trust]
12 Replies

8. UNIX for Dummies Questions & Answers

confused

hi! how when i'm chattin inside com there was this chatter andi don't know what he did but he saw all my files inside my shell. what did he do? (4 Replies)
Discussion started by: hapiworm
4 Replies

9. UNIX for Dummies Questions & Answers

confused,,,,

Hi,,, is there any possibility to install Linux in my P.C which is use Win98 without loose anything from my hard disk???? ---------------------------------------------------------------------------------- Is it better for a newbie in this kind of OS to install Linux instead of... (5 Replies)
Discussion started by: spyros
5 Replies
Login or Register to Ask a Question