Sponsored Content
Top Forums Shell Programming and Scripting Find header in a text file and prepend it to all lines until another header is found Post 303036770 by jim mcnamara on Wednesday 10th of July 2019 10:50:23 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Mojo::DOM(3pm)						User Contributed Perl Documentation					    Mojo::DOM(3pm)

NAME
Mojo::DOM - Minimalistic HTML5/XML DOM parser with CSS3 selectors SYNOPSIS
use Mojo::DOM; # Parse my $dom = Mojo::DOM->new('<div><p id="a">A</p><p id="b">B</p></div>'); # Find my $b = $dom->at('#b'); say $b->text; # Walk say $dom->div->p->[0]->text; say $dom->div->children('p')->first->{id}; # Iterate $dom->find('p[id]')->each(sub { say shift->{id} }); # Loop for my $e ($dom->find('p[id]')->each) { say $e->text; } # Modify $dom->div->p->[1]->append('<p id="c">C</p>'); # Render say $dom; DESCRIPTION
Mojo::DOM is a minimalistic and relaxed HTML5/XML DOM parser with CSS3 selector support. It will even try to interpret broken XML, so you should not use it for validation. CASE SENSITIVITY
Mojo::DOM defaults to HTML5 semantics, that means all tags and attributes are lowercased and selectors need to be lowercase as well. my $dom = Mojo::DOM->new('<P ID="greeting">Hi!</P>'); say $dom->at('p')->text; say $dom->p->{id}; If XML processing instructions are found, the parser will automatically switch into XML mode and everything becomes case sensitive. my $dom = Mojo::DOM->new('<?xml version="1.0"?><P ID="greeting">Hi!</P>'); say $dom->at('P')->text; say $dom->P->{ID}; XML detection can also be disabled with the "xml" method. # Force XML semantics $dom->xml(1); # Force HTML5 semantics $dom->xml(0); METHODS
Mojo::DOM implements the following methods. "new" my $dom = Mojo::DOM->new; my $dom = Mojo::DOM->new('<foo bar="baz">test</foo>'); Construct a new Mojo::DOM object. "all_text" my $trimmed = $dom->all_text; my $untrimmed = $dom->all_text(0); Extract all text content from DOM structure, smart whitespace trimming is enabled by default. # "foo bar baz" $dom->parse("<div>foo <p>bar</p>baz </div>")->div->all_text; # "foo barbaz " $dom->parse("<div>foo <p>bar</p>baz </div>")->div->all_text(0); "append" $dom = $dom->append('<p>Hi!</p>'); Append to element. # "<div><h1>A</h1><h2>B</h2></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->append('<h2>B</h2>'); "append_content" $dom = $dom->append_content('<p>Hi!</p>'); Append to element content. # "<div><h1>AB</h1></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->append_content('B'); "at" my $result = $dom->at('html title'); Find a single element with CSS3 selectors. All selectors from Mojo::DOM::CSS are supported. # Find first element with "svg" namespace definition my $namespace = $dom->at('[xmlns:svg]')->{'xmlns:svg'}; "attrs" my $attrs = $dom->attrs; my $foo = $dom->attrs('foo'); $dom = $dom->attrs({foo => 'bar'}); $dom = $dom->attrs(foo => 'bar'); Element attributes. "charset" my $charset = $dom->charset; $dom = $dom->charset('UTF-8'); Alias for "charset" in Mojo::DOM::HTML. "children" my $collection = $dom->children; my $collection = $dom->children('div'); Return a Mojo::Collection object containing the children of this element, similar to "find". # Show type of random child element say $dom->children->shuffle->first->type; "content_xml" my $xml = $dom->content_xml; Render content of this element to XML. # "<b>test</b>" $dom->parse('<div><b>test</b></div>')->div->content_xml; "find" my $collection = $dom->find('html title'); Find elements with CSS3 selectors and return a Mojo::Collection object. All selectors from Mojo::DOM::CSS are supported. # Find a specific element and extract information my $id = $dom->find('div')->[23]{id}; # Extract information from multiple elements my @headers = $dom->find('h1, h2, h3')->map(sub { shift->text })->each; "namespace" my $namespace = $dom->namespace; Find element namespace. # Find namespace for an element with namespace prefix my $namespace = $dom->at('svg > svg:circle')->namespace; # Find namespace for an element that may or may not have a namespace prefix my $namespace = $dom->at('svg > circle')->namespace; "parent" my $parent = $dom->parent; Parent of element. "parse" $dom = $dom->parse('<foo bar="baz">test</foo>'); Alias for "parse" in Mojo::DOM::HTML. # Parse UTF-8 encoded XML my $dom = Mojo::DOM->new->charset('UTF-8')->xml(1)->parse($xml); "prepend" $dom = $dom->prepend('<p>Hi!</p>'); Prepend to element. # "<div><h1>A</h1><h2>B</h2></div>" $dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend('<h1>A</h1>'); "prepend_content" $dom = $dom->prepend_content('<p>Hi!</p>'); Prepend to element content. # "<div><h2>AB</h2></div>" $dom->parse('<div><h2>B</h2></div>')->at('h2')->prepend_content('A'); "replace" $dom = $dom->replace('<div>test</div>'); Replace elements. # "<div><h2>B</h2></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace('<h2>B</h2>'); "replace_content" $dom = $dom->replace_content('test'); Replace element content. # "<div><h1>B</h1></div>" $dom->parse('<div><h1>A</h1></div>')->at('h1')->replace_content('B'); "root" my $root = $dom->root; Find root node. "text" my $trimmed = $dom->text; my $untrimmed = $dom->text(0); Extract text content from element only (not including child elements), smart whitespace trimming is enabled by default. # "foo baz" $dom->parse("<div>foo <p>bar</p>baz </div>")->div->text; # "foo baz " $dom->parse("<div>foo <p>bar</p>baz </div>")->div->text(0); "text_after" my $trimmed = $dom->text_after; my $untrimmed = $dom->text_after(0); Extract text content immediately following element, smart whitespace trimming is enabled by default. # "baz" $dom->parse("<div>foo <p>bar</p>baz </div>")->div->p->text_after; # "baz " $dom->parse("<div>foo <p>bar</p>baz </div>")->div->p->text_after(0); "text_before" my $trimmed = $dom->text_before; my $untrimmed = $dom->text_before(0); Extract text content immediately preceding element, smart whitespace trimming is enabled by default. # "foo" $dom->parse("<div>foo <p>bar</p>baz </div>")->div->p->text_before; # "foo " $dom->parse("<div>foo <p>bar</p>baz </div>")->div->p->text_before(0); "to_xml" my $xml = $dom->to_xml; Render this element and its content to XML. # "<div><b>test</b></div>" $dom->parse('<div><b>test</b></div>')->div->to_xml; "tree" my $tree = $dom->tree; $dom = $dom->tree(['root', [qw(text lalala)]]); Alias for "tree" in Mojo::DOM::HTML. "type" my $type = $dom->type; $dom = $dom->type('div'); Element type. # List types of child elements $dom->children->each(sub { say $_->type }); "xml" my $xml = $dom->xml; $dom = $dom->xml(1); Alias for "xml" in Mojo::DOM::HTML. CHILD ELEMENTS
In addition to the methods above, many child elements are also automatically available as object methods, which return a Mojo::DOM or Mojo::Collection object, depending on number of children. say $dom->p->text; say $dom->div->[23]->text; $dom->div->each(sub { say $_->text }); ELEMENT ATTRIBUTES
Direct hash reference access to element attributes is also possible. say $dom->{foo}; say $dom->div->{id}; SEE ALSO
Mojolicious, Mojolicious::Guides, <http://mojolicio.us>. perl v5.14.2 2012-09-05 Mojo::DOM(3pm)
All times are GMT -4. The time now is 03:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy