awk print header as text from separate file with getline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print header as text from separate file with getline
# 1  
Old 12-14-2011
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:

Code:
awk 'BEGIN{FS="_";print ((getline < "header.txt")>0)} { if (! ($0 ~ /EL/ )  print }" input.txt

What am i doing wrong?
# 2  
Old 12-14-2011
Try:
Code:
awk 'NR==FNR{print;next}!/EL/' header.txt input.txt

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-14-2011
Quote:
Originally Posted by bartus11
Try:
Code:
awk 'NR==FNR{print;next}!/EL/' header.txt input.txt

Thanks! Just curious, how would the code look like with text from a sperate second file at the end/bottom of the output-file?
# 4  
Old 12-14-2011
You appear to be trying to print the return value of getline, but it returns a status, not the record read.

getline sets $0 if you don't specify a variable, so you could have:
Code:
awk 'BEGIN{FS="_"; if ((getline < "header.txt") > 0) {print}} { if (! ($0 ~ /EL/ )  print }" input.txt

# 5  
Old 12-14-2011
Code:
awk 'NR==FNR && !/EL/;NR!=FNR' input.txt end.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: verdepollo
3 Replies

2. Shell Programming and Scripting

Print header and lines that meet both conditions in awk

In the awk below I am trying to print only the header lines starting with # or ## and the lines that $7 is PASS and AF= is less than 5%. The awk does execute but returns an empty file and I am not sure what I am doing wrong. Thank you. file ... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Programming

Read text from file and print each character in separate line

performing this code to read from file and print each character in separate line works well with ASCII encoded text void preprocess_file (FILE *fp) { int cc; for (;;) { cc = getc (fp); if (cc == EOF) break; printf ("%c\n", cc); } } int main(int... (1 Reply)
Discussion started by: khaled79
1 Replies

5. Shell Programming and Scripting

awk getline 8 times and if $3 = 8 when subtracted from 1st line,print

I have kind of a strange one here. I have a file of consecutive /24 ip blocks. If there are 8 consecutive ip blocks which represent a /20 then I need to print the first line. I played around and did not get the results I need, especially when considering that the highest $3 will be is 255 and then... (6 Replies)
Discussion started by: numele
6 Replies

6. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

7. Shell Programming and Scripting

awk getline t file

I want to import a textfile with getline into var t which has several lines. How do import all lines, since it only imports the last line: while < ((getline t "textfile") > 0) (7 Replies)
Discussion started by: sdf
7 Replies

8. Shell Programming and Scripting

awk/sed script to print each line to a separate named file

I have a large 3479 line .csv file, the content of which looks likes this: 1;0;177;170;Guadeloupe;x 2;127;171;179;Antigua and Barbuda;x 3;170;144;2;Umpqua;x 4;170;126;162;Coos Bay;x ... 1205;46;2;244;Unmak Island;x 1206;47;2;248;Yunaska Island;x 1207;0;2;240;north sea;x... (5 Replies)
Discussion started by: kalelovil
5 Replies

9. Shell Programming and Scripting

Need awk help to print specific columns with as string in a header

awk experts, I have a big file of 4000 columns with header. Would like to print the columns with string value of "Commands" in header. File has "," separator. This file is on ESX host with Bash. Thanks, Arv (21 Replies)
Discussion started by: arv_cds
21 Replies

10. Shell Programming and Scripting

awk - getline from parametric file name

Hi! I have an input file for an awk script that I need to split into several files and the process them separately line by line. I have splitted the input file into the other files, that have been created correctly. But, since their names are parametric (i.e. output_1.txt, output_2.txt..... (2 Replies)
Discussion started by: Alice236
2 Replies
Login or Register to Ask a Question