Find header in a text file and prepend it to all lines until another header is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find header in a text file and prepend it to all lines until another header is found
# 1  
Old 07-10-2019
Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty.

I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the appliance the metrics belong to, there's a header (the name of the asset basically) that is printed just once before all the rest of the text.

What I want to do is find the appliance name and print it before each one of the metrics. See below:

Sample input:

Code:
applianceName1
1.0 123 some
5.3 456 random
3.5 78900 string
applianceName2
1.2 1234 another
0.1 023 random
3.0 10 string
applianceName3
0.2 6544676 more
0.9 3543 random
4.1 123 stuff

Expected output:
Code:
applianceName1 1.0 123 some
applianceName1 5.3 456 random
applianceName1 3.5 78900 string
applianceName2 1.2 1234 another
applianceName2 0.1 023 random
applianceName2 3.0 10 string
applianceName3 0.2 6544676 more
applianceName3 0.9 3543 random
applianceName3 4.1 123 stuff

The ending file should, therefore, contain lines with 4 fields each one. Any clues or pointers would be appreciated.

Thanks!
# 2  
Old 07-10-2019
Using awk -

Code:
$ awk '/^app/ {pre=$0; next}
    length($0) {print pre " " $0 } ' filename > newfile

$ cat newfile
applianceName1 1.0 123 some
applianceName1 5.3 456 random
applianceName1 3.5 78900 string
applianceName2 1.2 1234 another
applianceName2 0.1 023 random
applianceName2 3.0 10 string
applianceName3 0.2 6544676 more
applianceName3 0.9 3543 random
applianceName3 4.1 123 stuff

It's late so I cannot stay on long to help you figure out how to do it. Otherwise I would not have just blurted out a ho-hum answer.
Next time please tell us your OS and shell so we can give you good help. The idea is to get you able to do all this by yourself....
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 07-11-2019
I'm guessing that all the appliance names do not start with the text "app".

A bit more of a generic approach might be to treat lines with only one field as an appliance name (assuming here that there are no spaces within the appliance name). The awk system variable NF contains the number of fields in the line so instead of /^app/ (line begins with app) to identify headers you could use NF == 1 (Number of fields on this line equals one) or even NF != 3 (Number of fields is not 3)
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 4  
Old 07-11-2019
That worked out beautifully. And yes, the asset name could be any FQDN so no spaces at all. Thanks a lot!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Put Header on Text file of all column

Hi I have in put file A.txt ABCDE1 JFHFJFJF3 1 1 SC1 12/10 ABCDE2 JFHFJFJF5 1 1 SC1 12/10 ABCDE3 JFHFJFJF5 1 1 SC1 12/10 ABCDE4 JFHFJFJF6 1 1 SC1 12/10 I want output in .csv with header: Name SUb_N x y No Board ABCDE1 JFHFJFJF3 1 1 SC1 12/10 ABCDE2 JFHFJFJF5 1 1 SC1... (7 Replies)
Discussion started by: pareshkp
7 Replies

2. Shell Programming and Scripting

awk print header as text from separate file with getline

I would like to print the output beginning with a header from a seperate file like this: awk 'BEGIN{FS="_";print ((getline < "header.txt")>0)} { if (! ($0 ~ /EL/ ) print }" input.txtWhat am i doing wrong? (4 Replies)
Discussion started by: sdf
4 Replies

3. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

4. Shell Programming and Scripting

Extract date from file header and prefix it to all lines

Hello All, I have a file in the following format. I want to extract the date(020090930, 020090929) in the string "STPAGE020090930" and "STPAGE020090929" and prefix it to all lines below them. The output must be put into a new file. STPAGE020090930 xyzz aalc... (3 Replies)
Discussion started by: john2022
3 Replies

5. Shell Programming and Scripting

how can I bcp out a table into a text file including the header row in the text file

Hi All, I need to BCP out a table into a text file along with the table headers. Normal BCP out command only bulk copies the data, and not the headers. I am using the following command: bcp database1..table1 out file1.dat -c -t\| -b1000 -A8192 -Uuser -Ppassword -efile.dat.err Regards,... (0 Replies)
Discussion started by: shilpa_acc
0 Replies

6. UNIX for Dummies Questions & Answers

Sort and uniq lines of a file while keeping a header line

So, I have a file that has some duplicate lines. The file has a header line that I would like to keep at the top. I could do this by extracting the header from the file, 'sort -u' the remaining lines, and recombine them. But they are quite big, so if there is a way to do it with a single... (1 Reply)
Discussion started by: Digby
1 Replies

7. Programming

to find header in Mp3 file and retrieve data

hi all, In an mp3 file , data is arranged in sequence of header and data ,how to retrieve data between two headers. Is the data between two headers fixed? because as per theory it says 1152 samples will be there , but dont knw how many bits one sample correspond to? it would help if any c... (2 Replies)
Discussion started by: shashi
2 Replies

8. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies

9. Shell Programming and Scripting

Convert Header into Column in Text file

Hi Gurus, I have a requirement like this and have to create a UX shell scripts. Thanks in advance. File-in: ------ Header2007-12-012007-11-21 100|xyz|was 101|wsa|qws ...... ....... Output should be: ------------------- 2007-12-01|100|xyz|was 2007-12-01|101|wsa|qws ...... .......... (7 Replies)
Discussion started by: vsubbu1000
7 Replies

10. Shell Programming and Scripting

Total of lines w/out header and footer incude for a file

I am trying to get a total number of tapes w/out headers or footers in a ERV file and append it to the file. For some reason I cannot get it to work. Any ideas? #!/bin/sh dat=`date +"%b%d_%Y"` + date +%b%d_%Y dat=Nov16_2006 tapemgr="/export/home/legato/tapemgr/rpts"... (1 Reply)
Discussion started by: gzs553
1 Replies
Login or Register to Ask a Question