line operation in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting line operation in a file
# 1  
Old 05-26-2008
line operation in a file

Hello all,

I have just started fidling with script. Can anyone please help me out with this question. I have a file that contains the below 4 lines of words

Abcd
Efgh
Ijkl
Mnop

The question is I want to add a line to the middle of these without using sed or awk. Can anyone bail me out please?
# 2  
Old 05-26-2008
Hi,

This is the sample script.

U can edit as per ur requirement.

:>tmp2
echo "Enter the where you want to put new line:"
read line_no
echo "Enter the line you want to append to this file:"
read new_str
var=`cat tmp | wc -l`
i=1
while [ $i -lt $var ]
do
str=`head -$i tmp|tail -1`
if [ $i = $line_no ]
then
echo $new_str >> tmp2
fi
echo $str >> tmp2
i=`expr $i + 1`
done


Regards,
Siba
# 3  
Old 05-26-2008
Quote:
Originally Posted by Musaroro
The question is I want to add a line to the middle of these without using sed or awk.
Is this a homework question?

Regards
# 4  
Old 05-26-2008
Thank you but could you please explain what the head and tail does in the script as I really want to understand rather than assume!!
# 5  
Old 05-26-2008
Have you read the man pages?

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to skip the first line during find and replace operation

Hi Gurus, I did an exhaustive search for finding the script using "sed" to exclude the first line of file during find and replace. The first line in my file is the header names. Thanks for your help.. (4 Replies)
Discussion started by: ks_reddy
4 Replies

2. UNIX for Dummies Questions & Answers

Need help in not fetching a file while file writing operation is not completed

Hi All, We have a Unix program in oracle when we run the program this connects to specified ftp and will get the file into local server. We are facing a problem like when file writing operations is not completed, this program is getting the incomplete file. Could anyone please help me... (2 Replies)
Discussion started by: world.apps
2 Replies

3. Shell Programming and Scripting

File Operation when delimiter is (Ctrl+A)

Hi, I am new to Shell Scripting. I need the following help. I have a file which is Ctrl +A delimited.I have to do the following validation 1. Header row second column should be having the SYSdate time stamp.(ddmmyyyyhhmiss) Need compare only sysdate. Time is not required. 2. ln Footer... (2 Replies)
Discussion started by: gatha_vdm
2 Replies

4. Emergency UNIX and Linux Support

Logic for file copy operation

Hi, i need to copy contents from source to destination with a few conditions, Please helpme out. Sample input file $>cat testfile.txt /a/b/c/d | /e/f/g/d (d can be either a file or directory) my conditions are: check if destination is valid and if its a file or directory if its a... (5 Replies)
Discussion started by: raghu_shekar
5 Replies

5. Shell Programming and Scripting

Column operation : cosne and sine operation

I have a txt file with several columns and i want to peform an operation on two columns and output it to a new txt file . file.txt 900.00000 1 1 1 500.00000 500.00000 100000.000 4 4 1.45257346E-07 899.10834 ... (4 Replies)
Discussion started by: shashi792
4 Replies

6. Shell Programming and Scripting

perform some operation on a specific coulmn starting from a specific line

I have a txt file having rows and coulmns, i want to perform some operation on a specific coulmn starting from a specific line. eg: 50.000000 1 1 1 1000.00000 1000.00000 50000.000 19 19 3.69797533E-07 871.66394 ... (3 Replies)
Discussion started by: shashi792
3 Replies

7. Linux

File read/ write operation

Hi, I am creating a progress bar for file upload for which I have CGI script which copies the data and depending on certain bytes it increments the progress bar. Here, I am writing the incremented value to a file which is read by Ajax at server end. However, here I want to ask that, is it... (18 Replies)
Discussion started by: xs2punit
18 Replies

8. Shell Programming and Scripting

File Operation

I have one text file like 1 filename 2 filename2 3 hi 4 myname i have one variable in which i have index value..i.e.1,2,3 and so... i want to copy value after the index into somevariable..... how i can do it.... (2 Replies)
Discussion started by: AbhijitIT
2 Replies

9. Shell Programming and Scripting

CVS file operation in Perl

Hi All, I have one CVS file, and doing some actions on it. I have removed some columns by SWITCH: { $column eq 'COL1' && do {next;}; $column eq 'COL2' && do {next;}; push(@columns, $column); ... (1 Reply)
Discussion started by: gentleDean
1 Replies

10. UNIX for Dummies Questions & Answers

Simultaneous file read operation

Hi All, I need information on file handling in UNIX enviornment. If in UNIX enviornment I have two process.. P1 P2 Both accessing the same file simultaneosuly(read operation).. Suppose P1 opened the file in read mode first and started reading the file.. Then P2 opens the same file in... (5 Replies)
Discussion started by: chibob
5 Replies
Login or Register to Ask a Question