Problem to add the string(without sed & awk) into the middle of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem to add the string(without sed & awk) into the middle of file
# 1  
Old 03-05-2007
Problem to add the string(without sed & awk) into the middle of file

Hi,
I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution.
I first tried by

$echo "paki" >> file

This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or the midd of the file.

Thanks
kind regards,
# 2  
Old 03-05-2007
Problem to add the string(without sed & awk) into the middle of file

Hi,

I have tried many times to add the string into the first line of the file f1 or the middle of the file f1. This file f1 already consists of 10 numbers of lines but could not find the solution.
I first tried by

$echo "paki" >> file

This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or the midd of the file.

Thanks
kind regards,
# 3  
Old 03-05-2007
To add in the beginning

Code:
awk '{ if ( NR == 1 ) { printf "%s\n%s\n", "somestring", $0 } else { print } }' file

To add in the middle,
based on the required line number,
change the code equating to NR
# 4  
Old 03-05-2007
Add string to the first line
Code:
echo "paki" > tmp
cat f1 >> tmp
mv tmp f1

or
Code:
perl -i -ne 'if( $. == 1 ) { print "paki\n",$_; } else { print; } ' f1

Add string to the middle of the file
Code:
lines=$( wc -l <f1 )
half_lines=$( expr $lines / 2 )
head -$half_lines f1 > tmp
echo "paki" >> tmp
(( half_lines=half_lines+1 ))
tail -$half_lines f1 >> tmp
mv tmp f1

or
Code:
lines=$( wc -l <f1 )
half_lines=$( expr $lines / 2 )
perl -i -ne 'if( $. == '$half_lines' ) { print "paki\n"."$_";} else { print; } ' f1

# 5  
Old 03-05-2007
Come on guys, we aren't supposed to be helping with homework. ali hussain, please stop posting homework questions.
# 6  
Old 03-05-2007
ali hussain, this is a warning. Please stop posting homework questions and do not create multiple threads for the same question. Please follow the rules, or you risk getting banned from the site.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

Find the middle character from a string using sed

Input: qwertmyuiop It should print "m". What's the command to do this with sed? (6 Replies)
Discussion started by: cola
6 Replies

4. Shell Programming and Scripting

SED/AWK file read & manipulation

I have large number of data files, close to 300 files, lets say all files are same kind and have extension .dat , each file have mulitple lines in it. There is a unique line in each file containing string 'SERVER'. Right after this line there is another line which contain a string 'DIGIT=0',... (4 Replies)
Discussion started by: sal_tx
4 Replies

5. Shell Programming and Scripting

AWK or SED to add string at specific position

Greetings. I don't have experience programing scripts. I need to insert a string in a specific position of another string on another file (last.cfg), for example: File last.cfg before using script: login_interval=1800 lcs.machinename=client04 File last.cfg after using script:... (4 Replies)
Discussion started by: vanesuke
4 Replies

6. UNIX for Dummies Questions & Answers

Add string to middle of a file

Hi, I want to write a script that takes a file and a string as params and adds the string to the middle line of the file. Also, I want to output the results back to the original file passed without using temp files. I am very much new to UNIX so this is all a little like black magic to me at... (15 Replies)
Discussion started by: Chiefos
15 Replies

7. Shell Programming and Scripting

awk & sed problem

Hello, I am new to shell scripting. I want to optimize my one of the script. I have one file and i want to remove selected zones for domains from that file.In this file i have almost 3500 zones for domains.Sample data for the file.... named.backup... (0 Replies)
Discussion started by: nrbhole
0 Replies

8. Shell Programming and Scripting

add a string in the middle of the file

i want to add a string in a very top of a file without using VI or SED or AWK this is what ive done: (echo '0a'; echo 'LINE OF TEXT'; echo '.'; echo 'wq') | ed -s myfile to add astrng right in the middle i could have count the lines of the file and just chenge the address. ... (6 Replies)
Discussion started by: ciroredz
6 Replies

9. Shell Programming and Scripting

Help needed - Replacing all date & time occurrences in a file with a string using Sed

Hi, I am new to using Sed. I have a file containg lines like the following: INFORM----Test.pc:168:10/11/05 12:34:26 > some text goes here.. TRACE-----Test.pc:197:10/11/05 12:34:26 > some text goes here.. My requirement is to replace 10/11/05 12:34:26 with a string <RUNDATE> (including <... (4 Replies)
Discussion started by: Hema_M
4 Replies

10. UNIX for Dummies Questions & Answers

sed/awk String problem

I would appreciate it if any one can guide me in using awk perhaps sed in extracting some values from a long string. here is an example. .......some lines here........ ........ aaaa bbbb cccc ddddd eeeee fffff gggg (time:hhhh)........ ......some lines here also.......... How can I extract... (2 Replies)
Discussion started by: odogbolu98
2 Replies
Login or Register to Ask a Question