Replace per line with first line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace per line with first line
# 1  
Old 12-05-2014
Replace per line with first line

hi expert,

i have log like this
Code:
AA-HOST-0
5 28
4 28
6 30
3 26
7 36
8 36
1 46
2 34
BB-HOST-4
6 0
4 0
1 0
5 0
3 0
7 0
2 0
8 0
BB-HOST-4
5 0
2 0
6 0
4 0
1 0
3 0
7 0
8 0


I want the output become :

Code:
AA-HOST-0 5 28
AA-HOST-0 4 28
AA-HOST-0 6 30
AA-HOST-0 3 26
AA-HOST-0 7 36
AA-HOST-0 8 36
AA-HOST-0 1 46
AA-HOST-0 2 34
BB-HOST-4 6 0
BB-HOST-4 4 0
BB-HOST-4 1 0
BB-HOST-4 5 0
BB-HOST-4 3 0
BB-HOST-4 7 0
BB-HOST-4 2 0
BB-HOST-4 8 0
BB-HOST-4 5 0
BB-HOST-4 2 0
BB-HOST-4 6 0
BB-HOST-4 4 0
BB-HOST-4 1 0
BB-HOST-4 3 0
BB-HOST-4 7 0
BB-HOST-4 8 0

Anyone can help?
# 2  
Old 12-05-2014
Code:
awk '$1 !~ /^[0-9]*$/ {key=$0; next} {print key, $0}' file

This User Gave Thanks to junior-helper For This Post:
# 3  
Old 12-05-2014
Hello justbow,

Following may help you, it will work only if we have not more than 2 columns and if this has one column it has the AA-HOST-04 etc values in it.

Code:
awk 'NF==1{V=$0;next} NF>1{$0=V OFS $0} 1' Input_file

Output will be as follows.
Code:
AA-HOST-0 5 28
AA-HOST-0 4 28
AA-HOST-0 6 30
AA-HOST-0 3 26
AA-HOST-0 7 36
AA-HOST-0 8 36
AA-HOST-0 1 46
AA-HOST-0 2 34
BB-HOST-4 6 0
BB-HOST-4 4 0
BB-HOST-4 1 0
BB-HOST-4 5 0
BB-HOST-4 3 0
BB-HOST-4 7 0
BB-HOST-4 2 0
BB-HOST-4 8 0
BB-HOST-4 5 0
BB-HOST-4 2 0
BB-HOST-4 6 0
BB-HOST-4 4 0
BB-HOST-4 1 0
BB-HOST-4 3 0
BB-HOST-4 7 0
BB-HOST-4 8 0

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 12-08-2014
Code:
$ awk ' /HOST/ { host=$0; getline } $0=(host" "$0) ' file
AA-HOST-0 5 28
AA-HOST-0 4 28
AA-HOST-0 6 30
AA-HOST-0 3 26
AA-HOST-0 7 36
AA-HOST-0 8 36
AA-HOST-0 1 46
AA-HOST-0 2 34
BB-HOST-4 6 0
BB-HOST-4 4 0
BB-HOST-4 1 0
BB-HOST-4 5 0
BB-HOST-4 3 0
BB-HOST-4 7 0
BB-HOST-4 2 0
BB-HOST-4 8 0
BB-HOST-4 5 0
BB-HOST-4 2 0
BB-HOST-4 6 0
BB-HOST-4 4 0
BB-HOST-4 1 0
BB-HOST-4 3 0
BB-HOST-4 7 0
BB-HOST-4 8 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

4. Shell Programming and Scripting

Replace first field of a line with previous filed of the line

Hi Everyone, I have a file as below: IM2345638,sherfvf,usha,30 IM384940374,deiufbd,usha,30 IM323763822,cdejdkdnbds,theju,15 0,dhejdncbfd,us,20 IM398202038,dhekjdkdld,tj,30 0,foifsjd,u2,40 The output i need is as below IM2345638,sherfvf,usha,30... (4 Replies)
Discussion started by: usha rao
4 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

7. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

8. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

9. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question