Print filename and last line using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print filename and last line using awk
# 15  
Old 11-03-2012
Quote:
Originally Posted by ysrini
... so checking for zero doesn't work
If you check that c.sh exists an is not empty and tail -1 c.sh| wc -l returns 0

then this file meet the condition you are looking for.
So checking for zero is not sufficient but if brought with a test -s then it should work doesn't it ?
At least, that was what i wanted to point out in my previous post.
# 16  
Old 11-03-2012
The files can be zero or non-zero size.

"ALL i want is a single line command that will print all filenames in the current directory along with the file last line where the file doesn't end with a newline"

A tested working solution as answer will be appreciated
# 17  
Old 11-04-2012
Quote:
Originally Posted by ysrini
The files can be zero or non-zero size.

"ALL i want is a single line command that will print all filenames in the current directory along with the file last line where the file doesn't end with a newline"

A tested working solution as answer will be appreciated
Files with the last newline character missing are not proper UNIX format. Awk just processes such a file as if the file did have a closing newline. So inside an awk script it cannot be detected whether or not it was present in the file..

You would need to use another way to detect whether it is present, or just convert all files to proper format, for example:
Code:
awk 1 file > newfile

GNU sed or BSD sed could use in-place editing, something like:
Code:
sed -i'.bak' '' file


--
Quote:
Originally Posted by ripat
Works with the original awk, mawk, nawk, gawk and gawk in its traditionnal mode. If OP needs to process big files, it's about two times faster.

See also awk.info Handy One Liners
Alister is right though, that does not work with POSIX awk or nawk on Solaris and HPUX for example. That page does not include an example where $0 is used in the END section BTW..

Last edited by Scrutinizer; 11-04-2012 at 03:46 AM..
# 18  
Old 11-04-2012
Quote:
Originally Posted by ysrini
The files can be zero or non-zero size.

"ALL i want is a single line command that will print all filenames in the current directory along with the file last line where the file doesn't end with a newline"

A tested working solution as answer will be appreciated
I believe what you should expect from this forum is proposals, suggestions, and consultancy so you can come up with a solution yourself AND test it by yourself. Anything beyond that is nice but not a given.
However . . . -- this is a proposal that works on my linux box BUT it does not cope with empty files. Please feel free to use and test it or dump it...
Code:
for i in *.sh; do  [ '0a' != $(od -An -tx1 -j$(( $(stat -c%s $i) - 1 )) $i) ] && { echo -ne $i"\t"; tail -n1 $i; } done
a.sh    apple$

The $ sign is the next command prompt as no newline char is output - as requested.
# 19  
Old 11-04-2012
Quote:
Originally Posted by Scrutinizer
That page does not include an example where $0 is used in the END section BTW..
Yes, it does. Look for Print the last line of a file (emulates "tail -1") 2/3 down the page.
awk.info Handy One Liners
# 20  
Old 11-04-2012
As has already been pointed out, any attempt to process an incomplete line at the end of a file with awk may or may not work. The standards say that tail (when using options that count lines), grep, and sed as well as awk produce unspecified results if the input files they process are not text files. (And, by definition, a non-empty text file's last character is a <newline>.)

A side note on the discussion about $0 in END actions in awk: The standards are silent about whether $0, $1, ... $NF are valid in END actions. However they require that FILENAME, FNR, and NR have the values they had for the last line read when used in END actions. They also require that:
Quote:
Inside an END action, NF shall retain the value it had for the last record read, unless a subsequent, redirected, getline function without a var argument is performed prior to entering the END action.
Since NF has to be preserved, I don't see why $0 through $NF woudn't also be preserved under the same conditions; but the standards don't require awk to do so.

Rudi has given you a script that will work on some systems, but it won't work on OS X, Solaris, or several other UNIX and UNIX-like systems.

The following script doesn't use anything that isn't specified by the standards, so it should work on almost any system. It was tested using ksh, but should work for any POSIX conforming shell.

This script will tell you if a file is missing the terminating <newlin>, but it does not attempt to print the incomplete line. Although I could do that in a shell script, if I had a REAL need to portably print incomplete lines, I'd do it in C; not shell.
Code:
for i in *.sh
do
        if [ ! -e "$i" ] || [ ! -r "$i" ]
        then    printf "%s: not a readable regular file\n" "$i"
        elif [ ! -s "$i" ]
        then    printf "%s: empty file\n" "$i"
        else    # Use 012 below for ASCII, 045 for EBCDIC
                if ! tail -c -1 "$i" | od -b | grep -Fq ' 012'
                then    printf "%s: missing <newline>\n" "$i"
                fi
        fi
done


Last edited by Don Cragun; 11-04-2012 at 06:57 PM.. Reason: Deleted stuff about 1-line scripts (I was working on two threads at the same time and that issue doesn't apply here.)
This User Gave Thanks to Don Cragun For This Post:
# 21  
Old 11-04-2012
Quote:
Originally Posted by ripat
Yes, it does. Look for Print the last line of a file (emulates "tail -1") 2/3 down the page.
awk.info Handy One Liners
OK you're right, my bad.. So yes the example on that page is incorrect, in the sense that it will not work with every awk..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to print filename words along with delimiter

Hi, I have filename as: 010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937 I need first 5 words of my filename along with the respective delimiters: I tried this: f=010020001_S-FOR-Sort-SYEXC_20180109_062320_0100.x937 echo $f | awk -F '' '{print $1$2$3$4$5}' 010020001SFORSortSYEXC ... (11 Replies)
Discussion started by: gnnsprapa
11 Replies

2. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

awk to print the line that matches and the next if line is wrapped

I have a file and when I match the word "initiators" in the first column I need to be able to print the rest of the columns in that row. This is fine for the most part but on occasion the "initiators" line gets wrapped to the next line. Here is a sample of the file. caw-enabled ... (3 Replies)
Discussion started by: kieranfoley
3 Replies

4. UNIX for Advanced & Expert Users

Problem piping find output to awk, 1st line filename is truncated, other lines are fine.

Today I needed to take a look through a load of large backup files, so I wrote the following line to find them, order them by size, and print the file sizes in GB along with the filename. What happened was odd, the output was all as expected except for the first output line which had the filename... (4 Replies)
Discussion started by: gencon
4 Replies

5. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

6. Shell Programming and Scripting

Print filename with awk

I can got the filename with this script. it's only show "-" in result. cut -d , -f7 CSV_d.* | awk 'OFS=":"{print FILENAME,substr($1,1,8),substr($1,9,2),substr($1,11,2),substr($1,13,2)}' | sort |uniq (2 Replies)
Discussion started by: before4
2 Replies

7. Shell Programming and Scripting

awk to print on the same line

Hi all, I've a script that uses awk to parse the output of wget during a database update. The code I use is: wget -c ftp://ftpsite/file 2>&1 | awk '/0%/ {print}'But this spits out each progress line on a new line: 37250K .......... .......... .......... .......... .......... 80% 10.9M 1s ... (2 Replies)
Discussion started by: euval
2 Replies

8. Shell Programming and Scripting

Howto Print File Path or Print the Filename

I'm trying to clean up my samba share and need to print the found file or print the path of the image it tried to searched for. So far I have this but can't seem to get the logic right. Can anyone help point me in the right direction? for FILE in `cat list`; do if ; then ... (1 Reply)
Discussion started by: overkill
1 Replies

9. Shell Programming and Scripting

awk print the next line on the current line

Ok I have a file with hundreds of lines, four columns, space delimited, TESTB.TXT for example TESTB.TXT --- AA ZZ 12 34 BB YY 56 78 CC XX 91 23 DD VV 45 67 --- I want a new file that has 7 columns, the first four are identical, and the next 3 are the last three of the next line...so... (5 Replies)
Discussion started by: ajp7701
5 Replies

10. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

how to print any required line by its line no using awk and its NR variable for eg: ------------ 121343 adfdafd 21213sds dafadfe432 adf.adf%adf --------------- requied o/p if give num=3 it print: 21213sds -------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies
Login or Register to Ask a Question