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.
"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
"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:
GNU sed or BSD sed could use in-place editing, something like:
--
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.
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..
"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...
The $ sign is the next command prompt as no newline char is output - as requested.
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.
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:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)