LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2007, 05:55 PM   #1
baks
Member
 
Registered: Feb 2007
Posts: 41

Rep: Reputation: 15
BASH append to middle of file


HI, Im new to this forum and would like some help. I have created the following script which takes two arguments from the commandline and inputs the text from the first argument into the top line of the second argument(a file).

INPUT=$1
FIRSTLINEF=$2
TEMPFILE=TEMP

echo $INPUT > $TEMPFILE # User input will be placed in the temp file
cat $FIRSTLINEF >> $TEMPFILE # Contents of original file will be added
mv -f TEMP firstlinefile # Temp file will replace original file

I just have one question. How would I place the text entered at the commandline into the middle of a file. How would I manage it ?

I know I need to count the lines in the file and then use another temp file but Im completely lost.

I also cant use SED or AWK.

Thank You in advance.
 
Old 02-26-2007, 06:03 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683Reputation: 683
Why don't you want to use sed or awk?

You could look into using the wc command to count lines, and the head and tail commands to disect a file on a certain line.
 
Old 02-26-2007, 06:58 PM   #3
jr1
LQ Newbie
 
Registered: Feb 2007
Distribution: mostly Debian
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by baks
echo $INPUT > $TEMPFILE # User input will be placed in the temp file
cat $FIRSTLINEF >> $TEMPFILE # Contents of original file will be added
mv -f TEMP firstlinefile # Temp file will replace original file
I'd replace that with this
Code:
echo $INPUT | cat - $FIRSTLINEF > $OUTFILE
Quote:
I just have one question. How would I place the text entered at the commandline into the middle of a file. How would I manage it ?

I know I need to count the lines in the file and then use another temp file but Im completely lost.

I also cant use SED or AWK.
Consider using perl or awk or sed or even ed. Otherwise, maybe something like this (as long as you're using bash)
Code:
echo $INPUT | cat <(head -$N $FILE) - <(tail -n +$((N+1)) $FILE) > $OUTFILE
Or, for a pure shell-based approach you could use a while loop or for loop. One example:
Code:
i=1; cat $FILE | while read line
do
    echo $line
    [ $i -eq $N ] && echo $INPUT
    i=$((i+1))
done > $OUTFILE; unset i

Last edited by jr1; 02-26-2007 at 07:03 PM.
 
Old 02-27-2007, 05:40 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Thumbs up

Quote:
Originally Posted by baks
HI, Im new to this forum and would like some help. I have created the following script which takes two arguments from the commandline and inputs the text from the first argument into the top line of the second argument(a file).

INPUT=$1
FIRSTLINEF=$2
TEMPFILE=TEMP

echo $INPUT > $TEMPFILE # User input will be placed in the temp file
cat $FIRSTLINEF >> $TEMPFILE # Contents of original file will be added
mv -f TEMP firstlinefile # Temp file will replace original file

I just have one question. How would I place the text entered at the commandline into the middle of a file. How would I manage it ?
What do you mean by "middle"? Do you mean the exact middle of the file, and, if so, do you mean by character or by line?

Quote:
I know I need to count the lines in the file and then use another temp file but Im completely lost.

I also cant use SED or AWK.
Why not? They are the logical tools to use.

To put something in the middle of the file, counting bytes rather than lines:

Code:
middle=$(( $( wc -c < FILENAME ) / 2 ))
{
  dd bs=$middle count=1 2>/dev/null
  printf "%s" "$INPUT"
  cat
} < FILENAME > TEMPFILE
To put it in the middle by line:

Code:
middle=$(( $( wc -l < FILENAME ) / 2 ))
n=0
{
  while [ $n -le $middle ]
  do
    IFS= read -r line
    printf "%s\n" "$line"
    n=$(( $n + 1 ))
  done
  printf "%s\n" "$INPUT"
  cat
} < FILENAME > TEMPFILE
 
Old 02-28-2007, 03:27 AM   #5
baks
Member
 
Registered: Feb 2007
Posts: 41

Original Poster
Rep: Reputation: 15
I have managed to complete the task. I used the head and tail command together with count to be able to determine the number of lines and then used a temporary file.

I was not allowed to use SED or AWK, dont really know why.

Thank you everyone for helping me.

Last edited by baks; 02-28-2007 at 03:28 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 09:41 AM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM
[Perl] append to new file noir911 Programming 5 02-08-2007 06:38 AM
bash help? append text to textfile entries embsupafly Programming 9 08-07-2005 07:01 AM
append to file d-rez Linux - Newbie 2 06-20-2002 04:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration