Display the file name on each line using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display the file name on each line using awk
# 1  
Old 06-16-2015
Display the file name on each line using awk

How do I display the filename that has been awk to each of the line in Unix, i need to so far I have tried {print FILENAME;nextfile} but to no avail.

Code:
`awk -F, '/1.2 Install TCP Wrappers/ {P=0} /1.1 Apply latest OS patches/ {P=1} P'  solarisappsummary.txt solarisdbsummary.txt`


Code:
For solarisappsummary.txt 
1.1 Apply latest OS patches
                    Oracle Solaris 10 8/11 s10x_u10wos_17b X86

For solarisdbsummary.txt
1.1 Apply latest OS patches
Oracle Solaris 10 8/11 s10x_u10wos_17b X86

# 2  
Old 06-16-2015
Hello alvinoo ,

You can try following changes in your code and let me know if that helps.
1st: Please change . to \., to remove it's special meaning in search part of awk. For example into your case. /1\.2 Install TCP Wrappers/ AND /1\.1 Apply latest OS patches/.
2nd: As mentioned by you in your post, on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk, or nawk.
3rd: Also I have seen symbol ` in starting of awk if you are NOT storing this result to a variable then kindly remove them too.

So it can be something like below.
Code:
 nawk -F, '/1\.2 Install TCP Wrappers/ {P=0} /1\.1 Apply latest OS patches/ {P=1} P{print FILENAME}'  solarisappsummary.txt solarisdbsummary.txt

Hope this may help you.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-17-2015
Hi Ravinder,

It works but it will print the filename but not the actual output itself. Do advise as I need to awk two files.
# 4  
Old 06-17-2015
Try
Code:
{print FILENAME, $0; nextfile}

# 5  
Old 06-17-2015
Your description isn't at all clear. Why are you setting the input field separator to a comma if you aren't doing anything with any fields? If you're trying to print a line giving the name of the file followed by the contents of the file, try:
Code:
awk 'FNR==1{printf("From file %s:\n", FILENAME)}1' file1 file2

If you're trying to print files with the file's name at the start of each line from that file, try:
Code:
awk '{print FILENAME, $0}' file1 file2

If you're trying either of these on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.

If neither of these are what you're trying to do, show us some sample contents for your two input files, give a full English description of the changes you want to make to the input, and show us the output you're trying to produce from those sample inputs.

The two code segments you have shown us do not even come close to doing what you described. And, the various versions of awk on most Solaris systems do not include a nextfile operator (and your description doesn't seem to need it even if your version of awk does include it).
# 6  
Old 06-18-2015
Quote:
Originally Posted by Don Cragun
Your description isn't at all clear. Why are you setting the input field separator to a comma if you aren't doing anything with any fields? If you're trying to print a line giving the name of the file followed by the contents of the file, try:
Code:
awk 'FNR==1{printf("From file %s:\n", FILENAME)}1' file1 file2

If you're trying to print files with the file's name at the start of each line from that file, try:
Code:
awk '{print FILENAME, $0}' file1 file2

If you're trying either of these on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.

If neither of these are what you're trying to do, show us some sample contents for your two input files, give a full English description of the changes you want to make to the input, and show us the output you're trying to produce from those sample inputs.

The two code segments you have shown us do not even come close to doing what you described. And, the various versions of awk on most Solaris systems do not include a nextfile operator (and your description doesn't seem to need it even if your version of awk does include it).
Hi,

Managed to solved my problem already thanks.

Code:
$ echo "1.1.1.3.1.1.1;"`var=$(awk '/Privilege Use/ {P=0} /Object Access/ {P=1} P {print FILENAME, $0}' AdvancedAudit.txt AdvancedAudit2.txt | grep -ia 'File System') ; echo $var; echo $var | grep -iq "No Auditing" && echo ';T' || echo ';F' `
1.1.1.3.1.1.1;AdvancedAudit.txt File System No Auditing AdvancedAudit2.txt File System No Auditing ;T

# 7  
Old 07-06-2015
Print Only Filename with Values in it

Hi there,

How can I print only the filename with values? The one in bold.

Code:
echo '<tr><td>4.4.1.2 </td><td>' ; awk '/[Cc]:\\/ {P=0} P {print $0 "<br>" } FNR==1{printf("File %s:\n", FILENAME)} /system32\\at.exe/ {P=1} '               $fileset   ;echo $var '</td></tr>'

Code:
<tr><td>4.4.1.2 </td><td>
File Regedit_NTSLA56.txt:
File Regedit_NTSLA57.txt:
File System32_NTSLA56.txt:
                           NT AUTHORITY\SYSTEM:F <br>
<br>
File System32_NTSLA57.txt:
                         NT AUTHORITY\SYSTEM:F <br>
<br>
File SystemDrivePermission_NTSLA56.txt:
File SystemDrivePermission_NTSLA57.txt:
                         NT AUTHORITY\SYSTEM:F <br>
<br>

Code:
C:\WINDOWS\system32\regedt32.exe BUILTIN\Administrators:F 
                                 NT AUTHORITY\SYSTEM:F 

C:\WINDOWS\system32\dllcache\regedt32.exe BUILTIN\Administrators:F 
                                          NT AUTHORITY\SYSTEM:F

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search first file line after Error and display

I have a log file which contains information like below (more than 200 ERROR sets). Here I want to find first .c file and function after "ERROR: AddressSanitizer" line. If you see here after "ERROR:" line first file - asfrecohandling.c function - ASFPotRecoHandling_Create_RecPaxSrvcComp ... (6 Replies)
Discussion started by: pushpabuzz
6 Replies

2. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

3. Shell Programming and Scripting

Display all the matches lines in one line using awk

Please can you let me know how to print all the matching lines from a file in one single line using awk. Thanks I have the following data in the input file data1 voice2 voice1 speech1 data2 data3 ... ... voice4 speech2 data4 and the output should be as follows data1 data2... (4 Replies)
Discussion started by: Sudhakar333
4 Replies

4. Shell Programming and Scripting

How to display the line number of file while searching for a pattern

awk 'BEGIN{IGNORECASE=1} /error|warning|exception/ { ++x } END { print x }' filename The above command returning the number of times the pattern present in the file. But I want the the line number as well. please help me out (6 Replies)
Discussion started by: arukuku
6 Replies

5. Shell Programming and Scripting

awk & display on one line

awk '/description/ || /instances/ {print;getline;print}' rtprod2.scp This command gives me something like below. description=OpsExec_Clinical instances=0 description=OpsExec_Pharmacy instances=1 description= instances=0 description= instances=0 description= description=OP_MVOR_ORU_OUT... (9 Replies)
Discussion started by: Daniel Gate
9 Replies

6. Shell Programming and Scripting

Display 3rd line of a file using cut only

Hello, i want to use new line character in cut command i want to display 3rd line of a file using cut only( not by sed or head -tail command) can anyone suggest me ? Regards (12 Replies)
Discussion started by: Deepak Dutt
12 Replies

7. UNIX for Dummies Questions & Answers

awk - display from line number to regex

Hi. Is there a way in awk to show all lines between a line number and the next line containing a particular regex? We can do these, of course: awk '/regex1/,/regex2/' filename awk 'FNR > X && FNR < Y' filename But can they be combined? Thanks. (3 Replies)
Discussion started by: treesloth
3 Replies

8. Shell Programming and Scripting

Display a particular line from a file

Hi, What is the command i can use iof i want to display a particular line from a file, i have the line number with me. (5 Replies)
Discussion started by: Rohini Vijay
5 Replies

9. Shell Programming and Scripting

Can't figure out how to display specific data from a line using awk.

Hello, I cannot figure this one out. I would like to do the following. I have a line that has 7 words. It is possible that the line can have 20 words too. I always want to show the 9th word and beyond. The 9th word will always change so I do not have something to search for, so I think... (1 Reply)
Discussion started by: ctcuser
1 Replies
Login or Register to Ask a Question