Append file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append file
# 1  
Old 09-23-2014
Append file

Legends,

I need to append the file. please help me out.

Current contents are
Code:
/tmp/a.txt

#
e11.Mer.out.host1              "fun.tp"                   read
e11.Mer.out.host2              "fun1.tp"                   read
e11.Mer.out.host3              "fun2.tp"                   read
e11.Mer.out.host4              "fun3.tp"                   read
e11.Mer.out.host5              "fun3.tp"                   read
#

Now i want to add additional line as below

Code:
#
e11.Mer.out.host1              "fun.tp"                   read
e11.Mer.out.host2              "fun1.tp"                   read
e11.Mer.out.host3              "fun2.tp"                   read
e11.Mer.out.host4              "fun3.tp"                   read
e11.Mer.out.host5              "fun4.tp"                   read
e11.Mer.out.host6              "fun5.tp"                   read
#
e12.Mer.out.host1              "fun.tp"                   read
e12.Mer.out.host2              "fun1.tp"                   read
e12.Mer.out.host3              "fun2.tp"                   read
e12.Mer.out.host4              "fun3.tp"                   read
e12.Mer.out.host5              "fun4.tp"                   read
e12.Mer.out.host6              "fun5.tp"                   read
#

# 2  
Old 09-23-2014
Hi,

To do something like this you just have to edit the file, not sure what editor you'll have but you can use Vi, Vim, Emacs, Gedit or any number of editors to do that.

Regards

Dave
# 3  
Old 09-23-2014
Quote:
Originally Posted by sdosanjh
Legends,

I need to append the file. please help me out.

Current contents are
Code:
/tmp/a.txt
 
#
e11.Mer.out.host1              "fun.tp"                   read
e11.Mer.out.host2              "fun1.tp"                   read
e11.Mer.out.host3              "fun2.tp"                   read
e11.Mer.out.host4              "fun3.tp"                   read
e11.Mer.out.host5              "fun3.tp"                   read
#

Now i want to add additional line as below

Code:
#
e11.Mer.out.host1              "fun.tp"                   read
e11.Mer.out.host2              "fun1.tp"                   read
e11.Mer.out.host3              "fun2.tp"                   read
e11.Mer.out.host4              "fun3.tp"                   read
e11.Mer.out.host5              "fun4.tp"                   read
e11.Mer.out.host6              "fun5.tp"                   read
#
e12.Mer.out.host1              "fun.tp"                   read
e12.Mer.out.host2              "fun1.tp"                   read
e12.Mer.out.host3              "fun2.tp"                   read
e12.Mer.out.host4              "fun3.tp"                   read
e12.Mer.out.host5              "fun4.tp"                   read
e12.Mer.out.host6              "fun5.tp"                   read
#

Hello,

Following may help in same.

Code:
awk '/^#/ {a++;print $0;getline;u=$0;match($1,/.*host/);c=substr($1,RSTART,RLENGTH);match($2,/.*\./);e=substr($2,RSTART,RLENGTH-1);match($2,/\..*/);f=substr($2,RSTART,RLENGTH);$1=$2="";V=$0;print u} !/^# && a/ {b++} {if(b>1 &&a==1){print $0}} {if(b==5){a=1;b;c=c b;e=e b f;print c OFS e OFS V;c=V=b=e=""}}' FS=" " OFS="\t"  Input_file

Output will be as follows.
Code:
#
e11.Mer.out.host1              "fun.tp"                   read
e11.Mer.out.host2              "fun1.tp"                   read
e11.Mer.out.host3              "fun2.tp"                   read
e11.Mer.out.host4              "fun3.tp"                   read
e11.Mer.out.host5              "fun3.tp"                   read
e11.Mer.out.host5       "fun5.tp"                       read
#

Thanks
R. Singh

Last edited by RavinderSingh13; 09-23-2014 at 10:29 AM.. Reason: Added output
# 4  
Old 09-23-2014
If you want to add the same Mer.out.host6 "fun5.tp" read everytime with first value different, you can use below
Code:
awk 'NF > 1 {p=$1; print; next} NR > 1{print p, "Mer.out.host6 \"fun5.tp\" read"}1' FS=. OFS=. /tmp/a.txt


Last edited by SriniShoo; 09-23-2014 at 06:09 AM.. Reason: Change tags from ICODE to CODE
# 5  
Old 09-23-2014
Code:
awk 'FNR==1{gsub(/,/,OFS,latest)}/^#/ && p==3{print latest }{p=NF; $1=$1}1' OFS='\t' latest='e12.Mer.out.host6,"fun5.tp",read'  file

# 6  
Old 09-24-2014
Quote:
Originally Posted by SriniShoo
If you want to add the same Mer.out.host6 "fun5.tp" read everytime with first value different, you can use below
Code:
awk 'NF > 1 {p=$1; print; next} NR > 1{print p, "Mer.out.host6 \"fun5.tp\" read"}1' FS=. OFS=. /tmp/a.txt


Thx Srini,
but it is giving the syntax error

Code:
awk: syntax error near line 1
awk: bailing out near line 1

# 7  
Old 09-24-2014
Quote:
Originally Posted by Akshay Hegde
Code:
awk 'FNR==1{gsub(/,/,OFS,latest)}/^#/ && p==3{print latest }{p=NF; $1=$1}1' OFS='\t' latest='e12.Mer.out.host6,"fun5.tp",read'  file

Thx Akshay,
but i am getting syntax error

Code:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Want it to read the file name and then append date stamp at the end of file?

I was thinking something like for i in `find . -name "*.log.Z"`; do mv $i name.log.Z or something like that? (3 Replies)
Discussion started by: xgringo
3 Replies

3. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

4. UNIX for Dummies Questions & Answers

How to append portion of a file content to another file when a certain pattern is matching?

Hi ladies and gentleman.. I have two text file with me. I need to replace one of the file content to another file if one both files have a matching pattern. Example: text1.txt: ABCD 1234567,HELLO_WORLDA,HELLO_WORLDB DCBA 3456789,HELLO_WORLDE,HELLO_WORLDF text2.txt: XXXX,ABCD... (25 Replies)
Discussion started by: bananamen
25 Replies

5. Shell Programming and Scripting

How to search and append words in the same file using unix scripting file operations

Hi , I have a file myhost.txt which contains below, 127.0.0.1 localhost 1.17.1.5 atrpx958 11.17.10.11 atrpx958zone nsybhost I need to append words only after "atrpx958" like 'myhost' and 'libhost' and not after atrpx958zone. How to search the word atrpx958(which is hostname) only,... (5 Replies)
Discussion started by: gsreeni
5 Replies

6. Shell Programming and Scripting

Unix Script file to Append Characters before rows in file.

Hi Experts, I am working on HP-UX. I am new to shell scripting. I would like to have a shell script which will prefix: 1. "H|" before first row of my file and, 2. "T" for all other rows of the file. For Example - File before running the script 20100430|4123451810|218.50|TC 20100430 ... (4 Replies)
Discussion started by: phani333
4 Replies

7. UNIX for Dummies Questions & Answers

How can I append a text at end of file after displaying the file

I have a file "sample.txt" with the content as below: Hi This is a Sample Text. I need a single command using cat which serve the following purpose. 1.display the contents of sample.txt 2.append some text to it 3. and then exit But, all should be served by a sinle command.:confused: (1 Reply)
Discussion started by: g.ashok
1 Replies

8. Solaris

gzip a file and append creation date stamp to file

I want to gzip a file and append the creation date to the end of the file. How can I accomplish this task. Basically they are log files which need a creation date stamp appended to make sure they do not overwrite other log files. -jack (3 Replies)
Discussion started by: jacktravine
3 Replies

9. UNIX for Dummies Questions & Answers

Trying to empty file using > but the file size increasing when next append

AIX 5.3 / KSH I have a Java application which creates a log file a.log. I have a KSH script which does the following action cp a.log /directory2/b.log > a.log After this the file size goes to 0 as per "ls -l" Then next time when the application writes into this file, the file size... (4 Replies)
Discussion started by: firdousamir
4 Replies

10. Shell Programming and Scripting

Append a field to the end of each line of a file based on searching another file.

Hi All, I have two comma separated value(CSV) files, say FileA and FileB. The contents looks like that shown below. FileA EmpNo,Name,Age,Sex, 1000,ABC,23,M, 1001,DES,24,F, ... (2 Replies)
Discussion started by: ultimate
2 Replies
Login or Register to Ask a Question