Append string to first line of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append string to first line of a file
# 1  
Old 07-06-2011
Append string to first line of a file

Hi,

Please suggest me to write unix command, HEADER20110101 string append to first line of a file..

Regards
Akshu
# 2  
Old 07-06-2011
i am adding 123 in the first line of the file called test
Code:
 
 
$ nawk ' { if (NR==1){printf("123%s\n",$0)} else {print} }' test 
123abc
abc
abc
abc
abc

$ cat test
abc
abc
abc
abc
abc

---------- Post updated at 11:50 AM ---------- Previous update was at 11:48 AM ----------

using sed

Code:
 
$ sed ' 1 s/.*/123&/' test
123abc
abc
abc
abc
abc

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-06-2011
Alternate awk..
Code:
awk 'NR==1{$0="header\n"$0}1' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 07-06-2011
Hi,

Try this:
Code:
sed -i '1s/.*/ HEADER20110101&/' a.txt


Last edited by Franklin52; 07-06-2011 at 02:17 PM.. Reason: Please use code tags, thank you
This User Gave Thanks to gprashant For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pre-append a variable string to each line in a file?

How to pre-append a variable string to each line in a file contains both single and double quotes? The variable string has no quotes in it. thank you very much. :confused: (8 Replies)
Discussion started by: dtdt
8 Replies

2. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

3. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

4. Shell Programming and Scripting

Append string at start of line

Hi, I want to append # at the start of line wherever keyword xyz is found through stream editor? Is it possible? (18 Replies)
Discussion started by: db2cap
18 Replies

5. Shell Programming and Scripting

how to append a string to next line in perl

hi all , i have a requirement like this.. this just a sample script... $ cat test.sh #!/bin/bash perl -e ' open(IN,"addrss"); open(out,">>addrss"); @newval; while (<IN>) { @col_val=split(/:/); if ($.==1) { for($i=0;$i<=$#col_val;$i++) { ... (2 Replies)
Discussion started by: tprayush
2 Replies

6. Shell Programming and Scripting

Append the end of each line in a file with a given string

Hi friends, I have a file containing many lines as follows. M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\ADBasicView.java@@\main\v6.4.0_pit_a I would like to append the string "\0" at the end of each line in the file. The output should look... (10 Replies)
Discussion started by: nmattam
10 Replies

7. Shell Programming and Scripting

Append a string at the end of every line in a file

Hi Friends, I have a file with many lines as shown below. /START SAMPLE LINE/ M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\port\Post.java@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\switchview\View.java@@\main\v6.4.0_pit_a /END SAMPLE LINE/ I... (1 Reply)
Discussion started by: nmattam
1 Replies

8. Shell Programming and Scripting

How can I append a string at the end of a line in a file

Hi, guys. I have one question: I have a file called "group", the contents of it is below: ******************************** ... test:x:203: sales:x:204: repair:x:205: research:x:206:brownj ... *********** Now I want to add string ",sherrys" at the end of "research:x:206:brownj", so... (5 Replies)
Discussion started by: daikeyang
5 Replies

9. Shell Programming and Scripting

append string with spaces to a line

hi i have a file like (every string contains 16 chars) CTL1330000000000 0000 00 008000 0080000000 i need to form a line and write to a file CTL13300000000000000 00008000 0080000000 total chars should be 64 ... (2 Replies)
Discussion started by: Satyak
2 Replies

10. Shell Programming and Scripting

line contains a string write to new file otherwise append

hi i have the following function, reading file line by line, if line contains "Change state" i need to cut some fields otherwise i need to append the total line to the new file function parse() { while read LINE do echo $LINE |grep -q "Change state" if then echo $LINE |grep -q... (1 Reply)
Discussion started by: Satyak
1 Replies
Login or Register to Ask a Question